import { useState, useEffect } from 'react'; import { FiWind, FiSunrise, FiSunset, FiThermometer, FiAlertCircle } from 'react-icons/fi'; interface WeatherProps { lat: number; lng: number; language: 'cs' | 'en'; sensorTemp?: number; windUnit?: 'kmh' | 'ms'; } interface WeatherData { temp: number; windSpeed: number; // m/s windGusts: number; // m/s windDir: number; // degrees sunrise: string; sunset: string; time?: string; } const getCompassDirection = (degrees: number, language: 'cs' | 'en') => { const directionsEn = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW']; const directionsCs = ['S', 'SV', 'V', 'JV', 'J', 'JZ', 'Z', 'SZ']; const directions = language === 'cs' ? directionsCs : directionsEn; const index = Math.round(((degrees %= 360) < 0 ? degrees + 360 : degrees) / 45) % 8; return directions[index]; }; const formatTime = (isoString: string) => { if (!isoString) return '--:--'; try { const match = isoString.match(/T(\d{2}:\d{2})/); if (match) return match[1]; const date = new Date(isoString); if (isNaN(date.getTime())) { return isoString; } return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); } catch (e) { return '--:--'; } }; export const WeatherWidget = ({ lat, lng, language, sensorTemp, windUnit = 'kmh' }: WeatherProps) => { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(false); useEffect(() => { if (data) { console.log("Weather data loaded:", data); } }, [data]); useEffect(() => { if (!lat || !lng) { setLoading(false); setError(true); return; } const fetchWeather = async () => { try { setLoading(true); const res = await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lng}¤t=temperature_2m,wind_speed_10m,wind_direction_10m,wind_gusts_10m&daily=sunrise,sunset&timezone=auto&wind_speed_unit=${windUnit}`); if (!res.ok) throw new Error('Failed to fetch weather'); const json = await res.json(); setData({ temp: json.current.temperature_2m, windSpeed: json.current.wind_speed_10m, windGusts: json.current.wind_gusts_10m, windDir: json.current.wind_direction_10m, sunrise: json.daily.sunrise[0], sunset: json.daily.sunset[0], time: json.current.time }); setError(false); } catch (err) { console.error('Weather fetch error:', err); setError(true); } finally { setLoading(false); } }; fetchWeather(); // Refresh weather every 15 minutes const interval = setInterval(fetchWeather, 15 * 60 * 1000); return () => clearInterval(interval); }, [lat, lng, windUnit]); const dict = { cs: { title: 'POČASÍ A VÍTR', error: 'Data nedostupná', wind: 'Vítr', gusts: 'Nárazy', temp: 'Teplota' }, en: { title: 'WEATHER & WIND', error: 'Data unavailable', wind: 'Wind', gusts: 'Gusts', temp: 'Temp' } }[language]; if (loading) { return
Loading weather...
; } if (error || !data) { return (

{dict.title}

{dict.error}
); } return (
{dict.title} {data.time ? `(${formatTime(data.time)})` : ''}
{/* Compass and Wind Info Wrapper */}
{/* SVG Compass Ring */} {/* Generate Ticks */} {Array.from({ length: 72 }).map((_, i) => { const angle = i * 5; const isMajor = angle % 90 === 0; const isMedium = angle % 45 === 0; const innerR = isMajor ? 90 : isMedium ? 100 : 105; const outerR = 115; const rad = (angle - 90) * (Math.PI / 180); const x1 = 130 + innerR * Math.cos(rad); const y1 = 130 + innerR * Math.sin(rad); const x2 = 130 + outerR * Math.cos(rad); const y2 = 130 + outerR * Math.sin(rad); if (isMajor) return null; // Put text here instead return ; })} {language === 'cs' ? 'S' : 'N'} {language === 'cs' ? 'V' : 'E'} {language === 'cs' ? 'J' : 'S'} {language === 'cs' ? 'Z' : 'W'} {/* Direction Indicator */} {(() => { const dirRad = (data.windDir + 180 - 90) * (Math.PI / 180); const x = 130 + 94 * Math.cos(dirRad); const y = 130 + 94 * Math.sin(dirRad); return ( ); })()} {/* Center Data */}
{data.windSpeed.toFixed(1)} {windUnit === 'kmh' ? 'km/h' : 'm/s'}
{dict.gusts}: {data.windGusts.toFixed(1)} {windUnit === 'kmh' ? 'km/h' : 'm/s'}
{/* Corner Elements */}
{sensorTemp !== undefined ? sensorTemp.toFixed(1) : data.temp.toFixed(1)} °C
{formatTime(data.sunrise)}
{formatTime(data.sunset)}
); };