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

133 lines
5.3 KiB
TypeScript

import Link from "next/link";
import { notFound } from "next/navigation";
import { api, isApiNotFoundError, resolveFileUrl, type Schedule, type StopDetail } from "@/lib/api";
import LazyMapView from "@/components/LazyMapView";
import { VisitTracker } from "@/lib/visit-tracker";
const TONES = ["sky", "purple", "pink", "mint", "amber"] as const;
type StopDetailsPageProps = {
searchParams: Promise<{ code?: string }>;
};
export default async function StopDetailsPage({ searchParams }: StopDetailsPageProps) {
const { code } = await searchParams;
if (!code) notFound();
let stop: StopDetail | null = null;
let schedules: Schedule[] = [];
try {
stop = await api.stop(code);
({ items: schedules } = await api.schedules({ stop_code: stop.code }));
} catch (error) {
if (isApiNotFoundError(error)) {
notFound();
}
stop = null;
schedules = [];
}
if (!stop) {
return (
<main className="page">
<VisitTracker path={`/stops/details?code=${code}`} />
<section className="hero">
<h1>ไม่สามารถโหลดข้อมูลป้ายนี้ได้</h1>
<p className="muted">ระบบยังเปิดอยู่ แต่ backend ตอบกลับไม่ได้ในตอนนี้</p>
<Link href="/stops">กลับไปหน้าป้ายหยุดรถ</Link>
</section>
</main>
);
}
const place = [stop.city_th, stop.province_th].filter(Boolean).join(" / ") || "—";
const hasMap = stop.lat != null && stop.lng != null;
const mapPoints = hasMap
? [{ id: stop.id, lat: stop.lat as number, lng: stop.lng as number, label: stop.name_th, code: stop.code }]
: [];
return (
<main className="page">
<VisitTracker path={`/stops/details?code=${stop.code}`} />
<section className="hero">
<span className="pill">{stop.station_type}</span>
<h1 style={{ marginTop: 10 }}>{stop.name_th}</h1>
<p className="muted">📍 {place}{hasMap ? ` · ${stop.lat?.toFixed(4)}, ${stop.lng?.toFixed(4)}` : ""}</p>
</section>
<div className="stop-detail__split">
{stop.images.length > 0 ? (
<div className="stop-detail__gallery">
<img
className="stop-detail__hero-img"
src={resolveFileUrl(stop.images[0].url_path)}
alt={(stop.images[0].meta as { alt_th?: string } | undefined)?.alt_th ?? stop.name_th}
/>
{stop.images.length > 1 && (
<div className="stop-detail__thumbs">
{stop.images.slice(1, 5).map((img) => (
<img key={img.id} src={resolveFileUrl(img.url_path)} alt={stop.name_th} />
))}
</div>
)}
</div>
) : (
<div className="stop-detail__gallery stop-detail__gallery--empty">
<div className="stop-detail__placeholder">📷 ยังไม่มีรูปป้ายนี้</div>
</div>
)}
{hasMap ? (
<div className="stop-detail__map">
<LazyMapView points={mapPoints} />
</div>
) : (
<div className="stop-detail__map stop-detail__map--empty">
<div className="stop-detail__placeholder">🗺️ ยังไม่มีพิกัดของป้ายนี้</div>
</div>
)}
</div>
<h2 className="section-title">ตารางเวลาที่จุดนี้</h2>
{schedules.length === 0 ? (
<p className="muted">ยังไม่มีข้อมูล</p>
) : (
<div className="schedule-list">
{schedules.map((s, i) => {
const tone = TONES[i % TONES.length];
return (
<article key={s.id} className={`schedule-card schedule-card--${tone}`}>
<div className="schedule-card__rail" />
<div className="schedule-card__head">
<Link
href={`/routes/${encodeURIComponent(s.route_id)}`}
className={`schedule-card__route ${tone}`}
>
<span className="schedule-card__route-label">เส้นทาง</span>
<span className="schedule-card__route-id">{s.route_id}</span>
</Link>
<div className="schedule-card__meta">
<span className="schedule-card__direction">
<span aria-hidden>🧭</span>
{s.direction_note || "ไม่ระบุทิศทาง"}
</span>
{s.schedule_note && (
<span className="schedule-card__note">📝 {s.schedule_note}</span>
)}
</div>
</div>
<div className="schedule-card__times">
{s.departure_times.length === 0 ? (
<span className="schedule-card__empty">— ยังไม่มีรอบ —</span>
) : (
s.departure_times.map((t, j) => (
<span key={j} className="schedule-card__time">🕒 {t}</span>
))
)}
</div>
</article>
);
})}
</div>
)}
</main>
);
}