import { useState, useEffect } from 'react';
import { AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, Line, ComposedChart, ReferenceLine } from 'recharts';
import { type Language, t } from '../translations';
import KpiCards from './KpiCards';
interface LipnoData {
timestamp: string;
date: string;
level: number;
inflow: number;
outflow: number;
volume: number;
fullness: number;
}
interface Props {
language: Language;
lakeId: string | null;
}
const CustomTooltip = ({ active, payload, label, language }: any) => {
if (active && payload && payload.length) {
const dict = t[language].chart;
return (
{label}
{dict.level}: {payload[0].value.toFixed(2)} m n. m.
{dict.outflow}: {payload[1].value.toFixed(1)} m³/s
);
}
return null;
};
const LakeDetail = ({ language, lakeId }: Props) => {
const [data, setData] = useState([]);
const [loading, setLoading] = useState(true);
const [lakeInfo, setLakeInfo] = useState(null);
const [isSmoothed, setIsSmoothed] = useState(true);
const dict = t[language].chart;
const topbarDict = t[language].topbar;
useEffect(() => {
fetch('/data/lakes_index.json')
.then(res => res.json())
.then(indexData => {
const found = indexData.find((l: any) => l.id === lakeId);
setLakeInfo(found || { name: 'Lipno 1', river: 'Vltava' });
})
.catch(err => console.error(err));
const internalId = lakeId ? lakeId.split('|')[0] : 'VLL1';
fetch(`/data/${internalId}.json`)
.then(res => res.json())
.then(json => {
const formattedData = json.map((item: any) => {
const outflow = item.flow === null || isNaN(item.flow) ? 0 : item.flow;
return {
timestamp: item.timestamp,
date: new Date(item.timestamp).toLocaleString(language === 'cs' ? 'cs-CZ' : 'en-GB', {
day: '2-digit', month: '2-digit', year: 'numeric',
hour: '2-digit', minute: '2-digit'
}),
level: item.level === null || isNaN(item.level) ? 0 : item.level,
outflow: outflow,
inflow: item.inflow || 0,
volume: item.volume || 0,
fullness: 0
};
});
setData(formattedData);
setLoading(false);
})
.catch(err => {
console.error('Failed to load data', err);
setLoading(false);
});
}, [language, lakeId]);
if (loading) {
return (
);
}
const latestData = data[data.length - 1] || { level: 0, inflow: 0, outflow: 0, volume: 0, fullness: 0 };
const curveType = isSmoothed ? 'monotone' : 'linear';
// Find the last record that actually has flow data (often the very last record is incomplete on PVL)
const lastValidFlowData = [...data].reverse().find(d => d.outflow > 0) || latestData;
const kpiData = {
level: latestData.level,
inflow: lastValidFlowData.inflow,
outflow: lastValidFlowData.outflow,
volume: lakeInfo?.volume || 0,
fullness: lakeInfo?.capacity || 0
};
return (
{topbarDict.updated} {new Date().toLocaleDateString(language === 'cs' ? 'cs-CZ' : 'en-GB', { day: '2-digit', month: '2-digit', year: 'numeric' })}, {new Date().toLocaleTimeString(language === 'cs' ? 'cs-CZ' : 'en-GB', { hour: '2-digit', minute: '2-digit' })} UTC
{/* CHART SECTION */}
{dict.title} {lakeInfo ? `${lakeInfo.name} ${lakeInfo.river ? `- ${lakeInfo.river}` : ''}` : 'Lipno 1 - Vltava'}
v.toFixed(2)} />
Math.max(dataMax, 1)]} stroke="var(--text-muted)" tick={{fill: 'var(--text-muted)', fontSize: 12}} />
} />
{/* Data Series */}
{/* Chart Legend */}
{dict.level}
{dict.outflow}
{/* Smoothed Toggle Control */}
{dict.view}
{dict.raw}
setIsSmoothed(!isSmoothed)}
>
{dict.smoothed}
);
};
export default LakeDetail;