import React, { useState, useEffect } from 'react'; import { Sun, Cloud, CloudRain, CloudSnow, CloudLightning, CloudDrizzle, Wind, Droplets, Thermometer, RefreshCw, Clock } from 'lucide-react'; export default function WeatherApp() { const [weatherData, setWeatherData] = useState(null); const [forecast, setForecast] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [lastUpdated, setLastUpdated] = useState(null); const fetchWeather = async () => { setLoading(true); setError(null); try { // In a real app, you would use your own API key and proper error handling // This is a mock implementation for demonstration purposes // Simulate API call delay await new Promise(resolve => setTimeout(resolve, 1000)); // Mock data for Kampala const mockCurrentWeather = { main: { temp: 23, feels_like: 24, humidity: 65, pressure: 1012 }, weather: [ { main: 'Clouds', description: 'scattered clouds', icon: '03d' } ], wind: { speed: 3.6, deg: 220 }, sys: { sunrise: 1617507000, sunset: 1617550200 }, dt: Date.now() / 1000 }; // Mock forecast data const mockForecast = [ { day: 'Tomorrow', temp: 24, condition: 'Sunny', icon: 'Sun' }, { day: 'Wednesday', temp: 25, condition: 'Partly Cloudy', icon: 'Cloud' }, { day: 'Thursday', temp: 23, condition: 'Rain Showers', icon: 'CloudRain' }, { day: 'Friday', temp: 22, condition: 'Thunderstorms', icon: 'CloudLightning' }, { day: 'Saturday', temp: 24, condition: 'Partly Cloudy', icon: 'Cloud' } ]; setWeatherData(mockCurrentWeather); setForecast(mockForecast); setLastUpdated(new Date()); } catch (err) { setError('Failed to fetch weather data. Please try again.'); console.error('Error fetching weather data:', err); } finally { setLoading(false); } }; useEffect(() => { fetchWeather(); // In a real app, you might want to set up a refresh interval const interval = setInterval(fetchWeather, 30 * 60 * 1000); // Refresh every 30 minutes return () => clearInterval(interval); }, []); // Helper function to get the appropriate weather icon const getWeatherIcon = (iconCode) => { if (!iconCode) return ; const iconMap = { '01d': , '01n': , '02d': , '02n': , '03d': , '03n': , '04d': , '04n': , '09d': , '09n': , '10d': , '10n': , '11d': , '11n': , '13d': , '13n': , '50d': , '50n': }; return iconMap[iconCode] || ; }; // Helper function for forecast icons const getForecastIcon = (iconName, size = 8) => { const iconMap = { 'Sun': , 'Cloud': , 'CloudRain': , 'CloudLightning': , 'CloudSnow': , 'CloudDrizzle': , 'Wind': }; return iconMap[iconName] || ; }; // Format time from timestamp const formatTime = (timestamp) => { if (!timestamp) return ''; const date = new Date(timestamp * 1000); return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); }; return (
{/* Header */}

Kampala, Uganda

{lastUpdated && (
Updated: {lastUpdated.toLocaleTimeString()}
)}
{/* Error Message */} {error && (

{error}

)} {/* Main Weather Card */} {loading ? (
) : weatherData ? (
{getWeatherIcon(weatherData.weather[0].icon)}

{Math.round(weatherData.main.temp)}°C

{weatherData.weather[0].description}

Feels like {Math.round(weatherData.main.feels_like)}°C

Humidity

{weatherData.main.humidity}%

Wind

{weatherData.wind.speed} m/s

Pressure

{weatherData.main.pressure} hPa

Sunrise/Sunset

{formatTime(weatherData.sys.sunrise)} / {formatTime(weatherData.sys.sunset)}

) : null} {/* 5-Day Forecast */}

5-Day Forecast

{loading ? ( Array(5).fill().map((_, i) => (
)) ) : ( forecast.map((day, index) => (

{day.day}

{getForecastIcon(day.icon)}

{day.temp}°C

{day.condition}

)) )}
{/* Footer */}
); }