|
| 1 | +import { useMemo, useState } from "react"; |
| 2 | +import { countryFlag } from "../../lib/format/format"; |
| 3 | +import { LAND_OUTLINE } from "../../lib/publish/landOutline"; |
| 4 | +import { useT } from "../../lib/i18n"; |
| 5 | +import type { MyPlace } from "./myPlaces"; |
| 6 | + |
| 7 | +// Pick trip stops from the places you've BEEN — a tappable list, or a lightweight |
| 8 | +// offline SVG map of your places (no MapLibre/tiles). One tap adds a stop. Built for |
| 9 | +// speed: flags for instant recognition, no gazetteer typing (spec 019). |
| 10 | + |
| 11 | +const W = 720; |
| 12 | +const H = 380; |
| 13 | +const PAD = 34; |
| 14 | +const MIN_SPAN = 0.35; |
| 15 | +const mercY = (lat: number) => Math.log(Math.tan(Math.PI / 4 + (Math.max(-85, Math.min(85, lat)) * Math.PI) / 360)); |
| 16 | + |
| 17 | +export function MyPlacesPicker({ |
| 18 | + places, |
| 19 | + addedKeys, |
| 20 | + onPick, |
| 21 | +}: { |
| 22 | + places: MyPlace[]; |
| 23 | + addedKeys: Set<string>; |
| 24 | + onPick: (p: MyPlace) => void; |
| 25 | +}) { |
| 26 | + const t = useT(); |
| 27 | + const [mode, setMode] = useState<"list" | "map">("list"); |
| 28 | + const [q, setQ] = useState(""); |
| 29 | + |
| 30 | + const filtered = useMemo(() => { |
| 31 | + const s = q.trim().toLowerCase(); |
| 32 | + return s ? places.filter((p) => p.name.toLowerCase().includes(s)) : places; |
| 33 | + }, [places, q]); |
| 34 | + |
| 35 | + if (places.length === 0) { |
| 36 | + return <p className="muted empty">{t("trip.compose.noPlaces")}</p>; |
| 37 | + } |
| 38 | + |
| 39 | + return ( |
| 40 | + <div className="myplaces-picker"> |
| 41 | + <div className="segmented" role="group" aria-label={t("trip.compose.pickModeAria")}> |
| 42 | + <button |
| 43 | + type="button" |
| 44 | + aria-pressed={mode === "list"} |
| 45 | + className={mode === "list" ? "seg-on" : ""} |
| 46 | + onClick={() => setMode("list")} |
| 47 | + > |
| 48 | + ☰ {t("trip.compose.pickList")} |
| 49 | + </button> |
| 50 | + <button |
| 51 | + type="button" |
| 52 | + aria-pressed={mode === "map"} |
| 53 | + className={mode === "map" ? "seg-on" : ""} |
| 54 | + onClick={() => setMode("map")} |
| 55 | + > |
| 56 | + 🗺 {t("trip.compose.pickMap")} |
| 57 | + </button> |
| 58 | + </div> |
| 59 | + |
| 60 | + {mode === "list" ? ( |
| 61 | + <> |
| 62 | + <input |
| 63 | + type="search" |
| 64 | + className="search-input" |
| 65 | + value={q} |
| 66 | + placeholder={t("trip.compose.filterPlaces")} |
| 67 | + aria-label={t("trip.compose.filterPlaces")} |
| 68 | + onChange={(e) => setQ(e.target.value)} |
| 69 | + /> |
| 70 | + <ul className="myplaces-list"> |
| 71 | + {filtered.map((p) => ( |
| 72 | + <li key={p.key}> |
| 73 | + <button |
| 74 | + type="button" |
| 75 | + className="myplaces-pick" |
| 76 | + aria-label={t("trip.compose.pickAria", { name: p.name })} |
| 77 | + onClick={() => onPick(p)} |
| 78 | + > |
| 79 | + <span className="flag" aria-hidden> |
| 80 | + {p.place.kind === "airport" ? "✈️" : countryFlag(p.countryId)} |
| 81 | + </span> |
| 82 | + <span className="myplaces-name">{p.name}</span> |
| 83 | + {addedKeys.has(p.key) && ( |
| 84 | + <span className="myplaces-added" aria-hidden> |
| 85 | + ✓ |
| 86 | + </span> |
| 87 | + )} |
| 88 | + </button> |
| 89 | + </li> |
| 90 | + ))} |
| 91 | + </ul> |
| 92 | + </> |
| 93 | + ) : ( |
| 94 | + <PickMap places={places} addedKeys={addedKeys} onPick={onPick} /> |
| 95 | + )} |
| 96 | + </div> |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +/** The offline SVG map: your places as pins, tap to add. Pins are decorative for |
| 101 | + * assistive tech; the legend list below is the keyboard/AT path (WCAG). */ |
| 102 | +function PickMap({ |
| 103 | + places, |
| 104 | + addedKeys, |
| 105 | + onPick, |
| 106 | +}: { |
| 107 | + places: MyPlace[]; |
| 108 | + addedKeys: Set<string>; |
| 109 | + onPick: (p: MyPlace) => void; |
| 110 | +}) { |
| 111 | + const t = useT(); |
| 112 | + const layout = useMemo(() => { |
| 113 | + const X = places.map((p) => (p.lon * Math.PI) / 180); |
| 114 | + const Y = places.map((p) => mercY(p.lat)); |
| 115 | + let minX = Math.min(...X); |
| 116 | + let maxX = Math.max(...X); |
| 117 | + let minY = Math.min(...Y); |
| 118 | + let maxY = Math.max(...Y); |
| 119 | + if (maxX - minX < MIN_SPAN) { |
| 120 | + const c = (minX + maxX) / 2; |
| 121 | + minX = c - MIN_SPAN / 2; |
| 122 | + maxX = c + MIN_SPAN / 2; |
| 123 | + } |
| 124 | + if (maxY - minY < MIN_SPAN) { |
| 125 | + const c = (minY + maxY) / 2; |
| 126 | + minY = c - MIN_SPAN / 2; |
| 127 | + maxY = c + MIN_SPAN / 2; |
| 128 | + } |
| 129 | + let spanX = maxX - minX; |
| 130 | + let spanY = maxY - minY; |
| 131 | + minX -= spanX * 0.16; |
| 132 | + maxX += spanX * 0.16; |
| 133 | + minY -= spanY * 0.2; |
| 134 | + maxY += spanY * 0.2; |
| 135 | + spanX = maxX - minX; |
| 136 | + spanY = maxY - minY; |
| 137 | + const scale = Math.min((W - 2 * PAD) / spanX, (H - 2 * PAD) / spanY); |
| 138 | + const midX = (minX + maxX) / 2; |
| 139 | + const midY = (minY + maxY) / 2; |
| 140 | + const sx = (x: number) => W / 2 + (x - midX) * scale; |
| 141 | + const sy = (y: number) => H / 2 - (y - midY) * scale; |
| 142 | + let land = ""; |
| 143 | + for (const ring of LAND_OUTLINE) { |
| 144 | + for (const off of [-360, 0, 360]) { |
| 145 | + let seg = ""; |
| 146 | + let any = false; |
| 147 | + let unwrapped = 0; |
| 148 | + let prevRaw: number | null = null; |
| 149 | + for (let i = 0; i < ring.length; i++) { |
| 150 | + const llon = ring[i]![0]; |
| 151 | + const llat = ring[i]![1]; |
| 152 | + if (prevRaw === null) unwrapped = llon + off; |
| 153 | + else { |
| 154 | + let d = llon - prevRaw; |
| 155 | + if (d > 180) d -= 360; |
| 156 | + else if (d < -180) d += 360; |
| 157 | + unwrapped += d; |
| 158 | + } |
| 159 | + prevRaw = llon; |
| 160 | + const Lx = sx((unwrapped * Math.PI) / 180); |
| 161 | + const Ly = sy(mercY(llat)); |
| 162 | + seg += (i === 0 ? "M" : "L") + Lx.toFixed(1) + " " + Ly.toFixed(1); |
| 163 | + if (Lx > -60 && Lx < W + 60 && Ly > -60 && Ly < H + 60) any = true; |
| 164 | + } |
| 165 | + if (any) land += seg + "Z"; |
| 166 | + } |
| 167 | + } |
| 168 | + const dots = places.map((p) => ({ ...p, x: sx((p.lon * Math.PI) / 180), y: sy(mercY(p.lat)) })); |
| 169 | + return { land, dots }; |
| 170 | + }, [places]); |
| 171 | + |
| 172 | + return ( |
| 173 | + <div className="myplaces-map"> |
| 174 | + <svg className="storymap-svg" viewBox={`0 0 ${W} ${H}`} role="img" aria-label={t("trip.compose.mapAria")}> |
| 175 | + {layout.land && <path className="storymap-land" d={layout.land} />} |
| 176 | + {layout.dots.map((p) => ( |
| 177 | + <g key={p.key} className="storymap-pin" aria-hidden onClick={() => onPick(p)}> |
| 178 | + <circle cx={p.x} cy={p.y} r={addedKeys.has(p.key) ? 8 : 6} className="storymap-pin-dot" /> |
| 179 | + </g> |
| 180 | + ))} |
| 181 | + </svg> |
| 182 | + <ul className="storymap-legend myplaces-legend"> |
| 183 | + {layout.dots.map((p) => ( |
| 184 | + <li key={p.key}> |
| 185 | + <button |
| 186 | + type="button" |
| 187 | + className="link" |
| 188 | + aria-label={t("trip.compose.pickAria", { name: p.name })} |
| 189 | + onClick={() => onPick(p)} |
| 190 | + > |
| 191 | + {p.place.kind === "airport" ? "✈️" : countryFlag(p.countryId)} {p.name} |
| 192 | + {addedKeys.has(p.key) && ( |
| 193 | + <span className="myplaces-added" aria-hidden> |
| 194 | + {" "} |
| 195 | + ✓ |
| 196 | + </span> |
| 197 | + )} |
| 198 | + </button> |
| 199 | + </li> |
| 200 | + ))} |
| 201 | + </ul> |
| 202 | + </div> |
| 203 | + ); |
| 204 | +} |
0 commit comments