Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

RevokeConnectDeviceDialog.tsx

114 lines3.2 KB

1import { useState } from "react";
2​
3import {
4 AlertDialog,
5 AlertDialogClose,
6 AlertDialogDescription,
7 AlertDialogFooter,
8 AlertDialogHeader,
9 AlertDialogPopup,
10 AlertDialogTitle,
11} from "../ui/alert-dialog";
12import { Button } from "../ui/button";
13import { Checkbox } from "../ui/checkbox";
14import { Spinner } from "../ui/spinner";
15​
16export interface ConnectDeviceRevokeRequest {
17 readonly title: string;
18 /** Revokes the device; resolves once the attempt finished, failed or not. */
19 readonly revoke: () => Promise<void>;
21​
22/**
23 * With approval off, a revoked T3 Connect device signs straight back in, so revoking offers to
24 * turn approval on first.
25 */
26export function RevokeConnectDeviceDialog({
27 request,
28 onClose,
29 requireApproval,
30}: {
31 readonly request: ConnectDeviceRevokeRequest | null;
32 readonly onClose: () => void;
33 /** Saves the setting; resolves false when the save failed. */
34 readonly requireApproval: () => Promise<boolean>;
35}) {
36 return (
37 <AlertDialog
38 open={request !== null}
39 onOpenChange={(open) => {
40 if (!open) onClose();
41 }}
42 >
43 {request ? (
44 <RevokeConnectDeviceDialogPopup
45 key={request.title}
46 request={request}
47 onClose={onClose}
48 requireApproval={requireApproval}
49 />
50 ) : null}
51 </AlertDialog>
52 );
54​
55function RevokeConnectDeviceDialogPopup({
56 request,
57 onClose,
58 requireApproval,
59}: {
60 readonly request: ConnectDeviceRevokeRequest;
61 readonly onClose: () => void;
62 readonly requireApproval: () => Promise<boolean>;
63}) {
64 const [turnOnApproval, setTurnOnApproval] = useState(true);
65 const [busy, setBusy] = useState(false);
66​
67 const confirm = async () => {
68 setBusy(true);
69 if (turnOnApproval && !(await requireApproval())) {
70 setBusy(false);
71 return;
72 }
73 await request.revoke();
74 setBusy(false);
75 onClose();
76 };
77​
78 return (
79 <AlertDialogPopup>
80 <AlertDialogHeader>
81 <AlertDialogTitle>{request.title}</AlertDialogTitle>
82 <AlertDialogDescription>
83 Approval for new devices is off, so a revoked device signed in to your T3 Connect account
84 can connect again on its own.
85 </AlertDialogDescription>
86 <label className="mt-2 flex cursor-pointer items-start gap-3">
87 <Checkbox
88 className="mt-0.5"
89 checked={turnOnApproval}
90 disabled={busy}
91 onCheckedChange={(checked) => setTurnOnApproval(checked === true)}
92 />
93 <span className="min-w-0">
94 <span className="block text-sm font-medium text-foreground">
95 Require approval for new devices
96 </span>
97 <span className="block text-xs text-muted-foreground">
98 The device has to be approved here before it can connect again.
99 </span>
100 </span>
101 </label>
102 </AlertDialogHeader>
103 <AlertDialogFooter>
104 <AlertDialogClose disabled={busy} render={<Button variant="outline" disabled={busy} />}>
105 Cancel
106 </AlertDialogClose>
107 <Button variant="destructive" disabled={busy} onClick={() => void confirm()}>
108 {busy && <Spinner size="sm" />}
109 Revoke
110 </Button>
111 </AlertDialogFooter>
112 </AlertDialogPopup>
113 );