From d7401b63cdddbdb736f2ee1dc33ab7a4d615b271 Mon Sep 17 00:00:00 2001 From: Claudio Schaad Date: Fri, 6 Nov 2026 20:50:55 +0100 Subject: [PATCH] Add start page with tiles for the available maps The app will show several maps and datasets, so it now opens on a start page with one large tile per map instead of the traffic map. - Routing with react-router-dom: "/" is the start page, the Thurgau traffic map moved to "/verkehr/thurgau" (pages/TrafficThurgauPage) - Map registry in src/maps.ts drives the tiles (title, region, description, tags, source, preview image) - SVG preview illustration for the traffic map tile - Traffic map header links back to the start page; page titles set per page Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 1 + client/index.html | 2 +- client/package-lock.json | 41 ++++- client/package.json | 3 +- client/public/previews/traffic-thurgau.svg | 74 ++++++++ client/src/App.tsx | 177 +------------------ client/src/main.tsx | 5 +- client/src/maps.ts | 22 +++ client/src/pages/HomePage.tsx | 47 ++++++ client/src/pages/TrafficThurgauPage.tsx | 187 +++++++++++++++++++++ client/src/styles.css | 162 ++++++++++++++++++ 11 files changed, 548 insertions(+), 173 deletions(-) create mode 100644 client/public/previews/traffic-thurgau.svg create mode 100644 client/src/maps.ts create mode 100644 client/src/pages/HomePage.tsx create mode 100644 client/src/pages/TrafficThurgauPage.tsx diff --git a/CLAUDE.md b/CLAUDE.md index bcecc55..2b14bc9 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -66,6 +66,7 @@ When the geometry algorithm changes, bump `GeometryFileName` in `TrafficCacheSto ### Client +- Routing with `react-router-dom`: `/` is the start page (`pages/HomePage.tsx`) with one large tile per map, each map has its own page (`/verkehr/thurgau` → `pages/TrafficThurgauPage.tsx`). Tiles come from the registry in `src/maps.ts`; a new map needs an entry there (with a preview image in `public/previews/`) and a route in `App.tsx`. Deep links work because the server falls back to `index.html`. - `traffic/flowAnimator.ts` owns the animation clock (time of day in hours, play/pause, speed) and draws on a canvas above Leaflet's overlay pane. Dots spawn per direction at a rate proportional to the interpolated hourly count, colored and sized by vehicle group, offset to the right of the driving direction. It reports the time to React at ~10 Hz via `onTick`; React components only get the integer hour, so markers and charts don't re-render every frame. - The vehicle class order in `traffic/vehicleClasses.ts` must match the `VehicleClass` enum / `VehicleCounts.ToArray()` order; count arrays in the API are positional. - UI language is German (de-CH). Base maps are swisstopo WMTS layers (grey `pixelkarte-grau` as default, colour `pixelkarte-farbe`, aerial `swissimage`), switchable via Leaflet's layer control; the choice is stored in localStorage (`odv.baseMap`). diff --git a/client/index.html b/client/index.html index 60386db..1f0b0e4 100644 --- a/client/index.html +++ b/client/index.html @@ -4,7 +4,7 @@ - OpenDataVisualizer – Verkehr Thurgau + OpenDataVisualizer
diff --git a/client/package-lock.json b/client/package-lock.json index cfe44a9..e2ea090 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -13,7 +13,8 @@ "leaflet": "^1.9.4", "react": "^18.3.1", "react-chartjs-2": "^5.2.0", - "react-dom": "^18.3.1" + "react-dom": "^18.3.1", + "react-router-dom": "^6.30.6" }, "devDependencies": { "@types/leaflet": "^1.9.12", @@ -754,6 +755,14 @@ "resolved": "https://registry.npmjs.org/@kurkle/color/-/color-0.3.4.tgz", "integrity": "sha512-M5UknZPHRu3DEDWoipU6sE8PdkZ6Z/S+v4dD+Ke8IaNlpdSQah50lz1KtcFBa2vsdOnwbbnxJwVM4wty6udA5w==" }, + "node_modules/@remix-run/router": { + "version": "1.23.4", + "resolved": "https://registry.npmjs.org/@remix-run/router/-/router-1.23.4.tgz", + "integrity": "sha512-q7j5geK7xs3UJSdm9/iytUNclBnLmYx1EnSeCFXHPeutdqgIMeFeHtUZgS3EhlKxdBEAu8OwtJCwmLrEzpSs7Q==", + "engines": { + "node": ">=14.0.0" + } + }, "node_modules/@rolldown/pluginutils": { "version": "1.0.0-beta.27", "resolved": "https://registry.npmjs.org/@rolldown/pluginutils/-/pluginutils-1.0.0-beta.27.tgz", @@ -1625,6 +1634,36 @@ "node": ">=0.10.0" } }, + "node_modules/react-router": { + "version": "6.30.6", + "resolved": "https://registry.npmjs.org/react-router/-/react-router-6.30.6.tgz", + "integrity": "sha512-5HfK7k5im7LTOB0EqCQmfvy4C13G92Ssj1VTmouTK3AJvyjKTnFuCV0vcMAD/JS+JC4DvDIBRrlAeJIFjh5VWg==", + "dependencies": { + "@remix-run/router": "1.23.4" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "react": ">=16.8" + } + }, + "node_modules/react-router-dom": { + "version": "6.30.6", + "resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-6.30.6.tgz", + "integrity": "sha512-0RHKZz7wwffvkU+2MFVT2NnjK44ssLEV+m0CAJaS2Ksmorrwj7WxH00jO0SOCW26/tINUnJHToXblDs33I38YQ==", + "dependencies": { + "@remix-run/router": "1.23.4", + "react-router": "6.30.6" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "react": ">=16.8", + "react-dom": ">=16.8" + } + }, "node_modules/rollup": { "version": "4.68.0", "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.68.0.tgz", diff --git a/client/package.json b/client/package.json index 535c0ca..debc024 100644 --- a/client/package.json +++ b/client/package.json @@ -16,7 +16,8 @@ "leaflet": "^1.9.4", "react": "^18.3.1", "react-chartjs-2": "^5.2.0", - "react-dom": "^18.3.1" + "react-dom": "^18.3.1", + "react-router-dom": "^6.30.6" }, "devDependencies": { "@types/leaflet": "^1.9.12", diff --git a/client/public/previews/traffic-thurgau.svg b/client/public/previews/traffic-thurgau.svg new file mode 100644 index 0000000..24234f3 --- /dev/null +++ b/client/public/previews/traffic-thurgau.svg @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/client/src/App.tsx b/client/src/App.tsx index 8778ee7..49a556f 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -1,174 +1,13 @@ -import { keepPreviousData, useQuery } from '@tanstack/react-query'; -import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'; -import { fetchDay, fetchMeta, fetchStations } from './api/traffic'; -import { Legend } from './components/Legend'; -import { StationPanel } from './components/StationPanel'; -import { SPEEDS, TimeControls } from './components/TimeControls'; -import { TrafficMap } from './components/TrafficMap'; -import type { FlowAnimator } from './traffic/flowAnimator'; -import { formatNumber } from './traffic/format'; -import { sumCounts } from './traffic/vehicleClasses'; - -const MemoStationPanel = memo(StationPanel); - -const INITIAL_TIME = 7.5; +import { Navigate, Route, Routes } from 'react-router-dom'; +import { HomePage } from './pages/HomePage'; +import { TrafficThurgauPage } from './pages/TrafficThurgauPage'; export function App() { - const metaQuery = useQuery({ - queryKey: ['meta'], - queryFn: fetchMeta, - refetchInterval: 10 * 60_000, - }); - const stationsQuery = useQuery({ - queryKey: ['stations'], - queryFn: fetchStations, - refetchInterval: (query) => - query.state.data?.some((s) => s.directions.some((d) => d.path.length < 2)) ? 30_000 : false, - }); - - const dates = useMemo(() => metaQuery.data?.dates ?? [], [metaQuery.data]); - const [date, setDate] = useState(null); - const [time, setTime] = useState(INITIAL_TIME); - const [playing, setPlaying] = useState(false); - const [hoursPerSecond, setHoursPerSecond] = useState(SPEEDS[1].hoursPerSecond); - const [selectedCode, setSelectedCode] = useState(null); - const animatorRef = useRef(null); - const datesRef = useRef(dates); - datesRef.current = dates; - - useEffect(() => { - if (!date && dates.length > 0) { - setDate(dates[dates.length - 1]); - } - }, [date, dates]); - - const dayQuery = useQuery({ - queryKey: ['day', date], - queryFn: () => fetchDay(date!), - enabled: !!date, - placeholderData: keepPreviousData, - staleTime: 5 * 60_000, - }); - const day = dayQuery.data ?? null; - - const handleAnimatorReady = useCallback((animator: FlowAnimator) => { - animatorRef.current = animator; - animator.time = INITIAL_TIME; - animator.onTick = setTime; - animator.onDayEnd = () => - setDate((current) => { - const all = datesRef.current; - const index = current ? all.indexOf(current) : -1; - return all.length === 0 ? current : all[(index + 1) % all.length]; - }); - }, []); - - useEffect(() => { - if (animatorRef.current) { - animatorRef.current.playing = playing; - } - }, [playing]); - - useEffect(() => { - if (animatorRef.current) { - animatorRef.current.hoursPerSecond = hoursPerSecond; - } - }, [hoursPerSecond]); - - const handleTimeChange = useCallback((value: number) => { - if (animatorRef.current) { - animatorRef.current.time = value; - } - setTime(value); - }, []); - - const handleClose = useCallback(() => setSelectedCode(null), []); - - const hour = Math.min(23, Math.floor(time)); - const stations = useMemo(() => stationsQuery.data ?? [], [stationsQuery.data]); - const selectedStation = stations.find((s) => s.code === selectedCode) ?? null; - - const hourTotal = useMemo( - () => - day?.stations.reduce( - (sum, station) => - sum + station.directions.reduce((s, d) => s + sumCounts(d.hours[hour]), 0), - 0, - ) ?? 0, - [day, hour], - ); - - const error = metaQuery.error ?? stationsQuery.error ?? dayQuery.error; - return ( -
-
-
-

OpenDataVisualizer

- - Strassenverkehr Kanton Thurgau · stündliche Zählungen nach Fahrzeugklassen - -
-
-
- {stations.length} - Zählstellen -
-
- {formatNumber(hourTotal)} - Fahrzeuge in dieser Stunde -
-
-
- -
- - -
- {date && ( - - )} - -
- - {(metaQuery.isLoading || (metaQuery.data && dates.length === 0)) && ( -
- Verkehrsdaten werden geladen… (erster Start lädt die Daten von data.tg.ch) -
- )} - {error &&
Fehler beim Laden: {String(error.message)}
} -
- - {selectedStation && ( - - )} - - -
+ + } /> + } /> + } /> + ); } diff --git a/client/src/main.tsx b/client/src/main.tsx index eb39b90..282a07e 100644 --- a/client/src/main.tsx +++ b/client/src/main.tsx @@ -1,4 +1,5 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { BrowserRouter } from 'react-router-dom'; import { StrictMode } from 'react'; import { createRoot } from 'react-dom/client'; import { App } from './App'; @@ -11,7 +12,9 @@ const queryClient = new QueryClient({ createRoot(document.getElementById('root')!).render( - + + + , ); diff --git a/client/src/maps.ts b/client/src/maps.ts new file mode 100644 index 0000000..c74d403 --- /dev/null +++ b/client/src/maps.ts @@ -0,0 +1,22 @@ +export interface MapEntry { + path: string; + title: string; + region: string; + description: string; + source: string; + tags: string[]; + preview: string; +} + +export const MAPS: MapEntry[] = [ + { + path: '/verkehr/thurgau', + title: 'Strassenverkehr', + region: 'Kanton Thurgau', + description: + 'Fahrzeuge fahren animiert über die Strassen der 64 Zählstellen – stündlich nach Fahrzeugklasse, mit Tagesverlauf und Auswertungen pro Zählstelle.', + source: 'Tiefbauamt Kanton Thurgau', + tags: ['Verkehr', 'Stundenwerte', 'Animation'], + preview: '/previews/traffic-thurgau.svg', + }, +]; diff --git a/client/src/pages/HomePage.tsx b/client/src/pages/HomePage.tsx new file mode 100644 index 0000000..223e80f --- /dev/null +++ b/client/src/pages/HomePage.tsx @@ -0,0 +1,47 @@ +import { useEffect } from 'react'; +import { Link } from 'react-router-dom'; +import { MAPS } from '../maps'; + +export function HomePage() { + useEffect(() => { + document.title = 'OpenDataVisualizer'; + }, []); + + return ( +
+
+

OpenDataVisualizer

+

+ Karten und Visualisierungen zu offenen Daten der Schweiz von{' '} + + opendata.swiss + + . +

+
+ +
+ {MAPS.map((map) => ( + +
+ +
+
+ {map.region} +

{map.title}

+

{map.description}

+
+ {map.tags.map((tag) => ( + + {tag} + + ))} +
+ Quelle: {map.source} +
+ + ))} +
+
+ ); +} diff --git a/client/src/pages/TrafficThurgauPage.tsx b/client/src/pages/TrafficThurgauPage.tsx new file mode 100644 index 0000000..d0b8eb0 --- /dev/null +++ b/client/src/pages/TrafficThurgauPage.tsx @@ -0,0 +1,187 @@ +import { keepPreviousData, useQuery } from '@tanstack/react-query'; +import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import { Link } from 'react-router-dom'; +import { fetchDay, fetchMeta, fetchStations } from '../api/traffic'; +import { Legend } from '../components/Legend'; +import { StationPanel } from '../components/StationPanel'; +import { SPEEDS, TimeControls } from '../components/TimeControls'; +import { TrafficMap } from '../components/TrafficMap'; +import type { FlowAnimator } from '../traffic/flowAnimator'; +import { formatNumber } from '../traffic/format'; +import { sumCounts } from '../traffic/vehicleClasses'; + +const MemoStationPanel = memo(StationPanel); + +const INITIAL_TIME = 7.5; + +export function TrafficThurgauPage() { + const metaQuery = useQuery({ + queryKey: ['meta'], + queryFn: fetchMeta, + refetchInterval: 10 * 60_000, + }); + const stationsQuery = useQuery({ + queryKey: ['stations'], + queryFn: fetchStations, + refetchInterval: (query) => + query.state.data?.some((s) => s.directions.some((d) => d.path.length < 2)) ? 30_000 : false, + }); + + const dates = useMemo(() => metaQuery.data?.dates ?? [], [metaQuery.data]); + const [date, setDate] = useState(null); + const [time, setTime] = useState(INITIAL_TIME); + const [playing, setPlaying] = useState(false); + const [hoursPerSecond, setHoursPerSecond] = useState(SPEEDS[1].hoursPerSecond); + const [selectedCode, setSelectedCode] = useState(null); + const animatorRef = useRef(null); + useEffect(() => { + document.title = 'Strassenverkehr Thurgau – OpenDataVisualizer'; + }, []); + + const datesRef = useRef(dates); + datesRef.current = dates; + + useEffect(() => { + if (!date && dates.length > 0) { + setDate(dates[dates.length - 1]); + } + }, [date, dates]); + + const dayQuery = useQuery({ + queryKey: ['day', date], + queryFn: () => fetchDay(date!), + enabled: !!date, + placeholderData: keepPreviousData, + staleTime: 5 * 60_000, + }); + const day = dayQuery.data ?? null; + + const handleAnimatorReady = useCallback((animator: FlowAnimator) => { + animatorRef.current = animator; + animator.time = INITIAL_TIME; + animator.onTick = setTime; + animator.onDayEnd = () => + setDate((current) => { + const all = datesRef.current; + const index = current ? all.indexOf(current) : -1; + return all.length === 0 ? current : all[(index + 1) % all.length]; + }); + }, []); + + useEffect(() => { + if (animatorRef.current) { + animatorRef.current.playing = playing; + } + }, [playing]); + + useEffect(() => { + if (animatorRef.current) { + animatorRef.current.hoursPerSecond = hoursPerSecond; + } + }, [hoursPerSecond]); + + const handleTimeChange = useCallback((value: number) => { + if (animatorRef.current) { + animatorRef.current.time = value; + } + setTime(value); + }, []); + + const handleClose = useCallback(() => setSelectedCode(null), []); + + const hour = Math.min(23, Math.floor(time)); + const stations = useMemo(() => stationsQuery.data ?? [], [stationsQuery.data]); + const selectedStation = stations.find((s) => s.code === selectedCode) ?? null; + + const hourTotal = useMemo( + () => + day?.stations.reduce( + (sum, station) => + sum + station.directions.reduce((s, d) => s + sumCounts(d.hours[hour]), 0), + 0, + ) ?? 0, + [day, hour], + ); + + const error = metaQuery.error ?? stationsQuery.error ?? dayQuery.error; + + return ( +
+
+
+ + ← + +
+

+ + OpenDataVisualizer + {' '} + / Strassenverkehr Thurgau +

+ Stündliche Zählungen nach Fahrzeugklassen +
+
+
+
+ {stations.length} + Zählstellen +
+
+ {formatNumber(hourTotal)} + Fahrzeuge in dieser Stunde +
+
+
+ +
+ + +
+ {date && ( + + )} + +
+ + {(metaQuery.isLoading || (metaQuery.data && dates.length === 0)) && ( +
+ Verkehrsdaten werden geladen… (erster Start lädt die Daten von data.tg.ch) +
+ )} + {error &&
Fehler beim Laden: {String(error.message)}
} +
+ + {selectedStation && ( + + )} + + +
+ ); +} diff --git a/client/src/styles.css b/client/src/styles.css index 099ddbe..129dd19 100644 --- a/client/src/styles.css +++ b/client/src/styles.css @@ -358,3 +358,165 @@ select { flex-wrap: wrap; } } + +.title-row { + display: flex; + align-items: center; + gap: 12px; +} + +.back-link { + display: inline-flex; + align-items: center; + justify-content: center; + width: 32px; + height: 32px; + border-radius: 8px; + border: 1px solid var(--border); + color: var(--text); + text-decoration: none; + font-size: 16px; +} + +.back-link:hover { + background: var(--bg); +} + +.brand-link { + color: inherit; + text-decoration: none; +} + +.brand-link:hover { + color: var(--accent); +} + +.title-separator { + color: var(--muted); + font-weight: 400; +} + +.home { + height: 100%; + overflow-y: auto; + padding: 48px 32px 64px; +} + +.home-header { + max-width: 1200px; + margin: 0 auto 32px; +} + +.home-header h1 { + margin: 0 0 6px; + font-size: 32px; + letter-spacing: -0.02em; +} + +.home-header p { + margin: 0; + color: var(--muted); + font-size: 16px; +} + +.home-header a { + color: var(--accent); +} + +.map-tiles { + max-width: 1200px; + margin: 0 auto; + display: grid; + grid-template-columns: repeat(auto-fill, minmax(min(100%, 480px), 1fr)); + gap: 24px; +} + +.map-tile { + display: flex; + flex-direction: column; + background: var(--surface); + border: 1px solid var(--border); + border-radius: 16px; + overflow: hidden; + color: inherit; + text-decoration: none; + box-shadow: 0 1px 3px rgba(15, 23, 42, 0.06); + transition: + transform 0.15s, + box-shadow 0.15s, + border-color 0.15s; +} + +.map-tile:hover, +.map-tile:focus-visible { + transform: translateY(-3px); + border-color: #bfdbfe; + box-shadow: 0 12px 32px rgba(15, 23, 42, 0.12); + outline: none; +} + +.map-tile-preview { + aspect-ratio: 16 / 9; + background: #e3e8e1; + border-bottom: 1px solid var(--border); +} + +.map-tile-preview img { + display: block; + width: 100%; + height: 100%; + object-fit: cover; +} + +.map-tile-body { + display: flex; + flex-direction: column; + gap: 8px; + padding: 20px 22px 22px; +} + +.map-tile-region { + font-size: 12px; + font-weight: 600; + text-transform: uppercase; + letter-spacing: 0.06em; + color: var(--accent); +} + +.map-tile h2 { + margin: 0; + font-size: 24px; + letter-spacing: -0.01em; +} + +.map-tile p { + margin: 0; + color: #334155; + line-height: 1.5; +} + +.map-tile-tags { + display: flex; + flex-wrap: wrap; + gap: 6px; + margin-top: 4px; +} + +.tag { + font-size: 12px; + padding: 3px 9px; + border-radius: 999px; + background: #eff6ff; + color: #1d4ed8; +} + +.map-tile-source { + font-size: 12px; + color: var(--muted); +} + +@media (max-width: 600px) { + .home { + padding: 28px 16px 40px; + } +}