function WeatherWidget() { const [weather, setWeather] = React.useState(null); const [loading, setLoading] = React.useState(true); const [error, setError] = React.useState(''); const [now, setNow] = React.useState(Date.now()); const [threshold, setThreshold] = React.useState(() => { try { const saved = Number(localStorage.getItem('closing-weather-humidity')); return [70, 75, 80, 85, 90, 95].includes(saved) ? saved : 80; } catch { return 80; } }); const active = React.useRef(null); const mounted = React.useRef(true); const load = React.useCallback(async () => { if (active.current) return; const controller = new AbortController(); active.current = controller; const timer = setTimeout(() => controller.abort(), 22000); setLoading(true); try { const res = await fetch('/api/weather', {signal: controller.signal, cache: 'no-store'}); const data = await res.json(); if (!res.ok) throw new Error(data.detail || '날씨를 불러오지 못했습니다. 잠시 후 다시 시도해 주세요.'); if (!Array.isArray(data.hours)) throw new Error('날씨 응답을 확인할 수 없습니다.'); if (mounted.current) { setWeather(data); setError(''); setNow(Date.now()); } } catch (e) { if (mounted.current) { setError(e.name === 'AbortError' ? '날씨 응답이 늦어지고 있습니다. 잠시 후 다시 시도해 주세요.' : e.message); setWeather(previous => previous ? {...previous, stale: true} : null); } } finally { clearTimeout(timer); active.current = null; if (mounted.current) setLoading(false); } }, []); React.useEffect(() => { mounted.current = true; load(); const interval = setInterval(() => { if (!document.hidden) load(); }, 10 * 60000); const clock = setInterval(() => setNow(Date.now()), 60000); const visible = () => { if (!document.hidden) { setNow(Date.now()); load(); } }; document.addEventListener('visibilitychange', visible); return () => { mounted.current = false; clearInterval(interval); clearInterval(clock); document.removeEventListener('visibilitychange', visible); active.current?.abort(); }; }, [load]); const info = ClosingWeather.summarize(weather, threshold, now); const fmt = ClosingWeather.formatTime; const value = (n, unit) => ClosingWeather.number(n) ? `${Math.round(n * 10) / 10}${unit}` : '—'; const current = weather?.current; const maximumPop = info.rows.filter(row => ClosingWeather.number(row.pop)).reduce((max, row) => Math.max(max, row.pop), -1); const naver = 'https://weather.naver.com/today/14130310'; const changeThreshold = e => { const next = Number(e.target.value); setThreshold(next); try { localStorage.setItem('closing-weather-humidity', String(next)); } catch {} }; return (

마감 전 날씨 확인

서귀포시 안덕면

{weather?.demo &&

화면 확인용 예시 데이터 · 실제 날씨가 아닙니다

} {!weather ? (

{loading ? '기상청 예보를 불러오고 있습니다.' : error}

밤사이 비와 습도를 확인하고 마감하세요.
) : ( <>
{value(current?.temperature, '°')}{current?.condition || '날씨 확인 중'}
현재 습도
{value(current?.humidity, '%')}
풍속
{value(current?.wind, ' m/s')}
최근 1시간 강수
{current?.rain_text || '—'}

{current ? `${fmt(current.time, true)} 관측 · 하늘 상태는 시간별 예보 기준` : '현재 관측값을 불러오지 못했습니다. 아래 시간별 예보를 확인해 주세요.'}

{info.title}

{info.tone === 'close' ? (
    {info.reasons.map(reason =>
  • {reason}
  • )}
) :

{info.description}

}
{error &&

{error} 이전 예보를 표시하고 있습니다.

}

{info.early ? '오늘 아침까지' : '오늘 밤부터 내일 아침까지'}

{fmt(info.start, true)}–{fmt(info.end, true)} · 한국 시간

최고 습도 {value(info.peak?.humidity, '%')}최대 강수확률 {maximumPop >= 0 ? `${maximumPop}%` : '—'}
{info.rows.map(row => )}{info.rows.map(row => )}{info.rows.map(row => )}{info.rows.map(row => )}{info.rows.map(row => )}{info.rows.map(row => )}
마감 이후부터 아침 09시까지 기상청 시간별 예보
시간{fmt(row.time, true)}
날씨{row.condition || '—'}
기온{value(row.temperature, '°')}
강수확률= 40 ? 'weather-rain' : ''}>{value(row.pop, '%')}
1시간 강수량{row.rain_text || '—'}
습도= threshold ? 'weather-humidity-high' : ''}>{value(row.humidity, '%')}

좌우로 밀어 시간별 예보를 확인하세요.

)}
창문 안내 기준 습도 {threshold}% 이상 · 비 예보

18시 마감부터 다음 날 09시까지 확인합니다. 마감 후에는 현재 시간부터, 새벽에는 당일 09시까지 확인합니다. 비·눈 예보, 강수확률 40% 이상 또는 설정 습도 이상일 때 닫기를 권유합니다. 마감 이후에는 최근 관측값도 함께 확인합니다. 습도 설정은 이 브라우저에 저장됩니다.

예보에 따른 참고 안내입니다. 실제 날씨와 실내 상태를 함께 확인해 주세요.

); }