Open History to see the last commit
1import type { AuthClientMetadata, AuthPairingLink } from "@t3tools/contracts";
2
3export interface ConnectDeviceApprovalPrompt {
4 readonly pairingLinkId: string;
5 readonly title: string;
6 readonly message: string;
7}
8
9export function describeConnectDevice(device: AuthClientMetadata | undefined): string {
10 const { label, browser, os } = device ?? {};
11 if (label) return label;
12 if (browser && os) return `${browser} on ${os}`;
13 return browser ?? os ?? "A new device";
14}
15
16/** Requests waiting on approval that have not been shown yet, oldest first. */
17export function connectDeviceApprovalPrompts(input: {
18 readonly environmentLabel: string;
19 readonly pairingLinks: ReadonlyArray<AuthPairingLink>;
20 readonly shownIds: ReadonlySet<string>;
21}): ReadonlyArray<ConnectDeviceApprovalPrompt> {
22 const waiting = input.pairingLinks.flatMap((link) =>
23 link.connectDeviceRequest !== undefined && !input.shownIds.has(link.id)
24 ? [{ link, ...link.connectDeviceRequest }]
25 : [],
26 );
27 return [...waiting]
28 .sort(
29 (left, right) =>
30 left.link.createdAt.epochMilliseconds - right.link.createdAt.epochMilliseconds,
31 )
32 .map(({ link, verificationCode, client: device }) => ({
33 pairingLinkId: link.id,
34 title: "Approve this device?",
35 message: `${describeConnectDevice(device)} wants to connect to ${input.environmentLabel} through T3 Connect. It shows code ${verificationCode}. Approve it only if that code matches.`,
36 }));
37}
38
39/**
40 * Shows approval prompts one at a time: an Android alert replaces the one on screen, which
41 * would drop an unanswered request. A prompt answered with "retry" goes back to the front;
42 * "later" puts it back without showing anything until the next offer.
43 */
44export function createApprovalPromptQueue<P extends { readonly pairingLinkId: string }>(
45 present: (prompt: P, answer: (outcome: "done" | "retry" | "later") => void) => void,
46) {
47 const known = new Set<string>();
48 const waiting: P[] = [];
49 let showing: { readonly prompt: P; dropped: boolean } | null = null;
50 const next = () => {
51 if (showing) return;
52 const prompt = waiting.shift();
53 if (prompt === undefined) return;
54 const current = { prompt, dropped: false };
55 showing = current;
56 let answered = false;
57 present(prompt, (outcome) => {
58 if (answered) return;
59 answered = true;
60 showing = null;
61 // A dropped prompt is forgotten, so its host can offer it again when it comes back.
62 if (outcome !== "done" && !current.dropped) waiting.unshift(prompt);
63 if (outcome !== "later") next();
64 });
65 };
66 return {
67 known: known as ReadonlySet<string>,
68 /** Forgets waiting prompts that match, such as a host that went away. */
69 drop(matches: (prompt: P) => boolean) {
70 if (showing && matches(showing.prompt)) {
71 showing.dropped = true;
72 known.delete(showing.prompt.pairingLinkId);
73 }
74 for (let index = waiting.length - 1; index >= 0; index--) {
75 const prompt = waiting[index]!;
76 if (!matches(prompt)) continue;
77 waiting.splice(index, 1);
78 known.delete(prompt.pairingLinkId);
79 }
80 },
81 offer(prompts: ReadonlyArray<P>) {
82 // Also re-shows a prompt deferred because another dialog took the screen.
83 for (const prompt of prompts) {
84 if (known.has(prompt.pairingLinkId)) continue;
85 known.add(prompt.pairingLinkId);
86 waiting.push(prompt);
87 }
88 next();
89 },
90 };
91}