import { useEffect, useRef, useState } from 'react' import { MachineStatus, titleOf } from './api' import { useEscape } from './ui' import { accentFor } from './accents' const hostOf = (url: string) => { try { return new URL(url).hostname } catch { return url } } // The host picker at the top of the rail. The hub is always first; managed machines follow // with their live status. Picking one re-points the whole console at it. export default function MachineSwitcher({ hub, hubAddr, current, machines, onChange, onManage }: { hub: string; hubAddr?: string; current: string | null; machines: MachineStatus[]; onChange: (name: string | null) => void; onManage: () => void }) { const [open, setOpen] = useState(false) const box = useRef(null) useEffect(() => { const h = (e: MouseEvent) => { if (box.current && !box.current.contains(e.target as Node)) setOpen(false) } window.addEventListener('click', h) return () => window.removeEventListener('click', h) }, []) useEscape(() => setOpen(false), open) const cur = current ? machines.find((m) => m.name === current) : null const title = (name: string | null) => titleOf(name, machines, hub) const dotFor = (m?: MachineStatus | null) => (!m ? 'ok' : m.online ? 'ok' : 'bad') return (
{open && (
{machines.map((m) => ( ))}
)}
) }