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