Open History to see the last commit
1import type { AuthConnectDevice, EnvironmentId } from "@t3tools/contracts";
2import {
3 isAtomCommandInterrupted,
4 squashAtomCommandFailure,
5} from "@t3tools/client-runtime/state/runtime";
6import * as DateTime from "effect/DateTime";
7import { useState } from "react";
8
9import type { ServerPairingLinkRecord } from "~/environments/primary";
10import { authEnvironment } from "~/state/auth";
11import { useAtomCommand } from "~/state/use-atom-command";
12import { Button } from "../ui/button";
13import { stackedThreadToast, toastManager } from "../ui/toast";
14import {
15 connectDeviceName,
16 describeConnectDeviceClient,
17 formatAccessTimestamp,
18} from "./ConnectionsSettings.logic";
19import { ITEM_ROW_CLASSNAME, ITEM_ROW_INNER_CLASSNAME } from "./itemRows";
20import { SettingsSection } from "./settingsLayout";
21
22function reportFailure(title: string, result: Parameters<typeof isAtomCommandInterrupted>[0]) {
23 if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return;
24 const error = squashAtomCommandFailure(result);
25 toastManager.add(
26 stackedThreadToast({
27 type: "error",
28 title,
29 description: error instanceof Error ? error.message : "An error occurred.",
30 }),
31 );
32}
33
34export function ConnectDeviceApprovalSettings({
35 environmentId,
36 requests,
37 devices,
38 canWrite,
39 requestRevoke,
40}: {
41 readonly environmentId: EnvironmentId;
42 readonly requests: ReadonlyArray<ServerPairingLinkRecord>;
43 readonly devices: ReadonlyArray<AuthConnectDevice>;
44 readonly canWrite: boolean;
45 /** Revokes now, or first asks to turn approval on when it is off. */
46 readonly requestRevoke: (title: string, revoke: () => Promise<void>) => void;
47}) {
48 const approve = useAtomCommand(authEnvironment.approveConnectDevice, { reportFailure: false });
49 const deny = useAtomCommand(authEnvironment.revokePairingLink, { reportFailure: false });
50 const revoke = useAtomCommand(authEnvironment.revokeConnectDevice, { reportFailure: false });
51 const [busyId, setBusyId] = useState<string | null>(null);
52
53 const run = async (id: string, action: () => Promise<void>) => {
54 setBusyId(id);
55 await action();
56 setBusyId(null);
57 };
58
59 return (
60 <SettingsSection id="connect-devices" title="T3 Connect devices">
61 {requests.map((request) => {
62 const connectRequest = request.connectDeviceRequest;
63 if (!connectRequest) return null;
64 const busy = busyId === request.id;
65 return (
66 <div key={request.id} className={ITEM_ROW_CLASSNAME}>
67 <div className={ITEM_ROW_INNER_CLASSNAME}>
68 <div className="min-w-0 flex-1 space-y-1">
69 <p className="text-sm font-medium text-foreground">
70 Waiting for approval ·{" "}
71 <span className="font-mono tabular-nums">{connectRequest.verificationCode}</span>
72 </p>
73 <p className="text-xs text-muted-foreground">
74 {describeConnectDeviceClient(connectRequest)}. Approve only if the device in front
75 of you shows this code.
76 </p>
77 </div>
78 <div className="flex w-full shrink-0 items-center gap-2 sm:w-auto sm:justify-end">
79 <Button
80 size="xs"
81 variant="destructive-outline"
82 disabled={!canWrite || busy}
83 onClick={() =>
84 void run(request.id, async () =>
85 reportFailure(
86 "Could not deny the device",
87 await deny({ environmentId, input: { id: request.id } }),
88 ),
89 )
90 }
91 >
92 Deny
93 </Button>
94 <Button
95 size="xs"
96 disabled={!canWrite || busy}
97 onClick={() =>
98 void run(request.id, async () =>
99 reportFailure(
100 "Could not approve the device",
101 await approve({ environmentId, input: { pairingLinkId: request.id } }),
102 ),
103 )
104 }
105 >
106 {busy ? "Saving…" : "Approve"}
107 </Button>
108 </div>
109 </div>
110 </div>
111 );
112 })}
113 {devices.map((device) => {
114 const busy = busyId === device.proofKeyThumbprint;
115 return (
116 <div key={device.proofKeyThumbprint} className={ITEM_ROW_CLASSNAME}>
117 <div className={ITEM_ROW_INNER_CLASSNAME}>
118 <div className="min-w-0 flex-1 space-y-1">
119 <p className="text-sm font-medium text-foreground">
120 {describeConnectDeviceClient(device)}
121 </p>
122 <p className="text-xs text-muted-foreground">
123 <span className="font-mono tabular-nums">{device.verificationCode}</span>
124 <span aria-hidden> · </span>
125 Trusted since {formatAccessTimestamp(DateTime.formatIso(device.approvedAt))}
126 </p>
127 </div>
128 <div className="flex w-full shrink-0 items-center gap-2 sm:w-auto sm:justify-end">
129 <Button
130 size="xs"
131 variant="destructive-outline"
132 disabled={!canWrite || busy}
133 onClick={() =>
134 requestRevoke(`Revoke ${connectDeviceName(device)}?`, () =>
135 run(device.proofKeyThumbprint, async () =>
136 reportFailure(
137 "Could not revoke the device",
138 await revoke({
139 environmentId,
140 input: { proofKeyThumbprint: device.proofKeyThumbprint },
141 }),
142 ),
143 ),
144 )
145 }
146 >
147 {busy ? "Revoking…" : "Revoke"}
148 </Button>
149 </div>
150 </div>
151 </div>
152 );
153 })}
154 {devices.length === 0 && requests.length === 0 ? (
155 <div className={ITEM_ROW_CLASSNAME}>
156 <p className="text-xs text-muted-foreground/60">
157 No device has connected through T3 Connect yet.
158 </p>
159 </div>
160 ) : null}
161 </SettingsSection>
162 );
163}