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 { msg } from "gt-react-native"; |
| 2 | import { AuthAccessWriteScope, type AuthPairingLink, type EnvironmentId } from "@t3tools/contracts"; |
| 3 | import { useEffect, useEffectEvent } from "react"; |
| 4 | |
| 5 | import * as Dialog from "../../components/ui/Dialog"; |
| 6 | import { m } from "../../i18n/messages"; |
| 7 | import { authEnvironment } from "../../state/auth"; |
| 8 | import { useEnvironments } from "../../state/environments"; |
| 9 | import { useEnvironmentQuery } from "../../state/query"; |
| 10 | import { useEnvironmentsWithScope } from "../../state/session"; |
| 11 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 12 | import { |
| 13 | type ConnectDeviceApprovalPrompt, |
| 14 | connectDeviceApprovalPrompts, |
| 15 | createApprovalPromptQueue, |
| 16 | } from "./connectDeviceApproval"; |
| 17 | |
| 18 | /** Asks to approve T3 Connect devices waiting on hosts that require it and this phone manages. */ |
| 19 | export function ConnectDeviceApprovalCoordinator() { |
| 20 | const { environments } = useEnvironments(); |
| 21 | const requiring = environments.filter( |
| 22 | (environment) => |
| 23 | environment.connection.phase === "connected" && |
| 24 | environment.serverConfig?.settings.requireConnectDeviceApproval === true, |
| 25 | ); |
| 26 | const managed = useEnvironmentsWithScope(requiring, AuthAccessWriteScope); |
| 27 | return ( |
| 28 | <> |
| 29 | {requiring |
| 30 | .filter((environment) => managed.has(environment.environmentId)) |
| 31 | .map((environment) => ( |
| 32 | <EnvironmentApprovalWatcher |
| 33 | key={environment.environmentId} |
| 34 | environmentId={environment.environmentId} |
| 35 | environmentLabel={environment.label} |
| 36 | /> |
| 37 | ))} |
| 38 | </> |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | /** "gone": the request expired or was answered elsewhere before this answer arrived. */ |
| 43 | type ApprovalAnswer = "done" | "failed" | "gone"; |
| 44 | |
| 45 | interface QueuedApprovalPrompt extends ConnectDeviceApprovalPrompt { |
| 46 | readonly environmentId: EnvironmentId; |
| 47 | readonly approve: () => Promise<ApprovalAnswer>; |
| 48 | readonly deny: () => Promise<ApprovalAnswer>; |
| 49 | } |
| 50 | |
| 51 | const watchedEnvironments = new Set<EnvironmentId>(); |
| 52 | |
| 53 | // One queue for every host: a second Android alert would replace an unanswered first one. |
| 54 | const approvalPrompts = createApprovalPromptQueue<QueuedApprovalPrompt>((prompt, answer) => { |
| 55 | const respond = (action: () => Promise<ApprovalAnswer>, failure: string) => () => |
| 56 | void action().then((outcome) => { |
| 57 | // A host that went away or lost access cannot be retried; let the next prompt show. |
| 58 | if (outcome === "done" || !watchedEnvironments.has(prompt.environmentId)) { |
| 59 | return answer("done"); |
| 60 | } |
| 61 | if (outcome === "gone") { |
| 62 | return Dialog.alert( |
| 63 | m(msg("Request no longer waiting")), |
| 64 | m(msg("It expired or was answered elsewhere.")), |
| 65 | [{ text: m(msg("OK")), onPress: () => answer("done") }], |
| 66 | { onDismiss: () => answer("done") }, |
| 67 | ); |
| 68 | } |
| 69 | const retryIfWatched = () => |
| 70 | answer(watchedEnvironments.has(prompt.environmentId) ? "retry" : "done"); |
| 71 | Dialog.alert( |
| 72 | failure, |
| 73 | m(msg("The request is still waiting. Try again.")), |
| 74 | [{ text: m(msg("OK")), onPress: retryIfWatched }], |
| 75 | // Another dialog took the screen; offer the request again on the next update. |
| 76 | { onDismiss: () => answer("later") }, |
| 77 | ); |
| 78 | }); |
| 79 | Dialog.alert( |
| 80 | prompt.title, |
| 81 | prompt.message, |
| 82 | [ |
| 83 | { |
| 84 | text: m(msg("Deny")), |
| 85 | onPress: respond(prompt.deny, m(msg("Could not deny the device"))), |
| 86 | }, |
| 87 | { |
| 88 | text: m(msg("Approve")), |
| 89 | onPress: respond(prompt.approve, m(msg("Could not approve the device"))), |
| 90 | }, |
| 91 | ], |
| 92 | // Another dialog took the screen; offer the request again on the next update. |
| 93 | { cancelable: false, onDismiss: () => answer("later") }, |
| 94 | ); |
| 95 | }); |
| 96 | |
| 97 | function EnvironmentApprovalWatcher(props: { |
| 98 | readonly environmentId: EnvironmentId; |
| 99 | readonly environmentLabel: string; |
| 100 | }) { |
| 101 | const access = useEnvironmentQuery( |
| 102 | authEnvironment.accessChanges({ environmentId: props.environmentId, input: null }), |
| 103 | ); |
| 104 | const approve = useAtomCommand(authEnvironment.approveConnectDevice, { reportFailure: false }); |
| 105 | const deny = useAtomCommand(authEnvironment.revokePairingLink, { reportFailure: false }); |
| 106 | const pairingLinks = |
| 107 | access.data?.type === "snapshot" ? access.data.payload.pairingLinks : undefined; |
| 108 | |
| 109 | const offerPrompts = useEffectEvent((links: ReadonlyArray<AuthPairingLink>) => { |
| 110 | const prompts = connectDeviceApprovalPrompts({ |
| 111 | environmentLabel: props.environmentLabel, |
| 112 | pairingLinks: links, |
| 113 | shownIds: approvalPrompts.known, |
| 114 | }); |
| 115 | approvalPrompts.offer( |
| 116 | prompts.map((prompt) => ({ |
| 117 | ...prompt, |
| 118 | environmentId: props.environmentId, |
| 119 | approve: () => |
| 120 | approve({ |
| 121 | environmentId: props.environmentId, |
| 122 | input: { pairingLinkId: prompt.pairingLinkId }, |
| 123 | }).then((result) => |
| 124 | result._tag !== "Success" ? "failed" : result.value.approved ? "done" : "gone", |
| 125 | ), |
| 126 | deny: () => |
| 127 | deny({ environmentId: props.environmentId, input: { id: prompt.pairingLinkId } }).then( |
| 128 | (result) => |
| 129 | result._tag !== "Success" ? "failed" : result.value.revoked ? "done" : "gone", |
| 130 | ), |
| 131 | })), |
| 132 | ); |
| 133 | }); |
| 134 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- registers this host with the module-level prompt queue and drops its prompts when it goes away |
| 135 | useEffect(() => { |
| 136 | const environmentId = props.environmentId; |
| 137 | watchedEnvironments.add(environmentId); |
| 138 | return () => { |
| 139 | watchedEnvironments.delete(environmentId); |
| 140 | approvalPrompts.drop((prompt) => prompt.environmentId === environmentId); |
| 141 | }; |
| 142 | }, [props.environmentId]); |
| 143 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- a new request on the access stream opens a native alert |
| 144 | useEffect(() => { |
| 145 | if (pairingLinks !== undefined) offerPrompts(pairingLinks); |
| 146 | }, [pairingLinks]); |
| 147 | |
| 148 | return null; |
| 149 | } |