84 lines
2.7 KiB
TypeScript
84 lines
2.7 KiB
TypeScript
<<<<<<< HEAD
|
|
import HomeView from "@/components/views/HomeView";
|
|
import PageRuntime from "@/components/PageRuntime";
|
|
|
|
export const metadata = { title: "หน้าแรก" };
|
|
|
|
export default async function Page({
|
|
searchParams,
|
|
}: {
|
|
searchParams: Promise<Record<string, string | string[] | undefined>>;
|
|
}) {
|
|
const sp = await searchParams;
|
|
const query: Record<string, string> = {};
|
|
// ลิงก์สั้นเดิม: #/?s=<รหัสสถานี> และ #/?p=<province_id>
|
|
for (const key of ["s", "p"]) {
|
|
const value = sp[key];
|
|
if (typeof value === "string") query[key] = value;
|
|
}
|
|
return (
|
|
<>
|
|
<HomeView />
|
|
<PageRuntime page="home" query={query} />
|
|
=======
|
|
import { api, type RouteListItem, type Schedule } from "@/lib/api";
|
|
import { VisitTracker } from "@/lib/visit-tracker";
|
|
import HomeClient from "@/components/HomeClient";
|
|
|
|
export default async function HomePage({
|
|
searchParams,
|
|
}: {
|
|
searchParams: Promise<{ p?: string; s?: string }>;
|
|
}) {
|
|
const sp = await searchParams;
|
|
|
|
const [groupsRes, routesRes, schedRes, provRes, stopsRes] = await Promise.allSettled([
|
|
api.routeGroups(),
|
|
api.routes(),
|
|
api.schedules({}),
|
|
api.provinces(),
|
|
api.stops(),
|
|
]);
|
|
|
|
const groups = groupsRes.status === "fulfilled" ? groupsRes.value.items : [];
|
|
const routes: RouteListItem[] = routesRes.status === "fulfilled" ? routesRes.value.items : [];
|
|
const schedules: Schedule[] = schedRes.status === "fulfilled" ? schedRes.value.items : [];
|
|
const provinces = provRes.status === "fulfilled" ? provRes.value.items : [];
|
|
const stops = stopsRes.status === "fulfilled" ? stopsRes.value.items : [];
|
|
|
|
// route_id -> all departure times
|
|
const timesByRoute: Record<string, string[]> = {};
|
|
for (const s of schedules) {
|
|
(timesByRoute[s.route_id] ??= []).push(...s.departure_times);
|
|
}
|
|
|
|
// route_id -> station ids (for station-level filtering)
|
|
const routeStations: Record<string, number[]> = {};
|
|
await Promise.all(
|
|
routes.map(async (r) => {
|
|
const { items } = await api.routeStops(r.route_id).catch(() => ({ items: [] }));
|
|
routeStations[r.route_id] = items.map((x) => x.station_id);
|
|
}),
|
|
);
|
|
|
|
const initialProvinceId = sp.p ? Number(sp.p) || undefined : undefined;
|
|
const initialStationCode = sp.s ?? undefined;
|
|
|
|
return (
|
|
<>
|
|
<VisitTracker path="/" />
|
|
<HomeClient
|
|
groups={groups}
|
|
routes={routes}
|
|
timesByRoute={timesByRoute}
|
|
provinces={provinces}
|
|
stops={stops}
|
|
routeStations={routeStations}
|
|
initialProvinceId={initialProvinceId}
|
|
initialStationCode={initialStationCode}
|
|
/>
|
|
>>>>>>> 1526c5b6d06989ba2d865f6df4d339d680dbd3f8
|
|
</>
|
|
);
|
|
}
|