import { useQuery } from '@tanstack/react-query'; import { BarController, BarElement, CategoryScale, Chart as ChartJS, Legend, LinearScale, LineController, LineElement, PointElement, Tooltip, type ChartData, type ChartOptions, } from 'chart.js'; import { useMemo } from 'react'; import { Chart } from 'react-chartjs-2'; import { fetchWeatherHistory, type WeatherStation } from '../../api/weather'; import { compassDirection, formatDecimal, formatTime, toHourly } from '../../weather/format'; ChartJS.register( CategoryScale, LinearScale, BarController, BarElement, LineController, LineElement, PointElement, Tooltip, Legend, ); type MixedType = 'bar' | 'line'; const HISTORY_DAYS = 7; const TEMPERATURE_COLOR = '#eb6834'; const PRECIPITATION_COLOR = '#2a78d6'; const GRID_COLOR = '#e2e8f0'; interface WeatherStationPanelProps { station: WeatherStation; lastRefresh: string | null; onClose: () => void; } export function WeatherStationPanel({ station, lastRefresh, onClose }: WeatherStationPanelProps) { const historyQuery = useQuery({ queryKey: ['weather-history', station.abbr, lastRefresh], queryFn: () => fetchWeatherHistory(station.abbr, HISTORY_DAYS), staleTime: Infinity, }); const hourly = useMemo(() => toHourly(historyQuery.data ?? []), [historyQuery.data]); const hasTemperature = hourly.some((h) => h.temperature !== null); const hasPrecipitation = hourly.some((h) => h.precipitation !== null); const first = hourly[0]?.time; const coversFullRange = first !== undefined && Date.now() - first.getTime() >= (HISTORY_DAYS - 1) * 24 * 3_600_000; const chartTitle = coversFullRange || first === undefined ? `Verlauf der letzten ${HISTORY_DAYS} Tage (Stundenwerte)` : `Verlauf seit ${first.toLocaleString('de-CH', { day: '2-digit', month: '2-digit', hour: '2-digit', minute: '2-digit' })}`; const data: ChartData = { labels: hourly.map((h) => h.time.toLocaleString('de-CH', { weekday: 'short', day: '2-digit', month: '2-digit', hour: '2-digit', minute: '2-digit', }), ), datasets: [ ...(hasTemperature ? [ { type: 'line' as const, label: 'Temperatur (°C)', data: hourly.map((h) => h.temperature), borderColor: TEMPERATURE_COLOR, backgroundColor: TEMPERATURE_COLOR, borderWidth: 2, spanGaps: true, yAxisID: 'y', }, ] : []), ...(hasPrecipitation ? [ { type: 'bar' as const, label: 'Niederschlag (mm/h)', data: hourly.map((h) => h.precipitation), backgroundColor: PRECIPITATION_COLOR, yAxisID: 'y1', }, ] : []), ], }; const options: ChartOptions = { responsive: true, maintainAspectRatio: false, animation: false, interaction: { mode: 'index', intersect: false }, elements: { point: { radius: 0 }, line: { tension: 0.2 } }, plugins: { legend: { position: 'bottom', labels: { boxWidth: 10, boxHeight: 10 } } }, scales: { x: { grid: { display: false }, ticks: { maxTicksLimit: 7, autoSkip: true } }, y: { display: hasTemperature, grid: { color: GRID_COLOR }, title: { display: true, text: '°C' }, }, y1: { display: hasPrecipitation, position: 'right', beginAtZero: true, grid: { display: false }, title: { display: true, text: 'mm/h' }, }, }, }; return ( ); }