diff --git a/src/components/WeatherWidget.tsx b/src/components/WeatherWidget.tsx index 7d4b030..373a6a7 100644 --- a/src/components/WeatherWidget.tsx +++ b/src/components/WeatherWidget.tsx @@ -6,61 +6,31 @@ export default function WeatherWidget() { const [weather, setWeather] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); - const [config, setConfig] = useState(null); - // Load weather configuration useEffect(() => { - const loadConfig = async () => { - try { - const res = await fetch('/api/site-assets?key=weather_config'); - if (res.ok) { - const data = await res.json(); - if (data.data) { - const parsedConfig = JSON.parse(data.data); - setConfig(parsedConfig); - } - } - } catch (err) { - console.error('Failed to load weather config:', err); - } - }; - loadConfig(); - }, []); - - // Fetch weather data when config is loaded and enabled - useEffect(() => { - if (!config || !config.enabled || config.cities.length === 0) { - setLoading(false); - return; - } - const fetchWeather = async () => { try { setLoading(true); setError(null); - // Use the first city in the list for now - const city = config.cities[0]; - const res = await fetch(`/api/weather?lat=${city.lat}&lon=${city.lon}`); + const res = await fetch('/api/weather'); if (res.ok) { const data = await res.json(); - setWeather(data); + setWeather(data.data); } else { throw new Error('Failed to fetch weather data'); } } catch (err) { console.error('Weather API error:', err); - setError('Failed to load weather data'); + setError('Failed to load weather'); } finally { setLoading(false); } }; fetchWeather(); - }, [config]); - - if (!config?.enabled) return null; + }, []); if (loading) { return ( @@ -78,49 +48,54 @@ export default function WeatherWidget() { } if (error || !weather) { - return ( -
-
Weather unavailable
-
- ); + return null; } const current = weather.current; - const temp = current.celsius; - const icon = '🌤️'; + const today = weather.today; + + const weatherIcons: Record = { + 0: '☀️', 1: '🌤️', 2: '⛅', 3: '☁️', + 45: '🌫️', 48: '🌫️', 51: '🌦️', 53: '🌦️', 55: '🌦️', + 61: '🌧️', 63: '🌧️', 65: '🌧️', 71: '🌨️', 73: '🌨️', + 75: '🌨️', 80: '🌦️', 81: '🌦️', 82: '🌧️', + 95: '⛈️', 96: '⛈️', 99: '⛈️' + }; + + const icon = weatherIcons[current.weathercode] || '🌤️'; return (
-
- {icon} +
+ {icon}
-
- {temp}°C +
+ {current.celsius}°C +
+
+ {current.summary}
- {config.cities[0] && ( -
- {config.cities[0].name} -
- )}
-
- Updated: {new Date(current.time).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })} +
+ 📍 Otavalo + ↑{today.maxC}° ↓{today.minC}°
);