OpenDataVisualizer/client/src/map/useYearPlayback.ts
Claudio Schaad 383226ed4f Add combined Thurgau map with switchable layers (#5)
- One shared Leaflet map (MapHost) with the traffic, watercraft and
  vehicle maps turned into layer components that attach to it and clean
  up on unmount; each layer adds its own data credit
- Data loading and per-dataset state moved into hooks used by both the
  single pages and the combined page
- /karte/thurgau: layer panel with on/off switch, controls and legend per
  dataset, shared year controls for the yearly layers, traffic day/time
  controls, layers and year in the URL
- Side panel shows the selected municipality, harbour or station, or an
  overview of all active layers; the municipality panel lists the
  counting stations and harbours in that municipality
- Start page shows only the combined map; single maps are linked from
  the layer cards
- Docs: combined map in docs/datasets.md and CLAUDE.md

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claudio Schaad <c.schaad@pog.ch>
Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
2026-11-18 19:08:09 +00:00

45 lines
1.3 KiB
TypeScript

import { useCallback, useEffect, useState } from 'react';
import { useSearchParams } from 'react-router-dom';
const PLAY_INTERVAL_MS = 1600;
const YEAR_PARAM = 'jahr';
export interface YearPlayback {
year: number | null;
setYear: (year: number) => void;
playing: boolean;
setPlaying: (playing: boolean) => void;
}
export function useYearPlayback(years: number[]): YearPlayback {
const [searchParams, setSearchParams] = useSearchParams();
const [playing, setPlaying] = useState(false);
const requested = Number(searchParams.get(YEAR_PARAM));
const year = years.includes(requested) ? requested : (years[years.length - 1] ?? null);
const setYear = useCallback(
(value: number) =>
setSearchParams(
(params) => {
params.set(YEAR_PARAM, String(value));
return params;
},
{ replace: true },
),
[setSearchParams],
);
useEffect(() => {
if (!playing || years.length < 2 || year === null) {
return;
}
const timer = window.setTimeout(() => {
const index = years.indexOf(year);
setYear(years[(index + 1) % years.length]);
}, PLAY_INTERVAL_MS);
return () => window.clearTimeout(timer);
}, [playing, years, year, setYear]);
return { year, setYear, playing, setPlaying };
}