30 lines
1.0 KiB
TypeScript
30 lines
1.0 KiB
TypeScript
import { useState, useEffect } from "react";
|
|
import {
|
|
resolveDomesticKisSession,
|
|
type DomesticKisSession,
|
|
} from "@/lib/kis/domestic-market-session";
|
|
|
|
// 클라이언트 환경에서 현재 시간을 기준으로 세션을 계산합니다.
|
|
// 매 30초마다 갱신됩니다.
|
|
|
|
/**
|
|
* @description 국내 주식 시장의 세션(장 운영 상태)을 관리하는 훅입니다.
|
|
* client-side에서 로컬 스토리지를 확인하거나 기본 세션을 반환하며,
|
|
* 30초마다 세션 정보를 갱신하여 장 시작/마감/시간외 단일가 등 상태 변화를 감지합니다.
|
|
*/
|
|
export function useDomesticSession() {
|
|
const [marketSession, setMarketSession] = useState<DomesticKisSession>(() =>
|
|
resolveDomesticKisSession(),
|
|
);
|
|
|
|
useEffect(() => {
|
|
const timerId = window.setInterval(() => {
|
|
const nextSession = resolveDomesticKisSession();
|
|
setMarketSession((prev) => (prev === nextSession ? prev : nextSession));
|
|
}, 30_000);
|
|
return () => window.clearInterval(timerId);
|
|
}, []);
|
|
|
|
return marketSession;
|
|
}
|