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