import { Link, useNavigate, useRouterState } from "@tanstack/react-router"; import { useCallback, useEffect, useId, useMemo, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { Icon } from "@/components/icons"; import { ConfirmDialog } from "@/components/shared/confirm-dialog"; import { RelativeTime } from "@/components/shared/relative-time"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Modal } from "@/components/ui/dialog"; import { KV } from "@/components/ui/kv"; import { StatusDot } from "@/components/ui/status-dot"; import { toast } from "@/components/ui/toast"; import { ConnectHero } from "@/features/connect/hero"; import { ManualConnectForm, pairErrorMessage, type ManualConnectInput } from "@/features/connect/manual-form"; import { clearPairMeta, isPairCode, maskSecret, normalizePairCode, parseEndpoint, readPairMeta, redeemPairing, safePairNext, scopesForIdentity, writePairMeta, type PairMeta, type PairTarget, } from "@/features/connect/pairing"; import { ScanDialog } from "@/features/connect/scan-dialog"; import { deriveServerIdentity, formatUptime, type InfoExtras } from "@/features/status/server"; import { useInfoQuery } from "@/features/status/queries"; import { getDeviceToken, setDeviceToken } from "@/lib/api"; import { useConnectionStore } from "@/stores/connection"; import "@/styles/pages/connect.css"; function scopeTone(scope: string): "default" | "info" | "ok" { if (scope === "read") return "default"; if (scope === "steer") return "info"; if (scope === "apply") return "ok"; return "default"; } /** * Port default for the manual form. The loaded daemon info wins; otherwise the * port this console was served from, else the scheme default. Never a literal. */ function defaultPortFor(infoPort: number | null | undefined): string { if (typeof infoPort === "number" && Number.isFinite(infoPort) && infoPort > 0) return String(infoPort); if (typeof window === "undefined") return ""; const { port, protocol } = window.location; if (port) return port; return protocol === "https:" ? "443" : "80"; } export function ConnectPage() { const { t } = useTranslation(); const navigate = useNavigate(); const searchStr = useRouterState({ select: (state) => state.location.searchStr }); const search = useMemo(() => new URLSearchParams(searchStr), [searchStr]); const next = useMemo(() => safePairNext(search.get("next")), [search]); const codeParam = useMemo(() => { const raw = normalizePairCode(search.get("code")); return isPairCode(raw) ? raw : ""; }, [search]); const refreshFromStorage = useConnectionStore((state) => state.refreshFromStorage); const paired = useConnectionStore((state) => state.paired); const connect = useConnectionStore((state) => state.connect); const disconnect = useConnectionStore((state) => state.disconnect); const storedEndpoint = useConnectionStore((state) => state.endpoint); const storedDeviceName = useConnectionStore((state) => state.deviceName); const connectedAt = useConnectionStore((state) => state.connectedAt); const wsState = useConnectionStore((state) => state.wsState); const storedServer = useConnectionStore((state) => state.server); const storedScopes = useConnectionStore((state) => state.scopes); const [meta, setMeta] = useState(() => readPairMeta()); const [scanOpen, setScanOpen] = useState(false); const [pending, setPending] = useState(null); const [pairError, setPairError] = useState(null); const [redeeming, setRedeeming] = useState(false); /** Manual pairing form: collapsed by default in both states. */ const [manualOpen, setManualOpen] = useState(() => codeParam !== ""); /** Connected state: the re-pair panel expands in place. */ const [repairOpen, setRepairOpen] = useState(false); const [repairManualOpen, setRepairManualOpen] = useState(false); /** `/connect?…&auto=1` (from a scanned `/pair/` link): pair with no input. */ const autoParam = useMemo(() => { const raw = search.get("auto"); return raw === "1" || raw === "true"; }, [search]); const originTarget = useMemo(() => { if (typeof window === "undefined") return null; const parsed = parseEndpoint(window.location.origin, ""); return "error" in parsed ? null : parsed; }, []); const autoAttempted = useRef(false); const [autoPairing, setAutoPairing] = useState(() => autoParam && codeParam !== "" && !paired); const [autoError, setAutoError] = useState(null); const [disconnectOpen, setDisconnectOpen] = useState(false); const hostInputRef = useRef(null); const formId = useId(); useEffect(() => { refreshFromStorage(); }, [refreshFromStorage]); useEffect(() => { const title = t("connect.title"); document.title = `${title} — pi2.nvim`; }, [t]); const info = useInfoQuery(paired); const server = deriveServerIdentity({ info: info.data as InfoExtras | undefined, endpoint: storedEndpoint, wsState, fetchedAt: info.dataUpdatedAt || null, nameFallback: storedServer?.name || meta?.server?.name, }); const scopes = scopesForIdentity(meta, info.data, storedScopes); const defaultDeviceName = t("connect.defaultDeviceName"); const osLabel = info.data?.os || info.data?.platform || server.os || "—"; const uptimeSeconds = typeof info.data?.uptimeMs === "number" ? info.data.uptimeMs / 1000 : server.uptimeSec; const defaultPort = defaultPortFor(info.data?.port ?? server.port); /** * One pairing path for the scan/confirm dialog and the manual form: * redeem against the public endpoint, persist token + scopes, then follow * the allow-listed `next`. */ const finishPairing = useCallback( async (target: PairTarget, deviceName: string) => { const result = await redeemPairing({ endpoint: target.endpoint, code: target.code, deviceName, }); setDeviceToken(result.token); connect({ endpoint: target.endpoint, deviceName, token: result.token, scopes: result.scopes }); const nextMeta: PairMeta = { scopes: result.scopes, deviceName, kind: result.kind, server: result.server, pairedAt: Date.now(), }; writePairMeta(nextMeta); setMeta(nextMeta); toast.success(t("connect.connected", { name: result.server?.name || target.name })); navigate({ href: next }); }, [connect, navigate, next, t], ); /** * A pairing link carries everything the form would ask for: the endpoint is * the origin the page was loaded from (host + port), the code is in the URL, * and the device name is the localized default. Redeem it immediately; only a * rejected/expired code falls back to the prefilled form. */ useEffect(() => { if (!autoParam || !codeParam || paired || originTarget === null || autoAttempted.current) return; autoAttempted.current = true; setAutoPairing(true); setAutoError(null); void (async () => { try { await finishPairing( { endpoint: originTarget.endpoint, code: codeParam, name: originTarget.hostname, hostname: originTarget.hostname, port: originTarget.port, }, defaultDeviceName, ); } catch (error) { const message = t(pairErrorMessage(error)); setAutoError(message); toast.error(message); setAutoPairing(false); } })(); }, [autoParam, codeParam, paired, originTarget, finishPairing, defaultDeviceName, t]); async function handleManual(input: ManualConnectInput) { let hostname = ""; let port = originTarget?.port ?? 0; try { const url = new URL(input.endpoint); hostname = url.hostname; port = url.port ? Number(url.port) : url.protocol === "https:" ? 443 : 80; } catch { /* validate host/port above guarantees a parseable endpoint */ } await finishPairing( { endpoint: input.endpoint, code: input.code, name: hostname, hostname, port }, input.deviceName, ); } const handleFound = useCallback((target: PairTarget) => { setPairError(null); setPending(target); }, []); async function confirmPending() { if (!pending || redeeming) return; setRedeeming(true); setPairError(null); try { await finishPairing(pending, defaultDeviceName); setPending(null); } catch (error) { const message = t(pairErrorMessage(error)); setPairError(message); toast.error(message); } finally { setRedeeming(false); } } function handleDisconnect() { disconnect(); clearPairMeta(); setMeta(null); setRepairOpen(false); setManualOpen(false); toast.success(t("connect.disconnected")); } function toggleRepair() { setRepairOpen((open) => { const nextOpen = !open; // Collapsing also collapses the form so re-opening is never "half open". if (!nextOpen) setRepairManualOpen(false); return nextOpen; }); } function renderManualForm(expanded: boolean, primary: boolean) { return ( ); } function manualToggle(expanded: boolean, onToggle: () => void) { return ( ); } /** Connected: summary + back (primary by default) / re-pair / disconnect. */ function connectedPanel() { return (

{server.name || "—"}

{t("connect.statePaired")}
{info.data?.hostname || server.hostname || "—"} {storedEndpoint || server.endpoint || "—"} {info.data?.version || meta?.server?.version || "—"} {storedDeviceName || meta?.deviceName || "—"} {scopes.length ? ( {scopes.map((scope) => ( {t(`connect.scope.${scope}`, { defaultValue: scope })} ))} ) : ( — )}
{t("connect.backToWorkspaces")}
{t("connect.details", { defaultValue: "Connection details" })}
{maskSecret(getDeviceToken() ?? "") || "—"} {info.data?.pluginVersion || meta?.server?.pluginVersion || "—"} {osLabel} {formatUptime(uptimeSeconds)}

{t("connect.secureNote")}

{t("connect.scanWarning")}

); } /** Unconnected: scan-to-pair is the single primary until the form opens. */ function unconnectedPanel() { return (

{t("connect.emptyTitle")}

{t("connect.emptyText")}

{autoError ? (
{autoError}
) : null}
{manualToggle(manualOpen, () => setManualOpen((open) => !open))}
{renderManualForm(manualOpen, manualOpen)}
); } return (
{paired ? ( connectedPanel() ) : autoPairing ? (

{t("connect.pairingAuto")}

{t("connect.connecting")}

) : ( unconnectedPanel() )}
{ if (!open && !redeeming) setPending(null); }} title={t("connect.confirmTitle")} footer={ <> } > {pending ? (

{t("connect.scanLead")}

{pending.name || pending.hostname} {pending.hostname}:{pending.port} {maskSecret(pending.code)}
{t("connect.scanWarning")}
{pairError ? (
{pairError}
) : null}
) : null}
); }