Files
web-at-transit/frontend/src/app/page.tsx
T
2026-05-24 22:55:17 +07:00

128 lines
4.8 KiB
TypeScript

import Link from "next/link";
import { api, type RouteListItem } from "@/lib/api";
import { VisitTracker } from "@/lib/visit-tracker";
// Map route_group_code to visual tone
const ZONE_TONES: Record<string, "sky" | "purple" | "pink" | "mint"> = {
bangkok: "sky",
interprovincial: "purple",
in_province: "pink",
municipal: "mint",
};
const ZONE_DESCS: Record<string, string> = {
bangkok: "เส้นทางเชื่อมต่อกรุงเทพมหานคร",
interprovincial: "เส้นทางเชื่อมต่อจังหวัดใกล้เคียง",
in_province: "เส้นทางภายในจังหวัดอ่างทอง",
municipal: "เส้นทางภายในเขตเทศบาล",
};
// Display order: bangkok → interprovincial → in_province → municipal
const ZONE_ORDER = ["bangkok", "interprovincial", "in_province", "municipal"];
// Cycle chip colors within a zone for visual rhythm
const CHIP_CYCLE = ["purple", "sky", "pink", "mint"] as const;
export default async function HomePage() {
const [groupsRes, routesRes] = await Promise.allSettled([
api.routeGroups(),
api.routes(),
]);
const groups = groupsRes.status === "fulfilled" ? groupsRes.value.items : [];
const routes = routesRes.status === "fulfilled" ? routesRes.value.items : [];
// Group routes by code
const byCode = new Map<string, RouteListItem[]>();
for (const r of routes) {
const list = byCode.get(r.route_group_code) ?? [];
list.push(r);
byCode.set(r.route_group_code, list);
}
const orderedGroups = [...groups].sort((a, b) => {
const ai = ZONE_ORDER.indexOf(a.code);
const bi = ZONE_ORDER.indexOf(b.code);
return (ai === -1 ? 99 : ai) - (bi === -1 ? 99 : bi);
});
return (
<>
<VisitTracker path="/" />
<section className="hero">
<div className="hero__blob one" aria-hidden />
<div className="hero__blob two" aria-hidden />
<div className="hero__blob three" aria-hidden />
<div className="container hero__inner">
<span className="hero__badge">✨ ยินดีต้อนรับ</span>
<h1>
สถานีขนส่งผู้โดยสารภายใน
<br />
<span className="hero__grad">จังหวัดอ่างทอง</span>
</h1>
<p className="hero__lead">
ดูสายรถ, ค้นหาป้าย และเปิดตัวอย่างเส้นทางได้ทันทีจากหน้าแรก
</p>
<div className="hero__actions">
<Link className="pill-link pill-link--primary" href="/routes">
ดูรายละเอียดสายรถ
</Link>
<Link className="pill-link pill-link--secondary" href="/stops">
ค้นหาป้ายหยุดรถ
</Link>
{routes[0] && (
<Link
className="pill-link pill-link--accent"
href={`/routes/${encodeURIComponent(routes[0].route_id)}`}
>
ดูตัวอย่างสายรถ
</Link>
)}
</div>
</div>
</section>
<main className="main">
<div className="container">
{orderedGroups.map((g) => {
const items = byCode.get(g.code) ?? [];
if (items.length === 0) return null;
const tone = ZONE_TONES[g.code] ?? "purple";
return (
<section key={g.id} className="zone">
<div className="zone__head">
<span className={`zone__tag ${tone}`}>{g.label_th}</span>
<p className="zone__desc">
{ZONE_DESCS[g.code] ?? ""} · {items.length} เส้นทาง
</p>
</div>
<div className="grid">
{items.map((r, i) => (
<Link
key={r.route_id}
href={`/routes/${encodeURIComponent(r.route_id)}`}
className="card"
>
<div className={`card__head ${tone}`}>
<p className="card__num">{r.route_id}</p>
</div>
<div className="card__body">
<p className="card__name">{r.name_th}</p>
<span className={`chip ${CHIP_CYCLE[i % CHIP_CYCLE.length]}`}>
✅ ดูตาราง
</span>
</div>
</Link>
))}
</div>
</section>
);
})}
</div>
</main>
</>
);
}