The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { settlePromise } from "@t3tools/client-runtime/state/runtime"; |
| 3 | import type { DesktopSshPasswordPromptRequest } from "@t3tools/contracts"; |
| 4 | import * as AsyncResult from "effect/reactivity/AsyncResult"; |
| 5 | import * as Cause from "effect/Cause"; |
| 6 | import { msg, T, useGT, Var } from "gt-react"; |
| 7 | import { useId, useRef, useState, useSyncExternalStore } from "react"; |
| 8 | |
| 9 | import { m } from "../../i18n/messages"; |
| 10 | |
| 11 | import { useDeadlinePassed } from "../../hooks/useDeadline"; |
| 12 | import { useNowSecond } from "../../hooks/useNowSecond"; |
| 13 | |
| 14 | import { Button } from "../ui/button"; |
| 15 | import { |
| 16 | Dialog, |
| 17 | DialogDescription, |
| 18 | DialogFooter, |
| 19 | DialogHeader, |
| 20 | DialogPanel, |
| 21 | DialogPopup, |
| 22 | DialogTitle, |
| 23 | } from "../ui/dialog"; |
| 24 | import { Input } from "../ui/input"; |
| 25 | |
| 26 | function describeSshTarget(request: DesktopSshPasswordPromptRequest): string { |
| 27 | return request.username ? `${request.username}@${request.destination}` : request.destination; |
| 28 | } |
| 29 | |
| 30 | function formatRemainingSeconds(seconds: number): string { |
| 31 | return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`; |
| 32 | } |
| 33 | |
| 34 | function getPromptErrorMessage(error: unknown): string { |
| 35 | const message = thrownErrorMessage(error, m) ?? m(msg("SSH verification prompt failed.")); |
| 36 | return message.includes("expired") || message.includes("no longer pending") |
| 37 | ? m(msg("This SSH verification prompt expired. Try connecting again.")) |
| 38 | : message; |
| 39 | } |
| 40 | |
| 41 | let promptQueue: readonly DesktopSshPasswordPromptRequest[] = []; |
| 42 | const promptQueueListeners = new Set<() => void>(); |
| 43 | |
| 44 | function setPromptQueue(next: readonly DesktopSshPasswordPromptRequest[]) { |
| 45 | promptQueue = next; |
| 46 | for (const listener of promptQueueListeners) listener(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Queues the desktop's SSH password prompts for the life of the page, so a prompt |
| 51 | * that arrives while no dialog is mounted still waits for one. |
| 52 | */ |
| 53 | export function startSshPasswordPromptQueue(): () => void { |
| 54 | const bridge = window.desktopBridge; |
| 55 | if (!bridge) return () => undefined; |
| 56 | const stopPrompts = bridge.onSshPasswordPrompt((request) => { |
| 57 | if (promptQueue.some((queued) => queued.requestId === request.requestId)) return; |
| 58 | setPromptQueue([...promptQueue, request]); |
| 59 | }); |
| 60 | const stopSettled = bridge.onSshPasswordPromptSettled(removeSshPasswordPrompt); |
| 61 | return () => { |
| 62 | stopPrompts(); |
| 63 | stopSettled(); |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | function subscribePromptQueue(listener: () => void): () => void { |
| 68 | promptQueueListeners.add(listener); |
| 69 | return () => { |
| 70 | promptQueueListeners.delete(listener); |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | const readPromptQueue = () => promptQueue; |
| 75 | |
| 76 | function removeSshPasswordPrompt(requestId: string) { |
| 77 | if (!promptQueue.some((request) => request.requestId === requestId)) return; |
| 78 | setPromptQueue(promptQueue.filter((request) => request.requestId !== requestId)); |
| 79 | } |
| 80 | |
| 81 | export function SshPasswordPromptDialog() { |
| 82 | const queue = useSyncExternalStore(subscribePromptQueue, readPromptQueue, readPromptQueue); |
| 83 | const currentRequest = queue[0] ?? null; |
| 84 | |
| 85 | if (!currentRequest) { |
| 86 | return null; |
| 87 | } |
| 88 | |
| 89 | return <ActiveSshPasswordPrompt key={currentRequest.requestId} request={currentRequest} />; |
| 90 | } |
| 91 | |
| 92 | function ActiveSshPasswordPrompt({ |
| 93 | request, |
| 94 | }: { |
| 95 | readonly request: DesktopSshPasswordPromptRequest; |
| 96 | }) { |
| 97 | const gt = useGT(); |
| 98 | const [password, setPassword] = useState(""); |
| 99 | const [isResponding, setIsResponding] = useState(false); |
| 100 | const [responseError, setResponseError] = useState<string | null>(null); |
| 101 | const inputRef = useRef<HTMLInputElement | null>(null); |
| 102 | const isRespondingRef = useRef(false); |
| 103 | const formId = useId(); |
| 104 | |
| 105 | const expiresAtMs = Date.parse(request.expiresAt); |
| 106 | const now = useNowSecond(); |
| 107 | const isExpired = useDeadlinePassed(Number.isFinite(expiresAtMs) ? expiresAtMs : null); |
| 108 | const remainingMs = Number.isFinite(expiresAtMs) |
| 109 | ? isExpired |
| 110 | ? 0 |
| 111 | : Math.max(0, expiresAtMs - now) |
| 112 | : null; |
| 113 | const remainingSeconds = remainingMs === null ? null : Math.ceil(remainingMs / 1_000); |
| 114 | const remainingLabel = |
| 115 | remainingSeconds === null ? null : formatRemainingSeconds(remainingSeconds); |
| 116 | const visibleResponseError = isExpired |
| 117 | ? gt("This SSH verification prompt expired. Try connecting again.") |
| 118 | : responseError; |
| 119 | |
| 120 | const respond = async (nextPassword: string | null) => { |
| 121 | if (isRespondingRef.current) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | const requestId = request.requestId; |
| 126 | if (nextPassword !== null && isExpired) { |
| 127 | setResponseError(gt("This SSH verification prompt expired. Try connecting again.")); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | isRespondingRef.current = true; |
| 132 | setIsResponding(true); |
| 133 | setResponseError(null); |
| 134 | const result = await settlePromise(async () => |
| 135 | window.desktopBridge?.resolveSshPasswordPrompt(requestId, nextPassword), |
| 136 | ); |
| 137 | if (AsyncResult.isSuccess(result) || nextPassword === null) { |
| 138 | removeSshPasswordPrompt(requestId); |
| 139 | } else { |
| 140 | setResponseError(getPromptErrorMessage(Cause.squash(result.cause))); |
| 141 | } |
| 142 | isRespondingRef.current = false; |
| 143 | setIsResponding(false); |
| 144 | }; |
| 145 | |
| 146 | const dismissExpiredPrompt = () => { |
| 147 | removeSshPasswordPrompt(request.requestId); |
| 148 | }; |
| 149 | |
| 150 | const cancelPrompt = () => { |
| 151 | if (isExpired) { |
| 152 | dismissExpiredPrompt(); |
| 153 | return; |
| 154 | } |
| 155 | void respond(null); |
| 156 | }; |
| 157 | |
| 158 | const target = describeSshTarget(request); |
| 159 | |
| 160 | return ( |
| 161 | <Dialog |
| 162 | open |
| 163 | onOpenChange={(open) => { |
| 164 | if (!open) { |
| 165 | cancelPrompt(); |
| 166 | } |
| 167 | }} |
| 168 | > |
| 169 | <DialogPopup className="max-w-md" initialFocus={inputRef} showCloseButton={false}> |
| 170 | <DialogHeader> |
| 171 | <DialogTitle> |
| 172 | <T>SSH verification required</T> |
| 173 | </DialogTitle> |
| 174 | <DialogDescription> |
| 175 | <T> |
| 176 | Enter the password or verification code required by{" "} |
| 177 | <code> |
| 178 | <Var>{target}</Var> |
| 179 | </code> |
| 180 | . T3 Code passes your response to the local SSH process and keeps it in memory to |
| 181 | reuse for this host. T3 Code does not write it to disk. |
| 182 | </T> |
| 183 | </DialogDescription> |
| 184 | </DialogHeader> |
| 185 | <DialogPanel scrollFade={false}> |
| 186 | <form |
| 187 | className="space-y-3" |
| 188 | id={formId} |
| 189 | onSubmit={(event) => { |
| 190 | event.preventDefault(); |
| 191 | void respond(password); |
| 192 | }} |
| 193 | > |
| 194 | <div className="space-y-2"> |
| 195 | <div className="flex items-center justify-between gap-3"> |
| 196 | <p className="text-sm font-medium text-foreground">{request.prompt}</p> |
| 197 | {remainingLabel ? ( |
| 198 | <span |
| 199 | className={ |
| 200 | isExpired |
| 201 | ? "shrink-0 text-xs font-medium text-destructive" |
| 202 | : "shrink-0 text-xs text-muted-foreground" |
| 203 | } |
| 204 | > |
| 205 | {isExpired ? gt("Expired") : remainingLabel} |
| 206 | </span> |
| 207 | ) : null} |
| 208 | </div> |
| 209 | <Input |
| 210 | ref={inputRef} |
| 211 | autoComplete="current-password" |
| 212 | disabled={isResponding || isExpired} |
| 213 | name="ssh-password" |
| 214 | type="password" |
| 215 | value={password} |
| 216 | onChange={(event) => setPassword(event.target.value)} |
| 217 | /> |
| 218 | </div> |
| 219 | {visibleResponseError ? ( |
| 220 | <p className="text-sm text-destructive">{visibleResponseError}</p> |
| 221 | ) : ( |
| 222 | <p className="text-sm text-muted-foreground"> |
| 223 | <T> |
| 224 | SSH keys may replace a password, but your server can still require a verification |
| 225 | code. |
| 226 | </T> |
| 227 | </p> |
| 228 | )} |
| 229 | </form> |
| 230 | </DialogPanel> |
| 231 | <DialogFooter> |
| 232 | <Button disabled={isResponding} type="button" variant="outline" onClick={cancelPrompt}> |
| 233 | {isExpired ? gt("Dismiss") : gt("Cancel")} |
| 234 | </Button> |
| 235 | <Button disabled={isResponding || isExpired} form={formId} type="submit"> |
| 236 | <T>Continue</T> |
| 237 | </Button> |
| 238 | </DialogFooter> |
| 239 | </DialogPopup> |
| 240 | </Dialog> |
| 241 | ); |
| 242 | } |