feat(web): zero-input pairing, a real QR, and no scanner or scopes

The console side of ADR 0001: a scanned link pairs itself, the status page
draws the daemon's actual symbol, and the second entry point for pairing
(the in-page camera scanner) is gone.

- `POST /api/v1/pair/redeem` replaced the dual-purpose `/api/v1/pair`; the
  redeem response is parsed strictly as `{token, device}` (the five-layer
  alias fallbacks for `deviceToken`/`payload.device`/… are deleted).
- `features/status/qr.ts` no longer invents "a deterministic fake QR from a
  seed" — it renders `pairCode.qr.rows` module by module (devicePixelRatio
  aware, square, token colours, redraws on theme/resize), and a mint without a
  matrix shows the link instead of a pattern that will not scan.
- `/pair/<CODE>` (daemon 302 or the SPA route) lands on
  `/connect?code=…&auto=1`, which redeems against its own origin with the
  localized device name and then navigates to `next`/`/workspaces`, dropping
  the code from the URL; a rejected code falls back to the prefilled form.
- The camera scanner (`features/connect/scan-dialog.tsx`) and its i18n keys are
  removed: phones scan with the system camera, desktops type the code. Paste a
  `pi2://pair?…` link or `/pair/CODE` still fills the form.
- Scope tracking disappears from the store, API types, device list and pairing
  metadata (`pi2-device-scopes` is no longer written or read).
- Stale default removed: link parsing falls back to the served origin's port,
  then 443/80, instead of a hard-coded 4321.
- `scripts/ui-{fixtures,mock-server,shots}.mjs` follow the new contract
  (mint carries `qr`/`bases`, redeem route, no scopes), so the visual harness
  exercises the real flow.

Verification: web typecheck clean, 299/299 tests, `vite build` green, visual
matrix 24/24 clean, and the i18n catalog has no missing keys.
This commit is contained in:
yuez committed 2026-10-27 10:44:54 +08:00
1 parent a57c874095
commit 3e35fb7ef3
20 files changed
+419 -946

No files matched your search

+12 -202
View File
@@ -7,7 +7,6 @@ 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";
@@ -22,12 +21,10 @@ import {
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";
@@ -35,13 +32,6 @@ 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.
@@ -74,19 +64,11 @@ export function ConnectPage() {
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<PairMeta | null>(() => readPairMeta());
const [scanOpen, setScanOpen] = useState(false);
const [pending, setPending] = useState<PairTarget | null>(null);
const [pairError, setPairError] = useState<string | null>(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. */
/** Connected state: the re-pair panel (the manual form) expands in place. */
const [repairOpen, setRepairOpen] = useState(false);
const [repairManualOpen, setRepairManualOpen] = useState(false);
/** `/connect?…&auto=1` (from a scanned `/pair/<CODE>` link): pair with no input. */
/** `/connect?…&auto=1` (from a `/pair/<CODE>` link): pair with no input. */
const autoParam = useMemo(() => {
const raw = search.get("auto");
return raw === "1" || raw === "true";
@@ -120,7 +102,6 @@ export function ConnectPage() {
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 =
@@ -128,9 +109,9 @@ export function ConnectPage() {
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`.
* One pairing path for the zero-input link and the manual form: redeem
* against the public endpoint, persist token + metadata, then follow the
* allow-listed `next`.
*/
const finishPairing = useCallback(
async (target: PairTarget, deviceName: string) => {
@@ -140,17 +121,15 @@ export function ConnectPage() {
deviceName,
});
setDeviceToken(result.token);
connect({ endpoint: target.endpoint, deviceName, token: result.token, scopes: result.scopes });
connect({ endpoint: target.endpoint, deviceName, token: result.token });
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 }));
toast.success(t("connect.connected", { name: target.name }));
navigate({ href: next });
},
[connect, navigate, next, t],
@@ -204,45 +183,14 @@ export function ConnectPage() {
);
}
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 (
<ManualConnectForm
@@ -260,28 +208,6 @@ export function ConnectPage() {
);
}
function manualToggle(expanded: boolean, onToggle: () => void) {
return (
<Button
variant="ghost"
className="cn-toggle"
aria-expanded={expanded}
aria-controls={formId}
aria-label={
expanded
? t("connect.collapseManual", { defaultValue: "Hide manual connection" })
: t("connect.expandManual", { defaultValue: "Show manual connection" })
}
onClick={onToggle}
>
<span className="cn-ico cn-toggle__chev">
<Icon name="chevron-down" />
</span>
<span>{t("connect.manual")}</span>
</Button>
);
}
/** Connected: summary + back (primary by default) / re-pair / disconnect. */
function connectedPanel() {
return (
@@ -303,24 +229,6 @@ export function ConnectPage() {
</KV>
<KV label={t("connect.version")}>{info.data?.version || meta?.server?.version || "—"}</KV>
<KV label={t("connect.device")}>{storedDeviceName || meta?.deviceName || "—"}</KV>
<KV label={t("connect.scopes")} className="cn-kv--badges">
{scopes.length ? (
<span className="cn-scopes">
{scopes.map((scope) => (
<Badge
key={scope}
className="badge--outline"
tone={scopeTone(scope)}
title={t("connect.scopeHint", { scope })}
>
{t(`connect.scope.${scope}`, { defaultValue: scope })}
</Badge>
))}
</span>
) : (
<span className="faint">—</span>
)}
</KV>
<KV label={t("connect.lastConnected")}>
<RelativeTime ts={connectedAt || meta?.pairedAt || null} />
</KV>
@@ -342,9 +250,9 @@ export function ConnectPage() {
aria-expanded={repairOpen}
aria-controls="cn-repair"
data-testid="connect-repair"
onClick={toggleRepair}
onClick={() => setRepairOpen((open) => !open)}
>
<Icon name="scan" />
<Icon name="refresh" />
{t("connect.reconnect")}
</Button>
</div>
@@ -360,22 +268,7 @@ export function ConnectPage() {
</div>
<div className="cn-repair" id="cn-repair" hidden={!repairOpen} data-testid="connect-repair-panel">
<div className="cn-pair">
<Button
variant={repairOpen && !repairManualOpen ? "primary" : "default"}
className="cn-pair__btn"
data-testid="connect-repair-scan"
onClick={() => setScanOpen(true)}
>
<Icon name="scan" />
{t("connect.scan")}
</Button>
<div className="cn-or" aria-hidden="true">
<span>{t("connect.or", { defaultValue: "or" })}</span>
</div>
{manualToggle(repairManualOpen, () => setRepairManualOpen((open) => !open))}
</div>
{renderManualForm(repairManualOpen, repairOpen && repairManualOpen)}
{renderManualForm(repairOpen, repairOpen)}
</div>
<details className="cn-details">
@@ -412,7 +305,7 @@ export function ConnectPage() {
);
}
/** Unconnected: scan-to-pair is the single primary until the form opens. */
/** Unconnected: the manual form is the single path and the primary action. */
function unconnectedPanel() {
return (
<section className="card card--quiet cn-card" aria-labelledby="cn-card-title">
@@ -436,23 +329,7 @@ export function ConnectPage() {
</div>
) : null}
<div className="cn-pair">
<Button
variant={manualOpen ? "default" : "primary"}
className="cn-pair__btn"
data-testid="connect-scan"
onClick={() => setScanOpen(true)}
>
<Icon name="scan" />
{t("connect.scan")}
</Button>
<div className="cn-or" aria-hidden="true">
<span>{t("connect.or", { defaultValue: "or" })}</span>
</div>
{manualToggle(manualOpen, () => setManualOpen((open) => !open))}
</div>
{renderManualForm(manualOpen, manualOpen)}
{renderManualForm(true, true)}
</section>
);
}
@@ -477,73 +354,6 @@ export function ConnectPage() {
</div>
</div>
<ScanDialog
open={scanOpen}
onOpenChange={setScanOpen}
onFound={handleFound}
fallbackEndpoint={storedEndpoint}
/>
<Modal
open={pending != null}
onOpenChange={(open) => {
if (!open && !redeeming) setPending(null);
}}
title={t("connect.confirmTitle")}
footer={
<>
<Button
variant="ghost"
data-testid="connect-confirm-cancel"
onClick={() => {
setPairError(null);
setPending(null);
}}
disabled={redeeming}
>
{t("common.cancel")}
</Button>
<Button
variant="primary"
data-testid="connect-confirm-connect"
onClick={() => void confirmPending()}
disabled={redeeming}
>
{redeeming ? t("connect.connecting") : t("connect.connect")}
</Button>
</>
}
>
{pending ? (
<div>
<p className="cn-lead">{t("connect.scanLead")}</p>
<div className="cn-confirm">
<KV label={t("connect.server")}>{pending.name || pending.hostname}</KV>
<KV label={t("connect.endpoint")} mono>
{pending.hostname}:{pending.port}
</KV>
<KV label={t("connect.pairCode")} mono>
{maskSecret(pending.code)}
</KV>
</div>
<div className="cn-warn">
<span className="cn-ico">
<Icon name="alert" />
</span>
<span>{t("connect.scanWarning")}</span>
</div>
{pairError ? (
<div className="cn-error" role="alert">
<span className="cn-ico">
<Icon name="alert" />
</span>
<span>{pairError}</span>
</div>
) : null}
</div>
) : null}
</Modal>
<ConfirmDialog
open={disconnectOpen}
onOpenChange={setDisconnectOpen}