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;
|
||||
}
|
||||
Reference in new issue
Block a user