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