Merge branch 'main' of https://gitea.o2o.kr/Web4ai/o2o-site-AEO
This commit is contained in:
commit
4cd756108d
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