feat(ui): full i18n (th/en) + highway stop search + real km list
i18n: - LangProvider (client context) + cookie/lang localStorage sync - locales/th.json + en.json จาก design (269 keys) - แปล header/nav/footer/home/route/stop/check ผ่าน t() - server pages อ่าน cookie lang (SSR แปลตรง) + client context Stop search (หน้า /stops): - code search 5 หลัก (kilometer '36+020' → pad5 '36020') + numpad - place search (ชื่อสถานที่) + suggest - near-me (geolocation → ป้ายทางหลวงใกล้สุด 8 อัน, haversine) - data: busstop_list.json (154 ป้ายทางหลวง) Route detail: km list ใช้ระยะจริงจาก stop-km.json (แทน stop_sequence) Note: 'nearby ใกล้เคียงป้าย' ไม่ทำ — stations.json ไม่มี landmarks data
This commit is contained in:
1 parent
0c3936df69
commit
1526c5b6d0
18 files changed
+3146
-141
No files matched your search
@@ -0,0 +1,177 @@
|
||||
"use client";
|
||||
|
||||
import { useMemo, useState } from "react";
|
||||
import { allBusStops, findBusStopByCode, nearestBusStops, type BusStop } from "@/lib/busstops";
|
||||
import { Icon } from "@/components/Icons";
|
||||
import { useLang } from "@/components/LangProvider";
|
||||
|
||||
type Mode = "code" | "place";
|
||||
|
||||
function BusStopCard({ stop, distM }: { stop: BusStop; distM?: number }) {
|
||||
const { lang } = useLang();
|
||||
return (
|
||||
<div className="busstop-result">
|
||||
<div className="busstop-result__head">
|
||||
<span className="busstop-result__km">{stop.kilometer}</span>
|
||||
<span className="busstop-result__name">{stop.route_name}</span>
|
||||
{distM != null && (
|
||||
<span className="busstop-result__dist">
|
||||
{distM < 1000 ? `${Math.round(distM)} m` : `${(distM / 1000).toFixed(1)} km`}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<p className="busstop-result__meta">
|
||||
{stop.position} · {stop.type}
|
||||
</p>
|
||||
{(stop["buses-line"] ?? []).length > 0 && (
|
||||
<div className="busstop-result__chips">
|
||||
{(stop["buses-line"] ?? []).map((r) => (
|
||||
<span key={r} className="chip-soft chip-soft--purple">{lang === "en" ? "Route" : "สาย"} {r}</span>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function StopSearch() {
|
||||
const { t, lang } = useLang();
|
||||
const [mode, setMode] = useState<Mode>("code");
|
||||
const [code, setCode] = useState("");
|
||||
const [query, setQuery] = useState("");
|
||||
const [nearby, setNearby] = useState<{ stop: BusStop; distM: number }[] | null>(null);
|
||||
const [geoErr, setGeoErr] = useState(false);
|
||||
|
||||
const pad = (d: string) => {
|
||||
if (code.length >= 5) return;
|
||||
setCode(code + d);
|
||||
setNearby(null);
|
||||
};
|
||||
const backspace = () => setCode(code.slice(0, -1));
|
||||
const clearAll = () => {
|
||||
setCode("");
|
||||
setQuery("");
|
||||
setNearby(null);
|
||||
setGeoErr(false);
|
||||
};
|
||||
|
||||
const codeResult = code.length === 5 ? findBusStopByCode(code) : undefined;
|
||||
|
||||
const placeResults = useMemo(() => {
|
||||
const q = query.trim();
|
||||
if (q.length < 2) return [];
|
||||
return allBusStops()
|
||||
.filter(
|
||||
(b) =>
|
||||
b.route_name.includes(q) || b.type.includes(q) || b.position.includes(q),
|
||||
)
|
||||
.slice(0, 20);
|
||||
}, [query]);
|
||||
|
||||
const nearMe = () => {
|
||||
if (typeof navigator === "undefined" || !navigator.geolocation) {
|
||||
setGeoErr(true);
|
||||
return;
|
||||
}
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
(pos) => setNearby(nearestBusStops(pos.coords.latitude, pos.coords.longitude, 8)),
|
||||
() => setGeoErr(true),
|
||||
{ timeout: 10000 },
|
||||
);
|
||||
};
|
||||
|
||||
const N = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
|
||||
|
||||
return (
|
||||
<div className="stop-search">
|
||||
<div className="stop-search__tabs" role="tablist" aria-label="รูปแบบการค้นหา">
|
||||
<button
|
||||
type="button"
|
||||
className={`stop-search__tab ${mode === "code" ? "on" : ""}`}
|
||||
onClick={() => setMode("code")}
|
||||
>
|
||||
<Icon name="map-pin" size={14} className="icon" /> {t("stops.modeCode")}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className={`stop-search__tab ${mode === "place" ? "on" : ""}`}
|
||||
onClick={() => setMode("place")}
|
||||
>
|
||||
<Icon name="search" size={14} className="icon" /> {t("stops.modePlace")}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{mode === "code" ? (
|
||||
<div className="stop-search__panel">
|
||||
<div className="code-boxes" aria-label={t("stops.codePh")}>
|
||||
{[0, 1, 2, 3, 4].map((i) => (
|
||||
<span key={i} className={`code-cell ${i < code.length ? "filled" : ""}`}>
|
||||
{code[i] ?? ""}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
<div className="numpad">
|
||||
{N.map((n) => (
|
||||
<button key={n} type="button" onClick={() => pad(String(n))}>
|
||||
{n}
|
||||
</button>
|
||||
))}
|
||||
<button type="button" onClick={backspace} aria-label={t("stops.padDelete")}>
|
||||
<Icon name="arrow-right" size={16} className="icon" style={{ transform: "rotate(180deg)" }} />
|
||||
</button>
|
||||
</div>
|
||||
<div className="stop-search__actions">
|
||||
<button type="button" className="mu-btn ghost" onClick={clearAll}>
|
||||
{t("stops.reset")}
|
||||
</button>
|
||||
<button type="button" className="mu-btn ghost" onClick={nearMe}>
|
||||
<Icon name="map-pin" size={14} className="icon" /> {t("map.useMyLocation")}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="stop-search__panel">
|
||||
<div className="stop-search__field">
|
||||
<Icon name="search" size={16} className="icon" />
|
||||
<input
|
||||
type="search"
|
||||
value={query}
|
||||
onChange={(e) => setQuery(e.target.value)}
|
||||
placeholder={t("stops.searchPh")}
|
||||
/>
|
||||
</div>
|
||||
{placeResults.length > 0 && (
|
||||
<ul className="stop-search__suggest">
|
||||
{placeResults.map((b, i) => (
|
||||
<li key={`${b.kilometer}-${i}`}>
|
||||
<button type="button" onClick={() => setCode(b.kilometer.replace("+", ""))}>
|
||||
<span className="km">{b.kilometer}</span>
|
||||
<span className="name">{b.route_name}</span>
|
||||
<span className="meta">{b.position}</span>
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{geoErr && (
|
||||
<p className="stop-search__err">
|
||||
{lang === "en" ? "Cannot access your location." : "ไม่สามารถเข้าถึงตำแหน่งของคุณได้"}
|
||||
</p>
|
||||
)}
|
||||
|
||||
{codeResult && <BusStopCard stop={codeResult} />}
|
||||
|
||||
{nearby && nearby.length > 0 && (
|
||||
<div className="stop-search__nearby">
|
||||
<h3>{t("map.nearestTitle")}</h3>
|
||||
{nearby.map((n, i) => (
|
||||
<BusStopCard key={`${n.stop.kilometer}-${i}`} stop={n.stop} distM={n.distM} />
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user