Skip to content

Commit bd65f35

Browse files
authored
Style mobile pairing QR codes (#2775)
## Summary - render mobile pairing QR codes locally with Wallet-inspired styling - hide the raw pairing URI behind a matching copy button - add focused rendering and dialog coverage ## Test plan - desktop lint and 3,489 desktop tests - focused pairing dialog Playwright test - decoded the rendered QR back to the exact pairing URI
1 parent dcad78f commit bd65f35

9 files changed

Lines changed: 556 additions & 38 deletions

File tree

‎desktop/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"jdenticon": "^3.3.0",
6767
"lucide-react": "^1.0.0",
6868
"motion": "^12.38.0",
69+
"qrcode": "^1.5.4",
6970
"qrcode.react": "^4.2.0",
7071
"react": "^19.1.0",
7172
"react-diff-view": "^3.3.2",

‎desktop/playwright.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export default defineConfig({
112112
"**/onboarding-backup.spec.ts",
113113
"**/onboarding-agent-defaults.spec.ts",
114114
"**/nostr-bind.spec.ts",
115+
"**/mobile-pairing-qr.spec.ts",
115116
"**/profile-nsec-reveal.spec.ts",
116117
"**/signout-confirmation.spec.ts",
117118
"**/agent-provider-dropdowns.spec.ts",

‎desktop/src/features/settings/ui/MobilePairingCard.tsx‎

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useCallback, useEffect, useRef, useState } from "react";
2-
import { QRCodeSVG } from "qrcode.react";
32
import {
43
Check,
54
Copy,
@@ -19,6 +18,7 @@ import {
1918
startPairing,
2019
} from "@/shared/api/tauri";
2120
import { Button } from "@/shared/ui/button";
21+
import { StyledQrCode } from "@/shared/ui/styled-qr-code";
2222
import {
2323
Dialog,
2424
DialogContent,
@@ -176,11 +176,11 @@ function PairingDialog({
176176
return (
177177
<Dialog onOpenChange={handleOpenChange} open={open}>
178178
<DialogContent
179-
className="max-w-md overflow-hidden p-0"
179+
className="max-w-md gap-0 overflow-hidden border-0 px-6 pb-6 pt-6"
180180
data-testid="mobile-pairing-dialog"
181181
>
182182
<div className="flex max-h-[85vh] flex-col">
183-
<DialogHeader className="border-b border-border/60 px-6 py-5 pr-14">
183+
<DialogHeader className="shrink-0 pb-5 pr-8">
184184
<DialogTitle>Pair Mobile Device</DialogTitle>
185185
<DialogDescription>
186186
{step === "sas"
@@ -191,7 +191,7 @@ function PairingDialog({
191191
</DialogDescription>
192192
</DialogHeader>
193193

194-
<div className="flex-1 overflow-y-auto px-6 py-4">
194+
<div className="min-h-0 flex-1 overflow-y-auto pt-4">
195195
{step === "error" && error ? (
196196
<div className="flex items-start gap-2 rounded-lg border border-destructive/30 bg-destructive/10 px-3 py-2 text-sm text-destructive">
197197
<TriangleAlert className="mt-0.5 h-4 w-4 shrink-0" />
@@ -205,35 +205,30 @@ function PairingDialog({
205205
</p>
206206
</div>
207207
) : step === "qr" && qrUri ? (
208-
<div className="space-y-4">
209-
<div className="flex justify-center rounded-lg border border-border/70 bg-white p-4">
210-
<QRCodeSVG
208+
<div className="mx-auto grid w-fit gap-4">
209+
<div
210+
className="rounded-lg border border-border/70 bg-white p-3"
211+
data-testid="mobile-pairing-qr-container"
212+
>
213+
<StyledQrCode
214+
centerImageSrc="/[email protected]"
211215
data-testid="mobile-pairing-qr"
212-
level="M"
213216
size={240}
217+
title="Mobile pairing QR code"
214218
value={qrUri}
215219
/>
216220
</div>
217221

218-
<div className="space-y-1.5">
219-
<p className="text-xs font-medium text-muted-foreground">
220-
Pairing code
221-
</p>
222-
<button
223-
className="flex w-full min-w-0 items-center gap-2 break-all rounded-lg border border-border bg-muted/50 px-3 py-2 text-left text-xs transition-colors hover:bg-muted/70"
224-
data-testid="copy-pairing-code"
225-
onClick={handleCopy}
226-
title="Copy pairing code"
227-
type="button"
228-
>
229-
<code className="min-w-0 flex-1 break-all">{qrUri}</code>
230-
<Copy className="h-4 w-4 shrink-0" />
231-
</button>
232-
</div>
233-
234-
<p className="text-center text-xs text-muted-foreground">
235-
Waiting for mobile device to scan...
236-
</p>
222+
<Button
223+
className="w-full"
224+
data-testid="copy-pairing-code"
225+
onClick={handleCopy}
226+
type="button"
227+
variant="outline"
228+
>
229+
<Copy className="mr-1.5 h-4 w-4" />
230+
Copy pairing code
231+
</Button>
237232
</div>
238233
) : step === "sas" && sasCode ? (
239234
<div className="space-y-4">
@@ -297,17 +292,6 @@ function PairingDialog({
297292
</div>
298293
) : null}
299294
</div>
300-
301-
<div className="flex justify-end border-t border-border/60 bg-background/95 px-6 py-4">
302-
<Button
303-
data-testid="mobile-pairing-done"
304-
onClick={() => handleOpenChange(false)}
305-
size="sm"
306-
variant="outline"
307-
>
308-
{step === "done" ? "Done" : "Close"}
309-
</Button>
310-
</div>
311295
</div>
312296
</DialogContent>
313297
</Dialog>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
import React from "react";
4+
import { renderToStaticMarkup } from "react-dom/server";
5+
6+
import { StyledQrCode } from "./styled-qr-code.tsx";
7+
8+
const TEST_PAIRING_URI =
9+
"nostrpair://8f4b8db31967ce14fef970a1ff1e8eecf19a430aa1c83875e2f5be68dcac0f1a?relay=wss%3A%2F%2Frelay.example.com&secret=87d5a8cfd5807a0cb44f728b67d88d6dcb8daf99be137c158f21a50c1e913c0a&v=1";
10+
11+
test("renders the Wallet-style QR geometry locally", () => {
12+
const html = renderToStaticMarkup(
13+
React.createElement(StyledQrCode, {
14+
centerImageSrc: "/[email protected]",
15+
title: "Mobile pairing QR code",
16+
value: TEST_PAIRING_URI,
17+
}),
18+
);
19+
20+
assert.match(html, /aria-label="Mobile pairing QR code"/);
21+
assert.match(html, /data-qr-matrix-size="57"/);
22+
assert.equal((html.match(/data-qr-finder-pattern=""/g) ?? []).length, 3);
23+
assert.ok(
24+
(html.match(/<circle /g) ?? []).length > 100,
25+
"expected the QR payload to render as individual circular data cells",
26+
);
27+
assert.match(html, /href="\/app-icon@2x\.png"/);
28+
});
29+
30+
test("uses a deterministic lower-density matrix for the same payload", () => {
31+
const first = renderToStaticMarkup(
32+
React.createElement(StyledQrCode, { value: TEST_PAIRING_URI }),
33+
);
34+
const second = renderToStaticMarkup(
35+
React.createElement(StyledQrCode, { value: TEST_PAIRING_URI }),
36+
);
37+
38+
assert.equal(first, second);
39+
});
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
import { useId, useMemo, type ReactNode, type SVGProps } from "react";
2+
import { create } from "qrcode";
3+
4+
const CELL_SPACING_RATIO = 0.2;
5+
const FINDER_PATTERN_SIZE = 7;
6+
const QUIET_ZONE_SIZE = 4;
7+
const MAX_CENTER_OBSCURED_RATIO = 0.1;
8+
const CENTER_ICON_SIZE_RATIO = 0.8;
9+
10+
type StyledQrCodeProps = Omit<
11+
SVGProps<SVGSVGElement>,
12+
"children" | "height" | "title" | "width"
13+
> & {
14+
backgroundColor?: string;
15+
centerImageSrc?: string;
16+
foregroundColor?: string;
17+
size?: number;
18+
title?: string;
19+
value: string;
20+
};
21+
22+
function isFinderCell(row: number, column: number, matrixSize: number) {
23+
const isTop = row < FINDER_PATTERN_SIZE;
24+
const isLeft = column < FINDER_PATTERN_SIZE;
25+
const isRight = column >= matrixSize - FINDER_PATTERN_SIZE;
26+
const isBottom = row >= matrixSize - FINDER_PATTERN_SIZE;
27+
28+
return (isTop && isLeft) || (isTop && isRight) || (isBottom && isLeft);
29+
}
30+
31+
function getCenterAreaSize(matrixSize: number) {
32+
const maxArea = matrixSize * matrixSize * MAX_CENTER_OBSCURED_RATIO;
33+
const maxSide = Math.floor(Math.sqrt(maxArea));
34+
return maxSide % 2 === 0 ? maxSide - 1 : maxSide;
35+
}
36+
37+
function FinderPattern({
38+
backgroundColor,
39+
foregroundColor,
40+
x,
41+
y,
42+
}: {
43+
backgroundColor: string;
44+
foregroundColor: string;
45+
x: number;
46+
y: number;
47+
}) {
48+
return (
49+
<g data-qr-finder-pattern="">
50+
<rect
51+
fill={foregroundColor}
52+
height={FINDER_PATTERN_SIZE}
53+
rx={2}
54+
width={FINDER_PATTERN_SIZE}
55+
x={x}
56+
y={y}
57+
/>
58+
<rect
59+
fill={backgroundColor}
60+
height={FINDER_PATTERN_SIZE - 2}
61+
rx={0.8}
62+
width={FINDER_PATTERN_SIZE - 2}
63+
x={x + 1}
64+
y={y + 1}
65+
/>
66+
<rect
67+
fill={foregroundColor}
68+
height={FINDER_PATTERN_SIZE - 4}
69+
rx={0.4}
70+
width={FINDER_PATTERN_SIZE - 4}
71+
x={x + 2}
72+
y={y + 2}
73+
/>
74+
</g>
75+
);
76+
}
77+
78+
export function StyledQrCode({
79+
backgroundColor = "#ffffff",
80+
centerImageSrc,
81+
foregroundColor = "#000000",
82+
size = 240,
83+
title = "QR code",
84+
value,
85+
...svgProps
86+
}: StyledQrCodeProps) {
87+
const clipId = `styled-qr-logo-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`;
88+
const matrix = useMemo(
89+
() => create(value, { errorCorrectionLevel: "M" }).modules,
90+
[value],
91+
);
92+
const viewBoxSize = matrix.size + QUIET_ZONE_SIZE * 2;
93+
const dataCellSize = 1 - CELL_SPACING_RATIO;
94+
const dataCellRadius = dataCellSize / 2;
95+
const centerAreaSize = centerImageSrc ? getCenterAreaSize(matrix.size) : 0;
96+
const centerAreaStart = (matrix.size - centerAreaSize) / 2;
97+
const centerAreaEnd = centerAreaStart + centerAreaSize;
98+
const centerImageSize = centerAreaSize * CENTER_ICON_SIZE_RATIO;
99+
const centerImageStart = (matrix.size - centerImageSize) / 2;
100+
const cells: ReactNode[] = [];
101+
102+
for (let row = 0; row < matrix.size; row += 1) {
103+
for (let column = 0; column < matrix.size; column += 1) {
104+
const isCenterCell =
105+
centerImageSrc &&
106+
row >= centerAreaStart &&
107+
row < centerAreaEnd &&
108+
column >= centerAreaStart &&
109+
column < centerAreaEnd;
110+
111+
if (
112+
!matrix.get(row, column) ||
113+
isFinderCell(row, column, matrix.size) ||
114+
isCenterCell
115+
) {
116+
continue;
117+
}
118+
119+
cells.push(
120+
<circle
121+
cx={column + 0.5}
122+
cy={row + 0.5}
123+
fill={foregroundColor}
124+
key={`${row}-${column}`}
125+
r={dataCellRadius}
126+
/>,
127+
);
128+
}
129+
}
130+
131+
return (
132+
<svg
133+
{...svgProps}
134+
aria-label={title}
135+
data-qr-matrix-size={matrix.size}
136+
height={size}
137+
role="img"
138+
viewBox={`${-QUIET_ZONE_SIZE} ${-QUIET_ZONE_SIZE} ${viewBoxSize} ${viewBoxSize}`}
139+
width={size}
140+
xmlns="http://www.w3.org/2000/svg"
141+
>
142+
<title>{title}</title>
143+
<rect
144+
fill={backgroundColor}
145+
height={viewBoxSize}
146+
width={viewBoxSize}
147+
x={-QUIET_ZONE_SIZE}
148+
y={-QUIET_ZONE_SIZE}
149+
/>
150+
<g data-qr-data-cells="">{cells}</g>
151+
<FinderPattern
152+
backgroundColor={backgroundColor}
153+
foregroundColor={foregroundColor}
154+
x={0}
155+
y={0}
156+
/>
157+
<FinderPattern
158+
backgroundColor={backgroundColor}
159+
foregroundColor={foregroundColor}
160+
x={matrix.size - FINDER_PATTERN_SIZE}
161+
y={0}
162+
/>
163+
<FinderPattern
164+
backgroundColor={backgroundColor}
165+
foregroundColor={foregroundColor}
166+
x={0}
167+
y={matrix.size - FINDER_PATTERN_SIZE}
168+
/>
169+
{centerImageSrc ? (
170+
<>
171+
<defs>
172+
<clipPath id={clipId}>
173+
<rect
174+
height={centerImageSize}
175+
rx={2}
176+
width={centerImageSize}
177+
x={centerImageStart}
178+
y={centerImageStart}
179+
/>
180+
</clipPath>
181+
</defs>
182+
<rect
183+
fill={foregroundColor}
184+
height={centerImageSize}
185+
rx={2}
186+
width={centerImageSize}
187+
x={centerImageStart}
188+
y={centerImageStart}
189+
/>
190+
<image
191+
clipPath={`url(#${clipId})`}
192+
height={centerImageSize}
193+
href={centerImageSrc}
194+
preserveAspectRatio="xMidYMid meet"
195+
width={centerImageSize}
196+
x={centerImageStart}
197+
y={centerImageStart}
198+
/>
199+
</>
200+
) : null}
201+
</svg>
202+
);
203+
}

‎desktop/src/testing/e2eBridge.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10902,6 +10902,11 @@ export function maybeInstallE2eTauriMocks() {
1090210902
case "plugin:event|listen":
1090310903
// Tauri event system (pairing, huddle) — no-op in e2e, return unlisten fn ID
1090410904
return Math.floor(Math.random() * 1_000_000);
10905+
case "start_pairing":
10906+
return "nostrpair://8f4b8db31967ce14fef970a1ff1e8eecf19a430aa1c83875e2f5be68dcac0f1a?relay=wss%3A%2F%2Frelay.example.com&secret=87d5a8cfd5807a0cb44f728b67d88d6dcb8daf99be137c158f21a50c1e913c0a&v=1";
10907+
case "cancel_pairing":
10908+
case "confirm_pairing_sas":
10909+
return null;
1090510910
// ── NIP-IA identity archival ────────────────────────────────────────
1090610911
// These mocks drive the archive-button gate matrix in
1090710912
// tests/e2e/identity-archive.spec.ts. Defaults keep the button hidden

‎desktop/src/types/qrcode.d.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
declare module "qrcode" {
2+
type ErrorCorrectionLevel = "L" | "M" | "Q" | "H";
3+
4+
type BitMatrix = {
5+
get(row: number, column: number): number;
6+
size: number;
7+
};
8+
9+
type QrCode = {
10+
modules: BitMatrix;
11+
};
12+
13+
export function create(
14+
value: string,
15+
options?: { errorCorrectionLevel?: ErrorCorrectionLevel },
16+
): QrCode;
17+
}

0 commit comments

Comments
 (0)