'use client'; import { CloudRain, Sun, Cloud, Thermometer } from 'lucide-react'; import { useState, useEffect } from 'react'; // Default to New York, user can change this const LAT = 40.7128; const LNG = -74.0060; export function WeatherCard() { const [weather, setWeather] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { async function fetchWeather() { try { const res = await fetch( `https://api.open-meteo.com/v1/forecast?latitude=${LAT}&longitude=${LNG}¤t=temperature_2m,relative_humidity_2m,weather_code&daily=temperature_2m_max,temperature_2m_min&temperature_unit=celsius&timezone=auto` ); const data = await res.json(); setWeather(data); } catch (e) { console.error(e); } finally { setLoading(false); } } fetchWeather(); const interval = setInterval(fetchWeather, 600000); // 10 mins return () => clearInterval(interval); }, []); if (loading) { return (
); } if (!weather?.current) { return (
Failed to load weather
); } const current = weather.current; const today = weather.daily; return (
Local Weather
New York, US
{Math.round(current.temperature_2m)}°C
{getWeatherDescription(current.weather_code)}
H: {Math.round(today.temperature_2m_max[0])}° L: {Math.round(today.temperature_2m_min[0])}°
Humidity: {current.relative_humidity_2m}%
); } function getWeatherDescription(code: number) { if (code === 0) return 'Clear'; if (code <= 3) return 'Partly Cloudy'; if (code <= 48) return 'Foggy'; if (code <= 67) return 'Rainy'; if (code <= 77) return 'Snowy'; return 'Cloudy'; }