import { useState, useCallback } from 'react'; import { LogEntry, MonitorState } from './types.js'; import { makeLog, parseNlCommand } from './factory.js'; export function useCommandHandler( addLog: (level: LogEntry['level'], msg: string) => void, fetchData: () => Promise, state: MonitorState, setCommand: (v: string) => void ) { const [pendingDestructive, setPendingDestructive] = useState(null); const handleSubmit = useCallback((value: string) => { const trimmed = value.trim(); if (!trimmed) return; setCommand(''); if (pendingDestructive) { if (trimmed.toLowerCase() === 'yes') { addLog('warn', `EXEC: ${pendingDestructive}`); } else { addLog('info', 'Aborted. Command cancelled.'); } setPendingDestructive(null); return; } addLog('command', `> ${trimmed}`); const cmd = parseNlCommand(trimmed); if (cmd.type === 'status') { addLog('frank', `Frank: Daemon ${state.daemonOnline ? 'ONLINE' : 'OFFLINE'} | ${state.stats.completed}/${state.stats.total} done`); } else if (cmd.type === 'refresh') { addLog('info', 'Pulse refresh triggered...'); fetchData(); } else if (cmd.type === 'navigate') { addLog('info', `Navigate: ${cmd.target}`); } else if (cmd.type === 'antidote_stats') { addLog('hive', `Hive: ${state.hiveStats?.total_antidotes || 0} signals, ${state.hiveStats?.fortress_rules || 0} fortress laws.`); addLog('info', `Avg Trust: ${state.hiveStats?.average_trust_score || 0}/100`); } else if (cmd.type === 'antidote_query') { addLog('hive', `Hive: Searching for "${cmd.query}"...`); addLog('info', `Try running 'rigstate antidote query "${cmd.query}"' for details.`); } else if (cmd.type === 'confirm_destructive') { addLog('warn', `⚠️ SECURE: "${trimmed}" — type 'yes' to confirm.`); setPendingDestructive(trimmed); } else { addLog('frank', `Frank: Processing "${trimmed}"...`); } }, [addLog, fetchData, pendingDestructive, state, setCommand]); return { handleSubmit, pendingDestructive }; }