import type {WeatherSky} from '@o2o/shared'; import {useSite} from '@site/lib/site-context'; import {useLiveWeather} from '@site/lib/use-live-weather'; import {useWeatherNotes} from '@site/lib/use-weather-note'; import {weatherBand, weatherMood} from '@site/lib/derive'; import {CloudIcon, RainIcon, SnowIcon, SunIcon} from './icons'; const MOOD_ICON = { 맑음: SunIcon, 흐림: CloudIcon, 비: RainIcon, 눈: SnowIcon, } as const; function observed(iso: string): string | undefined { const m = /^\d{4}-(\d{2})-(\d{2})T(\d{2}):(\d{2})/.exec(iso); return m ? `${Number(m[1])}월 ${Number(m[2])}일 ${m[3]}:${m[4]} 관측` : undefined; } export function Weather() { const payload = useSite(); const weather = useLiveWeather({ initial: payload.local.weather, regionCode: payload.place.regionCode, latitude: payload.place.latitude, longitude: payload.place.longitude, }); const mood = weatherMood(weather?.condition ?? ''); const band = weatherBand(weather?.temperature ?? 0); const notes = payload.local.weather; const condition = (weather?.condition ?? '') as WeatherSky; const skyKey: WeatherSky = notes?.noteSets?.[condition] ? condition : mood; const [line, bandLine] = useWeatherNotes( notes?.noteSets?.[skyKey], notes?.notes?.[mood] ?? weather?.note, notes?.tempNoteSets?.[band], notes?.tempNotes?.[band], ); if (!weather) return null; const Icon = MOOD_ICON[mood]; const region = payload.place.addressLocality?.replace(/(시|군)$/, ''); const at = observed(weather.observedAt); return (

오늘의 날씨

{region ? `지금 ${region}은` : '오늘의 날씨'}

{Math.round(weather.temperature * 10) / 10}° {weather.condition}
{at &&

{at}

} {line && (

{line}

)}

{band} {bandLine && {bandLine}}

); }