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

+2 -32
View File
@@ -8,9 +8,6 @@ export const CONNECTION_KEY = "pi2-connection";
export const ENDPOINT_KEY = "pi2-endpoint";
export const DEVICE_KEY = "pi2-device";
export const CONNECTED_AT_KEY = "pi2-connected-at";
export const SCOPES_KEY = "pi2-device-scopes";
/** Scope assumed for devices paired before scopes were persisted. */
export const DEFAULT_CONNECTION_SCOPES = ["read"] as const;
function read(key: string): string | null {
try {
@@ -36,26 +33,6 @@ function drop(key: string): void {
}
}
/** Persisted device scopes; defaults to `["read"]` when absent or unreadable. */
function readScopes(): string[] {
const raw = read(SCOPES_KEY);
if (!raw) return [...DEFAULT_CONNECTION_SCOPES];
try {
const parsed: unknown = JSON.parse(raw);
if (Array.isArray(parsed)) {
const scopes = parsed.filter((item): item is string => typeof item === "string" && item.trim() !== "");
return scopes.length ? scopes : [...DEFAULT_CONNECTION_SCOPES];
}
} catch {
/* fall through to the default */
}
return [...DEFAULT_CONNECTION_SCOPES];
}
function writeScopes(scopes: string[]): void {
write(SCOPES_KEY, JSON.stringify(scopes.length ? scopes : DEFAULT_CONNECTION_SCOPES));
}
export type PairingState = "paired" | "unpaired";
export interface ConnectionStore {
@@ -65,14 +42,12 @@ export interface ConnectionStore {
deviceName: string;
connectedAt: number;
token: string | null;
/** Device scopes granted by the daemon when this browser was paired. */
scopes: string[];
server: InfoResponse | null;
wsState: WsConnectionState;
isPaired: () => boolean;
refreshFromStorage: () => void;
connect: (options?: { endpoint?: string; deviceName?: string; token?: string | null; scopes?: string[] }) => void;
connect: (options?: { endpoint?: string; deviceName?: string; token?: string | null }) => void;
disconnect: () => void;
setServer: (server: InfoResponse | null) => void;
setWsState: (state: WsConnectionState) => void;
@@ -86,7 +61,6 @@ function initialState() {
deviceName: read(DEVICE_KEY) ?? "",
connectedAt: Number(read(CONNECTED_AT_KEY)) || 0,
token: getDeviceToken(),
scopes: readScopes(),
server: null as InfoResponse | null,
wsState: "idle" as WsConnectionState,
};
@@ -99,12 +73,11 @@ export const useConnectionStore = create<ConnectionStore>((set, get) => ({
refreshFromStorage: () => set(initialState()),
connect: ({ endpoint, deviceName, token, scopes } = {}) => {
connect: ({ endpoint, deviceName, token } = {}) => {
const now = Date.now();
if (endpoint) write(ENDPOINT_KEY, endpoint);
if (deviceName) write(DEVICE_KEY, deviceName);
if (token !== undefined) setDeviceToken(token);
if (scopes !== undefined) writeScopes(scopes);
write(CONNECTED_AT_KEY, String(now));
write(CONNECTION_KEY, "paired");
set({
@@ -113,7 +86,6 @@ export const useConnectionStore = create<ConnectionStore>((set, get) => ({
deviceName: deviceName ?? get().deviceName,
connectedAt: now,
token: getDeviceToken(),
scopes: scopes !== undefined ? readScopes() : get().scopes,
});
},
@@ -121,7 +93,6 @@ export const useConnectionStore = create<ConnectionStore>((set, get) => ({
drop(ENDPOINT_KEY);
drop(DEVICE_KEY);
drop(CONNECTED_AT_KEY);
drop(SCOPES_KEY);
setDeviceToken(null);
write(CONNECTION_KEY, "unpaired");
set({
@@ -130,7 +101,6 @@ export const useConnectionStore = create<ConnectionStore>((set, get) => ({
deviceName: "",
connectedAt: 0,
token: null,
scopes: [...DEFAULT_CONNECTION_SCOPES],
server: null,
wsState: "idle",
});