import { useState } from 'react' import { api } from './api' import { Btn, Spinner, useEscape } from './ui' // Header power control: a small button, a dialog that needs the hostname typed back. // `label` is what the user types (the machine name on a remote); `host` is what the node checks. export default function Power({ host, label, rebootNeeded }: { host: string; label: string; rebootNeeded?: boolean }) { const [open, setOpen] = useState(false) const [typed, setTyped] = useState('') const [sent, setSent] = useState<'' | 'reboot' | 'poweroff'>('') useEscape(() => { if (!sent) setOpen(false) }, open) const [err, setErr] = useState('') const ok = typed.trim() === label async function go(action: 'reboot' | 'poweroff') { setErr('') try { await api.power(action, host); setSent(action) } catch (e) { setErr(e instanceof Error ? e.message : 'failed') } } return ( <> {open && (
!sent && setOpen(false)}>
e.stopPropagation()}>

Power · {label}

{rebootNeeded &&
A reboot is pending from package updates.
} {sent ? (
{sent === 'reboot' ? 'Rebooting. The console will reconnect when the host is back.' : 'Shutting down. Power it back on by hand.'}
) : ( <> {err &&
{err}
}
go('reboot')}>Reboot go('poweroff')}>Shut down
)}
)} ) }