import { useState, useEffect, useCallback } from 'react'; import { MonitorState, LogEntry } from './types.js'; import { makeLog } from './factory.js'; import { useFetchData } from './use-fetch-data.js'; import { useCommandHandler } from './use-command-handler.js'; const PULSE_INTERVAL = 12000; export function useMonitorLogic(projectId: string) { const [state, setState] = useState({ projectId, apiUrl: '', activeTask: null, roadmap: [], daemonOnline: false, logs: [ makeLog('info', 'Initializing Rigstate Monitor v2.0...'), makeLog('info', `Project: ${projectId.substring(0, 8)}...`), ], stats: { total: 0, completed: 0, active: 0, blocked: 0 }, loading: true, time: new Date().toLocaleTimeString(), }); const [command, setCommand] = useState(''); const addLog = useCallback((level: LogEntry['level'], message: string) => { setState(prev => ({ ...prev, logs: [...prev.logs.slice(-24), makeLog(level, message)] })); }, []); const fetchData = useFetchData(addLog, setState); const { handleSubmit, pendingDestructive } = useCommandHandler(addLog, fetchData, state, setCommand); useEffect(() => { fetchData(); const dataTimer = setInterval(fetchData, PULSE_INTERVAL); const timeTimer = setInterval(() => { setState(prev => ({ ...prev, time: new Date().toLocaleTimeString() })); }, 1000); return () => { clearInterval(dataTimer); clearInterval(timeTimer); }; }, [fetchData]); return { state, command, setCommand, handleCommandSubmit: handleSubmit, pendingDestructive }; }