"use client"; import { useEffect, useState } from "react"; import { Cloud, Sun, CloudRain, Wind, Droplets, Loader2, Clock } from "lucide-react"; import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card"; interface WeatherData { temperature: number; condition: string; windSpeed: number; humidity: number; weatherCode: number; } // Coordonnées de Fakarava (Rotoava) const FAKARAVA_LAT = -16.3167; const FAKARAVA_LON = -145.6167; // Codes météo Open-Meteo vers descriptions const getWeatherCondition = (code: number): { text: string; icon: React.ReactNode } => { // Codes Open-Meteo WMO Weather interpretation codes if (code === 0) { return { text: "Ciel dégagé", icon: }; } else if (code <= 3) { return { text: "Partiellement nuageux", icon: }; } else if (code <= 48) { return { text: "Nuageux", icon: }; } else if (code <= 55) { return { text: "Brouillard", icon: }; } else if (code <= 67) { return { text: "Pluie", icon: }; } else if (code <= 77) { return { text: "Neige", icon: }; } else if (code <= 82) { return { text: "Averses", icon: }; } else if (code <= 86) { return { text: "Averses de neige", icon: }; } else { return { text: "Orage", icon: }; } }; export default function WeatherWidget() { const [weather, setWeather] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [localTime, setLocalTime] = useState(""); // Mise à jour de l'heure locale chaque seconde useEffect(() => { const updateTime = () => { const now = new Date(); // Fuseau horaire de Tahiti (UTC-10) const tahitiTime = new Date(now.toLocaleString("en-US", { timeZone: "Pacific/Tahiti" })); const hours = tahitiTime.getHours().toString().padStart(2, "0"); const minutes = tahitiTime.getMinutes().toString().padStart(2, "0"); setLocalTime(`${hours}:${minutes}`); }; updateTime(); const timeInterval = setInterval(updateTime, 1000); return () => clearInterval(timeInterval); }, []); useEffect(() => { const fetchWeather = async () => { try { setLoading(true); setError(null); // API Open-Meteo (gratuit, pas de clé API nécessaire) const response = await fetch( `https://api.open-meteo.com/v1/forecast?latitude=${FAKARAVA_LAT}&longitude=${FAKARAVA_LON}¤t=temperature_2m,relative_humidity_2m,weather_code,wind_speed_10m&wind_speed_unit=kmh&timezone=Pacific/Tahiti` ); if (!response.ok) { throw new Error("Erreur lors de la récupération de la météo"); } const data = await response.json(); const current = data.current; setWeather({ temperature: Math.round(current.temperature_2m), condition: getWeatherCondition(current.weather_code).text, windSpeed: Math.round(current.wind_speed_10m), humidity: current.relative_humidity_2m, weatherCode: current.weather_code, }); } catch (err) { console.error("Erreur météo:", err); setError("Impossible de charger la météo"); // Valeurs par défaut en cas d'erreur setWeather({ temperature: 28, condition: "Ensoleillé", windSpeed: 15, humidity: 75, weatherCode: 0, }); } finally { setLoading(false); } }; fetchWeather(); // Rafraîchir toutes les heures const interval = setInterval(fetchWeather, 3600000); return () => clearInterval(interval); }, []); if (loading) { return ( Météo - Fakarava ); } if (!weather) { return null; } const weatherInfo = getWeatherCondition(weather.weatherCode); return ( Météo - Fakarava {localTime && ( {localTime} )} {weather.temperature}°C {weather.condition} {error && ( Données en cache )} {weatherInfo.icon} Vent: {weather.windSpeed} km/h Humidité: {weather.humidity}% ); }
{weather.temperature}°C
{weather.condition}
Données en cache