// SPDX-License-Identifier: MIT // Copyright (C) 2026 Pooya Parsa // Tailscale in the top bar, as part of the Network applet // (applets/Network.tsx: its mark in #tb-net, this section in #tb-net-panel). // - tailscaleBar(): what the bar shows of it. The mark only while it is set // up and not off (its shape and tone: tailscale.ts tailscaleKind()); its // short state for the tooltip (tailscaleShort()). Off, not set up or // without the network, the bar shows nothing of it. // - TailscaleSection (#tb-net-ts): off and not set up, or without the // network, one compact row: the faint mark, "Tailscale", the state pill // (#tb-ts-state: Off, or Unavailable) and Set up… (#tb-ts-settings: // Settings, Network, at the Tailscale section; Network settings… without // the network). Else, top to bottom: // - the mark (its shape says the state), "Tailscale" and the state pill // (#tb-ts-state, its word in the state's tone; "Internet blocked" while // connected with an exit node it cannot use); // - what the state means (tailscale.ts tailscaleLine()), or once connected // a label/value grid: IPv4, IPv6, Name, Tailnet, Home relay, Login server // (and Peers while no device is listed); while a login waits, its link. // Values are selectable text (the panel is select-none otherwise), mono, // truncated with the whole value as their title; the addresses, the name, // the tailnet and the login link each have a copy button // (#tb-ts-copy-) that keeps the panel open, shows a check for a // moment and says "Copied …" to assistive tech; // - the devices (#tb-ts-devices), once connected: "Devices" with how many // are online, then each of the tailnet's other devices // (#tb-ts-device-) with a dot for online, its short name, what // it offers (exit node, subnet routes) and its IPv4 address; // - the exit node (#tb-ts-exit), while a node runs this load and there is a // choice or a peer that offers itself: "Exit node", what it does now // (#tb-ts-exit-state: through it, or in the attention tone that internet // traffic is blocked and why, tailscale.ts exitLine()), then the choices // as menu rows with a check on the current one: None (#tb-ts-exit-none), // each listed exit node (#tb-ts-exit-: its name, address, // offline) and a chosen one the list lacks (#tb-ts-exit-chosen). A row // switches at once (tailscale.ts setExitNode(), which saves it for the // next boot) and the panel stays, to show the new state; // - the last error control or DERP reported, if any; // - its actions: Tailscale settings… (#tb-ts-settings, quiet: Settings, // Network, at the Tailscale section) on the left; on the right Log out… // (#tb-ts-logout, quiet and red: it asks first, tailscale.ts // forgetDevice()) while logged in, and the state's action as the primary // button, which a keyboard open focuses (data-autofocus): Log in… // (#tb-ts-login), or Ignore and Enable and reboot… (#tb-ts-ignore, // #tb-ts-accept) for a link's settings. // Server-supplied strings are text nodes. Nothing of the Tailscale client // loads for it (types and tailscale.ts only). import { useEffect, useRef, useState } from "react"; import { KW } from "../kw.ts"; import { useStore, type TailscaleView } from "../store.ts"; import { openSection } from "../sysapps.ts"; import { acceptTailscaleLink, exitBlocked, exitCandidate, exitLine, exitShort, exitValue, forgetDevice, ignoreTailscaleLink, openLogin, setExitNode, tailscaleKind, tailscaleLine, tailscaleShort, tailscaleWord, type TailscaleKind } from "../tailscale.ts"; import type { MenuItem } from "./Menu.tsx"; import { CheckIcon, CopyIcon, TailscaleMark } from "./icons.tsx"; /** What the top bar shows of Tailscale (the top of this file). */ export interface TailscaleBar { /** Its kind (tailscaleKind()), "off" while not set up or without the network. */ kind: TailscaleKind; /** The option is off this load and nothing asks for it. */ unset: boolean; /** The guest has no network this boot (and no link asks to turn Tailscale on). */ noNet: boolean; /** Its mark shows in the bar: set up, with the network, and not off. */ shown: boolean; /** Its short state, for the bar item's tooltip. */ short: string; } export function tailscaleBar(v: TailscaleView): TailscaleBar { const cfg = KW.config; const unset = !cfg.tailscaleParam && v.state === "off"; const noNet = !cfg.net && v.state !== "ask"; const kind = noNet || unset ? "off" : tailscaleKind(v); return { kind, unset, noNet, shown: kind !== "off", short: noNet ? "unavailable, the network is off" : unset ? "off" : tailscaleShort(v) }; } /** The state as the pill says it (with the kind: its tone; the mark's shape says it too). */ function pill(v: TailscaleView, b: TailscaleBar): { word: string; kind: TailscaleKind } { if (b.noNet) return { word: "Unavailable", kind: "off" }; if (b.unset) return { word: "Off", kind: "off" }; return { word: v.state === "running" && exitBlocked(v) ? "Internet blocked" : tailscaleWord(v), kind: b.kind }; } const TONE: Record = { ok: "text-ok border-ok/40", pending: "text-muted border-ink/15", attention: "text-warn border-warn/45", error: "text-err border-err/45", off: "text-faint border-ink/12", }; /** * Puts `text` on the clipboard from the click: the Clipboard API, else (no * secure context) a hidden text area and execCommand("copy"), as Copy * diagnostics falls back to. False where neither works. */ async function copyText(text: string): Promise { if (navigator.clipboard?.writeText && isSecureContext) return navigator.clipboard.writeText(text).then(() => true, () => false); const prev = document.activeElement as HTMLElement | null; const ta = document.createElement("textarea"); ta.value = text; ta.setAttribute("readonly", ""); ta.style.cssText = "position:fixed;top:0;left:0;opacity:0;pointer-events:none"; document.body.append(ta); ta.select(); let ok = false; try { ok = document.execCommand("copy"); } catch {} ta.remove(); prev?.focus({ preventScroll: true }); return ok; } interface Row { key: string; label: string; value: string; copy?: boolean } /** A row's name in "Copy …" and "Copied …" (IPv4 and IPv6 as written, the others in lower case). */ const said = (r: Row) => (r.label.startsWith("IPv") ? r.label : r.label.toLowerCase()); /** The rows the state has (none before it is connected, but a waiting login's link); Peers only while no device is listed (the devices' heading counts them). */ function rows(v: TailscaleView): Row[] { if (v.state === "needs-login" && v.authURL) return [{ key: "login", label: "Login link", value: v.authURL, copy: true }]; if (v.state !== "running") return []; const v4 = v.addresses.filter((a) => !a.includes(":")), v6 = v.addresses.filter((a) => a.includes(":")); const tailnet = v.name.includes(".") ? v.name.slice(v.name.indexOf(".") + 1) : ""; return ([ ...v4.map((a, i) => ({ key: i ? `ipv4-${i}` : "ipv4", label: "IPv4", value: a, copy: true })), ...v6.map((a, i) => ({ key: i ? `ipv6-${i}` : "ipv6", label: "IPv6", value: a, copy: true })), { key: "name", label: "Name", value: v.name, copy: true }, { key: "tailnet", label: "Tailnet", value: tailnet, copy: true }, { key: "relay", label: "Home relay", value: v.relay }, { key: "peers", label: "Peers", value: v.devices.length ? "" : `${v.peers} (${v.online} online)` }, { key: "server", label: "Login server", value: v.server }, ] as Row[]).filter((r) => r.value); } /** The node runs (or starts) this load: its exit node can be switched. */ const LIVE: readonly TailscaleView["state"][] = ["idle", "starting", "needs-login", "needs-machine-auth", "connecting", "running"]; /** A small heading inside the section, with an optional note on the right. */ function Heading({ label, note, noteClass = "text-faint" }: { label: string; note?: string; noteClass?: string }) { return (
{label} {note && {note}}
); } /** One exit node choice, a menu row with a check on the current one; it switches at once and the panel stays. */ function ExitRow({ id, on, label, detail, onPick }: { id: string; on: boolean; label: string; detail?: string; onPick: () => void }) { return ( ); } /** The tailnet's other devices: a dot for online, the short name, what it offers (exit node, routes) and its address. */ function Devices({ v }: { v: TailscaleView }) { return (
    {v.devices.map((d) => { const subnets = d.routes.filter((r) => !r.endsWith("/0")); const offers = [d.exitNode ? "exit node" : "", subnets.length ? subnets.join(", ") : ""].filter(Boolean).join(" · "); return (
  • ); })}
); } /** The exit node: what it does now, and the choices (the top of this file). */ function ExitNode({ v }: { v: TailscaleView }) { const sel = v.exitNode; const chosen = sel ? exitCandidate(v, sel.selected) : undefined; const blocked = exitBlocked(v); const state = sel ? exitLine(v) : "none: the guest's internet goes through Wisp"; return (
Exit node {state.charAt(0).toUpperCase() + state.slice(1)}
setExitNode("")} /> {v.exitNodes.map((n) => ( setExitNode(exitValue(n))} /> ))} {sel && !chosen && setExitNode(sel.selected)} />}
); } /** Settings, Network, scrolled to the Tailscale section. */ export function openTailscaleSettings(): void { openSection("network"); requestAnimationFrame(() => document.getElementById("opt-tailscale")?.closest("form")?.scrollIntoView({ block: "nearest" })); } /** Tailscale's part of the Network panel (the top of this file); `pick` closes the panel and runs the item. */ export function TailscaleSection({ pick }: { pick: (it: MenuItem) => void }) { const v = useStore((s) => s.tailscale); const b = tailscaleBar(v); const p = pill(v, b); const compact = b.noNet || b.unset; const list = compact ? [] : rows(v); const [copied, setCopied] = useState(null); const timer = useRef>(undefined); useEffect(() => () => clearTimeout(timer.current), []); const copy = async (r: Row) => { if (!await copyText(r.value)) { KW.ui?.toast(`Could not copy the ${said(r)}: the clipboard is not available here; select the text instead`); return; } setCopied(r); clearTimeout(timer.current); timer.current = setTimeout(() => setCopied(null), 1500); }; const head = ( <> Tailscale {p.word} ); if (compact) { return (
{head}
); } const text = v.state === "running" ? null : tailscaleLine(v); const loggedIn = v.state === "running" || v.state === "connecting" || v.state === "needs-machine-auth"; const url = v.authURL; return (
{head}
{text &&

{text}

} {list.length > 0 && (
{list.map((r) => (
{r.label}
{r.value}
{r.copy && ( )}
))}
)} {v.state === "running" && v.devices.length > 0 && } {LIVE.includes(v.state) && (v.exitNode || v.exitNodes.length > 0) && } {v.error && v.state !== "failed" && v.state !== "blocked" && (

{v.error}

)} {copied ? `Copied ${said(copied)}` : ""}
{loggedIn && } {v.state === "needs-login" && url && } {v.state === "ask" && <> }
); }