Something went wrong. Try again.
An experimental port of NetBSD, FreeBSD, OpenBSD, and XNU to unkernel unkernel.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111// SPDX-License-Identifier: MIT// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>// What the device applets share (Bluetooth.tsx, Serial.tsx, Usb.tsx: the// guest's devices through Web Bluetooth, Web Serial and WebUSB):// - Count: the number next to a bar item's icon (devices attached or// connected), in the item's colour, at the bar's text size;// - DeviceList and DeviceRow: the panel's devices, one card each (a dot in// its state's colour, the label, the guest's side on the right in mono,// then what it is doing and its actions), roomy for touch;// - RowIcon and RowButton: a row's actions, quiet (no fill until hovered),// the same height: an icon with a tooltip and an aria-label (Terminal),// or short text (Disconnect, Forget);// - Empty: the line a panel shows with no device;// - keepFocus(): a row's action that removes the row (Disconnect, Forget)// leaves the keyboard focus in the panel (on its primary action, else its// first button) once the row is gone; only when the focus was in the// panel (a mouse press, as on a panel opened by hover, leaves it in the// terminal, and it stays there), and not once the panel has closed.// Device-supplied strings (labels, names) are text nodes.import type { MouseEvent, ReactNode } from "react";
/** A row's state: the dot's colour. */export type DeviceTone = "ok" | "idle" | "warn" | "err";
const DOT: Record<DeviceTone, string> = { ok: "bg-ok", idle: "bg-ink/30", warn: "bg-warn", err: "bg-err" };const DETAIL: Record<DeviceTone, string> = { ok: "text-muted", idle: "text-faint", warn: "text-warn", err: "text-err" };
/** The count next to a bar item's icon. */export const Count = ({ n }: { n: number }) => ( <span aria-hidden="true" className="-ml-0.5 font-sans text-[12px] leading-none font-semibold tabular-nums">{n}</span>);
export function DeviceList({ id, label, children }: { id: string; label: string; children: ReactNode }) { return <ul id={id} aria-label={label} className="flex max-h-[min(50dvh,22rem)] flex-col gap-1.5 overflow-y-auto px-3 py-2">{children}</ul>;}
/** One device: the dot, the label, the guest's side (`where`), then what it is doing and its actions. */export function DeviceRow({ id, tone, label, where, detail, children }: { id: string; tone: DeviceTone; label: string; where?: string; detail?: string; children?: ReactNode;}) { return ( <li id={id} className="flex flex-col gap-1.5 rounded-lg bg-ink/[0.05] px-3 py-2.5 font-sans"> <div className="flex min-w-0 items-center gap-2"> <span aria-hidden="true" className={`size-2 flex-none rounded-full ${DOT[tone]}`} /> <span className="min-w-0 flex-1 truncate text-[12.5px] text-fg" title={label}>{label}</span> {where && <code className="flex-none font-mono text-[11.5px] text-muted select-text">{where}</code>} </div> {(detail || children) && ( <div className="flex min-w-0 flex-wrap items-center gap-x-3 gap-y-1.5 pl-4"> {detail && <span className={`min-w-0 flex-1 basis-32 text-[11.5px] leading-snug break-words ${DETAIL[tone]}`}>{detail}</span>} {children && <span className="-my-1 -mr-1.5 ml-auto flex flex-none items-center gap-0.5">{children}</span>} </div> )} </li> );}
const QUIET = "flex-none rounded-md text-muted transition-colors enabled:hover:bg-ink/10 enabled:hover:text-fg focus-visible:bg-ink/10 focus-visible:text-fg focus-visible:outline-none disabled:opacity-40";
/** A row's action as quiet text (Disconnect, Forget), as tall as RowIcon; touch-sized on coarse pointers. */export function RowButton({ id, title, disabled, onClick, children }: { id: string; title: string; disabled?: boolean; onClick: (e: MouseEvent<HTMLButtonElement>) => void; children: ReactNode;}) { return ( <button id={id} type="button" title={title} disabled={disabled} onClick={onClick} className={`${QUIET} h-7 px-2 font-sans text-[12px] pointer-coarse:h-9 pointer-coarse:px-3`}> {children} </button> );}
/** A row's action as an icon (Terminal): `label` is its name for assistive tech, `title` the tooltip. */export function RowIcon({ id, label, title, disabled, onClick, children }: { id: string; label: string; title: string; disabled?: boolean; onClick: (e: MouseEvent<HTMLButtonElement>) => void; children: ReactNode;}) { return ( <button id={id} type="button" title={title} aria-label={label} disabled={disabled} onClick={onClick} className={`${QUIET} grid size-7 place-items-center pointer-coarse:size-9`}> <span aria-hidden="true" className="flex">{children}</span> </button> );}
/** The line a panel shows with no device (or why there is none yet). */export const Empty = ({ id, children }: { id: string; children: ReactNode }) => ( <p id={id} className="px-4 py-3 font-sans text-[12px] leading-snug text-muted">{children}</p>);
/** * Runs `act` (which removes the row the click came from) and, when that * click left the focus inside the panel `panelId` (from the keyboard: a * mouse press keeps it where it was), puts it back on the panel's * `[data-autofocus]` button, else its first, once the row is gone (the * state modules publish at most every 200 ms). */export function keepFocus(panelId: string, act: () => Promise<unknown>): void { const box = document.getElementById(panelId); const had = !!box && box.contains(document.activeElement); void act().finally(() => { if (!had) return; setTimeout(() => { const p = document.getElementById(panelId); if (!p || p.contains(document.activeElement)) return; (p.querySelector<HTMLButtonElement>("button[data-autofocus]:not(:disabled)") ?? p.querySelector<HTMLButtonElement>("button:not(:disabled)"))?.focus({ preventScroll: true }); }, 260); });}
/** A `GattDeviceInfo.id` (Web Bluetooth's base64 id) as an id's part. */export const idPart = (s: string) => s.replace(/[^A-Za-z0-9_-]/g, "_");