Skip to content

Commit 9420e8d

Browse files
wesbillmanCarlclaude
committed
feat(pairing): copy the pairing code from Settings
Pairing a simulator can't scan a QR code, so Settings → Pair mobile now offers "Copy pairing code" alongside the QR. Native pairing status includes the nostrpair:// URI already encoded in the QR, and the button copies it. Feedback is bound to the URI being shown, so a refreshed QR resets the label and a late copy result can't mislabel it. Co-authored-by: Carl <32a2e2c9d428ee08902cab75d956da2c1d235a22d4766b0dd4138bf6e2e5db1d@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Wes <[email protected]>
1 parent 33b0e15 commit 9420e8d

6 files changed

Lines changed: 108 additions & 6 deletions

File tree

‎src-tauri/src/pairing/mod.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub enum Status {
2323
Connecting,
2424
Qr {
2525
svg: String,
26+
uri: String,
2627
},
2728
Code {
2829
code: String,
@@ -338,10 +339,11 @@ async fn run(
338339
pending,
339340
auth,
340341
svg,
342+
uri.to_string(),
341343
connection_started,
342344
))
343345
};
344-
let (mut exchange, mut socket, relay_url, pending, mut auth, svg, connection_started) = tokio::select! {
346+
let (mut exchange, mut socket, relay_url, pending, mut auth, svg, uri, connection_started) = tokio::select! {
345347
biased;
346348
_ = cancel.cancelled() => return Ok(()),
347349
result = tokio::time::timeout(Duration::from_secs(35), setup) =>
@@ -350,7 +352,7 @@ async fn run(
350352
// Keep protocol expiry aligned with display, but renew before the transport cap.
351353
exchange.session.start_source_lifetime();
352354
let deadline = exchange_deadline(&exchange.session, connection_started);
353-
pairing.update(id, Status::Qr { svg });
355+
pairing.update(id, Status::Qr { svg, uri });
354356
exchange_until_deadline(
355357
ExchangeContext {
356358
pairing,

‎src-tauri/src/pairing/tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ async fn early_connection_loss_is_error_before_publication_and_uncertain_after()
717717
confirm: tx,
718718
status: Status::Qr {
719719
svg: "fixture".into(),
720+
uri: "fixture".into(),
720721
},
721722
payload_sent,
722723
});

‎src/bundled/pairing/PairingSettings.test.tsx‎

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ it.each([true, false])(
100100
const native = {
101101
account: vi.fn(async () => viewer),
102102
start: vi.fn<PairingNative["start"]>(async () => {
103-
status = { phase: "qr", svg: "<svg/>" };
103+
status = { phase: "qr", svg: "<svg/>", uri: "nostrpair://fixture" };
104104
}),
105105
status: vi.fn(async () => status),
106106
cancel: vi.fn(
@@ -180,7 +180,7 @@ it.each(["uncertain", "cancelled"] as const)(
180180
const native = {
181181
account: vi.fn(async () => viewer),
182182
start: vi.fn<PairingNative["start"]>(async () => {
183-
status = { phase: "qr", svg: "<svg/>" };
183+
status = { phase: "qr", svg: "<svg/>", uri: "nostrpair://fixture" };
184184
}),
185185
status: vi.fn(async () => status),
186186
cancel: vi.fn(
@@ -253,6 +253,78 @@ it.each(["uncertain", "cancelled"] as const)(
253253
},
254254
);
255255

256+
it("copies the pairing code shown in the current QR", async () => {
257+
vi.useFakeTimers();
258+
vi.stubGlobal("matchMedia", () => ({
259+
matches: false,
260+
addEventListener() {},
261+
removeEventListener() {},
262+
}));
263+
const writeText = vi
264+
.fn<(text: string) => Promise<void>>()
265+
.mockResolvedValueOnce(undefined)
266+
.mockRejectedValueOnce(new Error("denied"));
267+
Object.defineProperty(navigator, "clipboard", {
268+
configurable: true,
269+
value: { writeText },
270+
});
271+
const viewer = "a".repeat(64);
272+
const snapshot: ClientSnapshot = {
273+
status: "ready",
274+
relayAvailable: true,
275+
viewer,
276+
selected: "https://community.example",
277+
profile: { name: "", picture: "" },
278+
memberships: [],
279+
};
280+
let status: PairingStatus = {
281+
phase: "qr",
282+
svg: "<svg/>",
283+
uri: "nostrpair://first",
284+
};
285+
const native = {
286+
account: vi.fn(async () => viewer),
287+
start: vi.fn(async () => {}),
288+
status: vi.fn(async () => status),
289+
confirm: vi.fn(async () => {}),
290+
deny: vi.fn(async () => {}),
291+
cancel: vi.fn(async (): Promise<PairingStatus> => ({ phase: "cancelled" })),
292+
} satisfies PairingNative;
293+
render(
294+
<PairingSettings
295+
communities={{ snapshot: () => snapshot, subscribe: () => () => {} }}
296+
active={() => true}
297+
available
298+
native={native}
299+
/>,
300+
);
301+
await act(async () => {});
302+
await act(async () => {
303+
fireEvent.click(screen.getByRole("button", { name: "Copy pairing code" }));
304+
});
305+
expect(writeText).toHaveBeenLastCalledWith("nostrpair://first");
306+
expect(screen.getByRole("button", { name: "Copied" })).toBeTruthy();
307+
308+
status = { phase: "qr", svg: "<svg/>", uri: "nostrpair://second" };
309+
await act(async () => {
310+
await vi.advanceTimersByTimeAsync(400);
311+
});
312+
await act(async () => {
313+
fireEvent.click(screen.getByRole("button", { name: "Copy pairing code" }));
314+
});
315+
expect(writeText).toHaveBeenLastCalledWith("nostrpair://second");
316+
expect(screen.getByRole("alert").textContent).toContain(
317+
"Couldn’t copy the pairing code",
318+
);
319+
320+
status = { phase: "code", code: "123456", codeEntry: true };
321+
await act(async () => {
322+
await vi.advanceTimersByTimeAsync(400);
323+
});
324+
expect(screen.queryByRole("button", { name: /Cop/ })).toBeNull();
325+
expect(screen.queryByRole("alert")).toBeNull();
326+
});
327+
256328
it("rejects a legacy mismatch and requires explicit retry", async () => {
257329
vi.useFakeTimers();
258330
vi.stubGlobal("matchMedia", () => ({

‎src/bundled/pairing/PairingSettings.tsx‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export function PairingSettings({
5454
const [manualOrigin, setManualOrigin] = useState<string>();
5555
const [reading, setReading] = useState(false);
5656
const [error, setError] = useState<string>();
57+
const [copied, setCopied] = useState<{ uri: string; failed: boolean }>();
5758
const [relay, setRelay] = useState(() =>
5859
client.selected ? communityDestination(client.selected).url : "",
5960
);
@@ -101,6 +102,14 @@ export function PairingSettings({
101102
if (attempt === lifetime.current) setReading(false);
102103
}
103104
}
105+
async function copy(uri: string) {
106+
try {
107+
await navigator.clipboard.writeText(uri);
108+
setCopied({ uri, failed: false });
109+
} catch {
110+
setCopied({ uri, failed: true });
111+
}
112+
}
104113
function start() {
105114
setError(undefined);
106115
try {
@@ -364,6 +373,20 @@ export function PairingSettings({
364373
</>
365374
)}
366375
</div>
376+
{state.phase === "qr" && (
377+
<>
378+
<Button onClick={() => void copy(state.uri)}>
379+
{copied?.uri === state.uri && !copied.failed
380+
? "Copied"
381+
: "Copy pairing code"}
382+
</Button>
383+
{copied?.uri === state.uri && copied.failed && (
384+
<p role="alert" className="m-0 text-body-sm text-danger">
385+
Couldn’t copy the pairing code. Try again.
386+
</p>
387+
)}
388+
</>
389+
)}
367390
</div>
368391
<ol
369392
aria-label="Pairing steps"

‎src/features/pairing/client.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ function native() {
99
account: vi.fn(async () => "a".repeat(64)),
1010
start: vi.fn<PairingNative["start"]>(async () => {}),
1111
status: vi.fn(
12-
async (): Promise<PairingStatus> => ({ phase: "qr", svg: "fixture" }),
12+
async (): Promise<PairingStatus> => ({
13+
phase: "qr",
14+
svg: "fixture",
15+
uri: "fixture",
16+
}),
1317
),
1418
confirm: vi.fn(async () => {}),
1519
deny: vi.fn(async () => {}),

‎src/features/pairing/client.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type PairingStatus =
1212
| "complete"
1313
| "cancelled";
1414
}
15-
| { phase: "qr"; svg: string }
15+
| { phase: "qr"; svg: string; uri: string }
1616
| { phase: "code"; code: string; codeEntry: boolean }
1717
| { phase: "error"; message: string };
1818
export type PairingNative = {

0 commit comments

Comments
 (0)