'use client' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { LineChart, Line, XAxis, YAxis, CartesianGrid, ResponsiveContainer, ReferenceLine, Legend, Area, ComposedChart, Tooltip, } from 'recharts' import { format, parseISO } from 'date-fns' import type { WeatherPoint } from '@/lib/types' import { useThresholdStore } from '@/store/threshold-store' interface WindSpeedChartProps { data: WeatherPoint[] yesterdayData?: WeatherPoint[] className?: string } interface ChartDataPoint { timestamp: string time: string speed?: number yesterdaySpeed?: number isInRange: boolean } export function WindSpeedChart({ data, yesterdayData, className }: WindSpeedChartProps) { const { speedMin, speedMax } = useThresholdStore() // Filter data for TODAY's 8am-10pm window only const filterTimeWindow = (points: WeatherPoint[]) => { const now = new Date() const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 8, 0, 0) const todayEnd = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 22, 0, 0) return points.filter((point) => { const timestamp = parseISO(point.timestamp) return timestamp >= todayStart && timestamp < todayEnd }) } // Generate static time slots for 8am-10pm (14 hours) const generateTimeSlots = (): { hour: number; label: string }[] => { const slots = [] for (let hour = 8; hour < 22; hour++) { slots.push({ hour, label: format(new Date().setHours(hour, 0, 0, 0), 'ha') }) } return slots } const filteredData = filterTimeWindow(data) // Don't filter yesterday's data - show all available historical data const filteredYesterday = yesterdayData || [] // Generate static time slots and map data to them const timeSlots = generateTimeSlots() const chartData: ChartDataPoint[] = timeSlots.map(slot => { // Find forecast data for this hour const forecastPoint = filteredData.find(point => parseISO(point.timestamp).getHours() === slot.hour ) // Find yesterday's data for this hour const yesterdayPoint = filteredYesterday.find(yp => parseISO(yp.timestamp).getHours() === slot.hour ) return { timestamp: forecastPoint?.timestamp || '', time: slot.label, speed: forecastPoint?.wind_speed, yesterdaySpeed: yesterdayPoint?.wind_speed, isInRange: forecastPoint ? (forecastPoint.wind_speed >= speedMin && forecastPoint.wind_speed <= speedMax) : false } }) // Custom tooltip const CustomTooltip = ({ active, payload }: any) => { if (!active || !payload || !payload.length) return null const data = payload[0].payload // Don't show tooltip if there's no forecast data for this time slot if (data.speed === undefined) return null return (
{data.time}
Forecast: {data.speed.toFixed(1)} mph
{data.yesterdaySpeed !== undefined && (Yesterday: {data.yesterdaySpeed.toFixed(1)} mph
)}