import L from 'leaflet'; import { useEffect, useRef } from 'react'; import type { DayView, Station } from '../api/traffic'; import { escapeHtml } from '../map/baseLayers'; import { useMap, useMapAttribution } from '../map/mapContext'; import { FlowAnimator } from '../traffic/flowAnimator'; import { sumCounts } from '../traffic/vehicleClasses'; interface TrafficLayerProps { stations: Station[]; day: DayView | null; hour: number; selectedCode: number | null; onSelect: (code: number) => void; onAnimatorReady: (animator: FlowAnimator) => void; fitToData?: boolean; } const DATA_ATTRIBUTION = 'Verkehr: Kanton Thurgau'; export function TrafficLayer({ stations, day, hour, selectedCode, onSelect, onAnimatorReady, fitToData = true, }: TrafficLayerProps) { const map = useMap(); useMapAttribution(DATA_ATTRIBUTION); const animatorRef = useRef(null); const roadsRef = useRef(null); const markersRef = useRef>(new Map()); const onSelectRef = useRef(onSelect); onSelectRef.current = onSelect; const fittedRef = useRef(!fitToData); const dayRef = useRef(day); dayRef.current = day; useEffect(() => { const canvas = document.createElement('canvas'); canvas.className = 'flow-canvas'; map.getContainer().appendChild(canvas); const roads = L.layerGroup().addTo(map); const animator = new FlowAnimator(map, canvas); animator.start(); roadsRef.current = roads; animatorRef.current = animator; onAnimatorReady(animator); const markers = markersRef.current; return () => { animator.dispose(); canvas.remove(); roads.remove(); markers.forEach((marker) => marker.remove()); markers.clear(); roadsRef.current = null; animatorRef.current = null; }; }, [map, onAnimatorReady]); useEffect(() => { const roads = roadsRef.current; if (!roads) { return; } roads.clearLayers(); markersRef.current.forEach((marker) => marker.remove()); markersRef.current.clear(); for (const station of stations) { for (const direction of station.directions.slice(0, 1)) { if (direction.path.length >= 2) { L.polyline(direction.path, { color: '#334155', weight: 5, opacity: 0.18, interactive: false, }).addTo(roads); } } const marker = L.circleMarker([station.lat, station.lon], { radius: 5, color: '#0f172a', weight: 1.5, fillColor: '#ffffff', fillOpacity: 0.85, }) .on('click', () => { onSelectRef.current(station.code); if (map.getZoom() < 14) { map.flyTo([station.lat, station.lon], 15, { duration: 0.8 }); } }) .addTo(map); markersRef.current.set(station.code, marker); } animatorRef.current?.setStations(stations); animatorRef.current?.setDay(dayRef.current); if (stations.length > 0 && !fittedRef.current) { fittedRef.current = true; map.fitBounds(L.latLngBounds(stations.map((s) => [s.lat, s.lon] as L.LatLngTuple)), { padding: [40, 40], }); } }, [map, stations]); useEffect(() => { animatorRef.current?.setDay(day); }, [day]); useEffect(() => { const hourIndex = Math.min(23, Math.max(0, hour)); const dayByStation = new Map(day?.stations.map((s) => [s.code, s]) ?? []); for (const station of stations) { const marker = markersRef.current.get(station.code); if (!marker) { continue; } const stationDay = dayByStation.get(station.code); const perDirection = station.directions.map((direction) => { const hours = stationDay?.directions.find((d) => d.direction === direction.name)?.hours; return { name: direction.name, total: sumCounts(hours?.[hourIndex]) }; }); const total = perDirection.reduce((sum, d) => sum + d.total, 0); const selected = station.code === selectedCode; marker.setRadius(3 + Math.sqrt(total) / 6); marker.setStyle({ color: selected ? '#dc2626' : '#0f172a', weight: selected ? 3 : 1.5, fillColor: selected ? '#fee2e2' : '#ffffff', }); marker.bindTooltip( `${escapeHtml(station.name)}
` + `${escapeHtml(station.road)} · ${String(hourIndex).padStart(2, '0')}:00–${String(hourIndex + 1).padStart(2, '0')}:00
` + perDirection .map((d) => `${escapeHtml(d.name)}: ${d.total} Fz/h`) .join('
'), { direction: 'top', offset: [0, -6] }, ); } }, [map, stations, day, hour, selectedCode]); return null; }