import { useState, useCallback } from 'react'; export interface MonitorState { currentTime: number; // always Date.now() historyMinutes: number; // how far back to show (default 60) } export function useMonitor() { const [state, setState] = useState({ currentTime: Date.now(), historyMinutes: 60, }); // LIVE 모드에서 1초 tick 제거 — 데이터 polling 응답 시에만 갱신하면 충분. // currentTime은 표시용으로 렌더 시 Date.now() 사용, 데이터 훅에는 전달하지 않음. const setHistoryMinutes = useCallback((minutes: number) => { setState(prev => ({ ...prev, historyMinutes: minutes })); }, []); /** 데이터 갱신 시점에 호출하여 currentTime 동기화 */ const refreshTime = useCallback(() => { setState(prev => ({ ...prev, currentTime: Date.now() })); }, []); const startTime = state.currentTime - state.historyMinutes * 60_000; const endTime = state.currentTime; return { state, startTime, endTime, setHistoryMinutes, refreshTime }; }