OpenDataVisualizer/client/src/hydrology/useHydrologyData.ts
Claudio Schaad a838b17dc1 Add live water layer from BAFU hydrology data
- New Schaad.OpenData.Hydrology project: LindasHydroClient fetches the
  current level, discharge, water temperature and danger level of nine
  BAFU stations around Thurgau from LINDAS every 10 minutes
- LINDAS has no history, so the server keeps readings per day (60 days)
  and fills gaps from the 7-day plot JSON of hydrodaten.admin.ch
- API: /api/hydrology/meta, /stations, /stations/{id}/history
- Combined map: "Gewässer (live)" layer with river circles (size by
  discharge, colour by danger level or water temperature), lake level
  badges, station panel with 7-day chart, overview and municipality
  context entries
- Docs: docs/datasets/gewaesser.md, overview in docs/datasets.md,
  CLAUDE.md; Docker and appsettings wiring

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 20:40:33 +01:00

41 lines
1.2 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import { useMemo, useState } from 'react';
import { fetchHydroStations, fetchHydrologyMeta } from '../api/hydrology';
import type { HydroMetric } from './format';
const REFRESH_MS = 5 * 60_000;
export function useHydrologyData() {
const metaQuery = useQuery({
queryKey: ['hydrology-meta'],
queryFn: fetchHydrologyMeta,
refetchInterval: REFRESH_MS,
});
const stationsQuery = useQuery({
queryKey: ['hydrology-stations', metaQuery.data?.lastRefresh ?? null],
queryFn: fetchHydroStations,
enabled: !!metaQuery.data,
staleTime: Infinity,
});
const [metric, setMetric] = useState<HydroMetric>('discharge');
const stations = useMemo(() => stationsQuery.data ?? [], [stationsQuery.data]);
const latestTime = useMemo(
() =>
stations.reduce<string | null>(
(latest, s) => (s.time && (!latest || s.time > latest) ? s.time : latest),
null,
),
[stations],
);
return {
meta: metaQuery.data,
stations,
latestTime,
metric,
setMetric,
error: metaQuery.error ?? stationsQuery.error,
loading: metaQuery.isLoading || stationsQuery.isLoading || stations.length === 0,
};
}