feat(site): 오늘의 날씨 문구 로딩과 조건별 순환 추가
목업에만 있던 날씨 문구 계약을 제품 payload와 렌더러에 연결한다. 군산 전용 장소를 다른 사업장에 복사하지 않도록 공통 안내를 별도 JSON으로 관리한다. 날씨 변환 단위 테스트 3건, 렌더링 테스트 3건 및 TypeScript·ESLint 통과. 전체 발행 테스트는 깨끗한 renderer 재빌드 후 별도 확인.
This commit is contained in:
1 parent
f2087aad5e
commit
820a2e9e35
10 files changed
+149
-8
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import {useEffect, useState} from 'react';
|
||||
|
||||
/** 첫 렌더는 고정해 SSR과 맞추고, 이후에는 한 바퀴 안에서 중복 없이 돌린다. */
|
||||
export function useWeatherNote(lines: string[] | undefined, fallback?: string) {
|
||||
const [selection, setSelection] = useState<{lines: string[]; index: number}>();
|
||||
useEffect(() => {
|
||||
if (!lines?.length) return;
|
||||
let remaining: number[] = [];
|
||||
let previous = -1;
|
||||
const next = () => {
|
||||
if (!remaining.length) remaining = lines.map((_, index) => index);
|
||||
const choices = remaining.filter((index) => index !== previous);
|
||||
const available = choices.length ? choices : remaining;
|
||||
const index = available[Math.floor(Math.random() * available.length)];
|
||||
remaining = remaining.filter((value) => value !== index);
|
||||
previous = index;
|
||||
setSelection({lines, index});
|
||||
};
|
||||
// effect 안의 동기 setState 대신 첫 타이머에서 무작위 문구로 전환한다.
|
||||
const first = window.setTimeout(next, 0);
|
||||
const timer = window.setInterval(next, 20_000);
|
||||
return () => {
|
||||
window.clearTimeout(first);
|
||||
window.clearInterval(timer);
|
||||
};
|
||||
}, [lines]);
|
||||
return lines?.[selection?.lines === lines ? selection.index : 0] ?? fallback;
|
||||
}
|
||||
@@ -11,6 +11,7 @@
|
||||
*/
|
||||
import {useSite} from '@site/lib/site-context';
|
||||
import {useLiveWeather} from '@site/lib/use-live-weather';
|
||||
import {useWeatherNote} from '@site/lib/use-weather-note';
|
||||
import {weatherBand, weatherMood} from '@site/lib/derive';
|
||||
import {Card, Section} from '@site/lib/ui';
|
||||
import './weather.css';
|
||||
@@ -32,12 +33,12 @@ export function WeatherSection() {
|
||||
longitude: payload.place.longitude,
|
||||
});
|
||||
|
||||
if (!weather) return null;
|
||||
|
||||
const mood = weatherMood(weather.condition);
|
||||
const band = weatherBand(weather.temperature);
|
||||
const mood = weatherMood(weather?.condition ?? '');
|
||||
const band = weatherBand(weather?.temperature ?? 0);
|
||||
const notes = payload.local.weather;
|
||||
const skyKey = weather?.condition === '구름많음' ? '구름많음' : mood;
|
||||
// 조건별 문장이 우선. 없으면 예전 한 칸(note)으로 떨어진다 — 옛 payload 도 깨지지 않는다.
|
||||
const line = payload.local.weather?.notes?.[mood] ?? weather.note;
|
||||
const line = useWeatherNote(notes?.noteSets?.[skyKey], notes?.notes?.[mood] ?? weather?.note);
|
||||
/*
|
||||
* ★ 기온 줄 (2026-09-09, 사장님: "기온에 따라서 문구 다르게, 펜션지역의 관광에 맞춰서")
|
||||
* 하늘 줄은 집 안 이야기(마당·CAFÉ)라 "그래서 오늘 어디를 가지"에는 답하지 않았다.
|
||||
@@ -45,7 +46,8 @@ export function WeatherSection() {
|
||||
* ★ 없으면 줄 자체를 그리지 않는다 — 기온대는 항상 계산되지만 문장은 사장님이 적은
|
||||
* 것뿐이라, 빈 칸을 우리가 채우면 지어낸 관광 안내가 된다.
|
||||
*/
|
||||
const bandLine = payload.local.weather?.tempNotes?.[band];
|
||||
const bandLine = useWeatherNote(notes?.tempNoteSets?.[band], notes?.tempNotes?.[band]);
|
||||
if (!weather) return null;
|
||||
const observed = observedText(weather.observedAt);
|
||||
|
||||
return (
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import {expect, it} from 'vitest';
|
||||
import {renderToStaticMarkup} from 'react-dom/server';
|
||||
import {SiteProvider} from '@site/lib/site-context';
|
||||
import {MOONLIGHT_STAY_PAYLOAD} from '@site/fixtures/moonlight-stay';
|
||||
import {WeatherSection} from './WeatherSection';
|
||||
import type {WeatherSnapshot} from '@o2o/shared';
|
||||
|
||||
function render(weather?: WeatherSnapshot) {
|
||||
const payload = structuredClone(MOONLIGHT_STAY_PAYLOAD);
|
||||
payload.local.weather = weather;
|
||||
return renderToStaticMarkup(<SiteProvider payload={payload}><WeatherSection /></SiteProvider>);
|
||||
}
|
||||
|
||||
it('uses the first sky and temperature lines for deterministic HTML', () => {
|
||||
const weather = {temperature: 30, observedAt: '2026-09-15T12:00', condition: '구름많음', noteSets: {'구름많음': ['구름 첫 줄', '다음 줄']}, tempNoteSets: {'혹서': ['더위 첫 줄']}};
|
||||
expect(render(weather)).toContain('구름 첫 줄');
|
||||
expect(render(weather)).toContain('더위 첫 줄');
|
||||
expect(render(weather)).not.toContain('다음 줄');
|
||||
expect(render(weather)).toBe(render(weather));
|
||||
});
|
||||
|
||||
it('preserves legacy single notes', () => {
|
||||
const html = render({temperature: 15, observedAt: '2026-09-15T12:00', condition: '비', notes: {'비': '기존 비 안내'}, tempNotes: {'쌀쌀': '겉옷 안내'}});
|
||||
expect(html).toContain('기존 비 안내');
|
||||
expect(html).toContain('겉옷 안내');
|
||||
});
|
||||
|
||||
it('does not invent weather when an observation is missing', () => {
|
||||
expect(render()).toBe('');
|
||||
});
|
||||
Reference in new issue
Block a user