import { h, Fragment } from 'preact'; import { useState, useEffect } from 'preact/hooks'; import { wsClient } from './lib/ws'; import { controlDialogs, dismissControlDialog, type ControlDialog } from './lib/controldialogs'; // Renders dialogs pushed by server-side control-logic action.dialog nodes over // the WebSocket. Mounted once globally (App) since these are addressed to the // connected user, independent of which panel is open. info/error dialogs are // acknowledgements; input dialogs send a numeric response back to the server. export default function ControlDialogs() { const [reqs, setReqs] = useState([]); useEffect(() => controlDialogs.subscribe(setReqs), []); if (reqs.length === 0) return null; return {reqs.map(r => )}; } function ControlDialogModal({ req }: { req: ControlDialog; key?: string }) { const isInput = req.kind === 'input'; const [val, setVal] = useState(''); function ok() { if (isInput) { const n = parseFloat(val); wsClient.sendDialogResponse(req.id, Number.isFinite(n) ? n : null); } dismissControlDialog(req.id); } function cancel() { if (isInput) wsClient.sendDialogResponse(req.id, null); dismissControlDialog(req.id); } function onKey(e: KeyboardEvent) { if (e.key === 'Enter') { e.preventDefault(); ok(); } if (e.key === 'Escape') { e.preventDefault(); cancel(); } } return (
); }