"use client"; import { useEffect, useState } from "react"; import { motion } from "framer-motion"; import { AlertTriangle, Droplets, Gauge, MapPin, Navigation, Umbrella, Wind } from "lucide-react"; import { calculateFeelsLike, formatDateTime, formatDistance, formatHumidity, formatPressure, formatRainfall, formatTemperature, formatWind, getWeatherDescription, getWeatherMoodFromData, } from "@/lib/weather-utils"; import type { SynopStation } from "@/types/imgw"; import type { ImgwCurrentWeather } from "@/types/imgw-current"; import { WeatherIcon } from "@/components/weather/weather-icon"; import { WeatherEffects } from "@/components/weather/weather-effects"; import { useI18n } from "@/lib/i18n"; import { useWeatherStore } from "@/lib/store"; type DevWeatherEffectOverride = "rain" | "thunderstorm" | "none"; function readDevWeatherEffectOverride(): DevWeatherEffectOverride | null { if (process.env.NODE_ENV !== "development" || typeof window === "undefined") return null; const effect = new URLSearchParams(window.location.search).get("effect"); if (effect === "rain" || effect === "thunderstorm" || effect === "none") return effect; if (effect === "storm") return "thunderstorm"; return null; } export function WeatherHero({ station, currentWeather, currentWeatherLoading = false, currentForecastWeatherCode, locationName, distanceKm, }: { station: SynopStation; currentWeather?: ImgwCurrentWeather | null; currentWeatherLoading?: boolean; currentForecastWeatherCode?: number | null; locationName?: string; distanceKm?: number | null; }) { const { language, t } = useI18n(); const temperatureUnit = useWeatherStore((state) => state.temperatureUnit); const windSpeedUnit = useWeatherStore((state) => state.windSpeedUnit); const precipitationUnit = useWeatherStore((state) => state.precipitationUnit); const pressureUnit = useWeatherStore((state) => state.pressureUnit); const distanceUnit = useWeatherStore((state) => state.distanceUnit); const [devEffectOverride, setDevEffectOverride] = useState(null); const displayedLocationName = locationName ?? station.name; const hasGlobalModel = currentWeather?.coverage === "global-model"; const hasFullHybridAnalysis = currentWeather?.coverage === "full" || currentWeather?.coverage === "hourly"; const hasPartialHybridAnalysis = currentWeather?.coverage === "precipitation-only"; const hasDistantFallback = !hasGlobalModel && !hasFullHybridAnalysis && !currentWeatherLoading && distanceKm !== undefined && distanceKm !== null && distanceKm >= 30; const formattedDistance = formatDistance(distanceKm ?? null, language, distanceUnit); const displayedStation = currentWeather ? { ...station, measuredAt: hasFullHybridAnalysis || hasGlobalModel ? currentWeather.measuredAt : station.measuredAt, temperature: currentWeather.temperature ?? station.temperature, windSpeed: currentWeather.windSpeed ?? station.windSpeed, windDirection: currentWeather.windDirection ?? station.windDirection, humidity: currentWeather.humidity ?? station.humidity, pressure: currentWeather.pressure ?? station.pressure, rainfall: currentWeather.precipitation10m ?? station.rainfall, } : station; const mood = getWeatherMoodFromData(displayedStation); const feelsLike = currentWeather?.feelsLike ?? calculateFeelsLike(displayedStation.temperature, displayedStation.humidity, displayedStation.windSpeed); const metrics = [ { icon: Droplets, label: t("weather.humidity"), value: formatHumidity(displayedStation.humidity, language) }, { icon: Wind, label: t("weather.wind"), value: formatWind(displayedStation.windSpeed, null, language, windSpeedUnit), }, { icon: Umbrella, label: hasGlobalModel ? t("weather.currentPrecipitation") : currentWeather?.precipitation10m !== null && currentWeather?.precipitation10m !== undefined ? t("weather.rainfall10m") : t("weather.rainfallTotal"), value: formatRainfall(displayedStation.rainfall, language, precipitationUnit), }, { icon: Gauge, label: t("weather.pressure"), value: formatPressure(displayedStation.pressure, language, pressureUnit), }, ]; const effectPrecipitation10m = devEffectOverride === "rain" || devEffectOverride === "thunderstorm" ? 0.1 : devEffectOverride === "none" ? 0 : currentWeather?.precipitation10m; const effectThunderstorm = devEffectOverride === "thunderstorm" ? true : devEffectOverride === "rain" || devEffectOverride === "none" ? false : currentWeather?.condition === "thunderstorm"; const weatherDescription = getWeatherDescription( displayedStation, language, currentWeather?.condition, currentWeather?.weatherCode, currentWeather?.cloudCover, currentForecastWeatherCode, ); useEffect(() => { if (process.env.NODE_ENV !== "development") return; const updateOverride = () => setDevEffectOverride(readDevWeatherEffectOverride()); updateOverride(); window.addEventListener("popstate", updateOverride); return () => window.removeEventListener("popstate", updateOverride); }, []); return (
{displayedLocationName}

{currentWeatherLoading ? t("location.heroHybridLoading", { station: station.name }) : hasGlobalModel ? t("location.heroGlobalModelSource", { location: displayedLocationName }) : hasFullHybridAnalysis ? t("location.heroHybridSource", { location: displayedLocationName }) : hasPartialHybridAnalysis ? t("location.heroHybridPartial", { station: station.name, distance: formattedDistance }) : locationName ? t("location.heroStationFallbackWithDistance", { station: station.name, distance: formattedDistance, }) : t("location.heroStationFallback", { station: station.name })}

{hasFullHybridAnalysis && locationName && (

{t("location.heroNearestStation", { station: station.name, distance: formattedDistance })}

)} {hasDistantFallback && (

{t("location.heroDistantFallback")}

)}
{formatTemperature(displayedStation.temperature, language, temperatureUnit)}

{weatherDescription}

{t("weather.feelsLike")} {formatTemperature(feelsLike, language, temperatureUnit)} ·{" "} {hasGlobalModel ? t("weather.modelUpdate") : t("weather.measurement")}{" "} {formatDateTime(displayedStation.measuredAt, language)}

{metrics.map(({ icon: Icon, label, value }) => (
{label}

{value}

))}
{displayedStation.windDirection !== null && (

{t("weather.windDirection")}: {displayedStation.windDirection}°

)}
); }