The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { msg, T, useMessages } from "gt-react"; |
| 2 | import { useSyncExternalStore } from "react"; |
| 3 | |
| 4 | import { |
| 5 | completeConfirmDialogClose, |
| 6 | readConfirmDialogState, |
| 7 | respondToConfirmDialog, |
| 8 | subscribeConfirmDialogHost, |
| 9 | } from "../confirmDialog"; |
| 10 | import { |
| 11 | AlertDialog, |
| 12 | AlertDialogClose, |
| 13 | AlertDialogAction, |
| 14 | AlertDialogDescription, |
| 15 | AlertDialogFooter, |
| 16 | AlertDialogHeader, |
| 17 | AlertDialogPopup, |
| 18 | AlertDialogTitle, |
| 19 | } from "./ui/alert-dialog"; |
| 20 | import { Button } from "./ui/button"; |
| 21 | import type { MessageResolver } from "../i18n/messages"; |
| 22 | |
| 23 | type ConfirmationCopy = { |
| 24 | readonly title: string; |
| 25 | readonly description: string | null; |
| 26 | }; |
| 27 | |
| 28 | // LocalApi.confirm takes one string; translated questions may end in a full-width mark. |
| 29 | const QUESTION_MARK = /[??]/; |
| 30 | |
| 31 | function resolveConfirmDialogCopy(message: string, resolve: MessageResolver): ConfirmationCopy { |
| 32 | const normalizedMessage = message.trim(); |
| 33 | const lines = normalizedMessage.split("\n"); |
| 34 | const questionLineIndex = lines.findIndex((line) => QUESTION_MARK.test(line.trim().slice(-1))); |
| 35 | |
| 36 | if (questionLineIndex >= 0) { |
| 37 | const title = lines[questionLineIndex]!.trim(); |
| 38 | const description = lines |
| 39 | .filter((_, index) => index !== questionLineIndex) |
| 40 | .join("\n") |
| 41 | .trim(); |
| 42 | return { title, description: description || null }; |
| 43 | } |
| 44 | |
| 45 | const questionMarkIndex = normalizedMessage.search(QUESTION_MARK); |
| 46 | if (questionMarkIndex >= 0) { |
| 47 | return { |
| 48 | title: normalizedMessage.slice(0, questionMarkIndex + 1).trim(), |
| 49 | description: normalizedMessage.slice(questionMarkIndex + 1).trim() || null, |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | return { |
| 54 | title: resolve(msg("Confirm action")), |
| 55 | description: normalizedMessage || resolve(msg("This action requires your confirmation.")), |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | export function ConfirmDialogHost() { |
| 60 | const m = useMessages(); |
| 61 | const state = useSyncExternalStore( |
| 62 | subscribeConfirmDialogHost, |
| 63 | readConfirmDialogState, |
| 64 | readConfirmDialogState, |
| 65 | ); |
| 66 | |
| 67 | const copy = resolveConfirmDialogCopy(state.status === "idle" ? "" : state.message, m); |
| 68 | const confirmVariant = state.status === "idle" ? "default" : state.variant; |
| 69 | const focusConfirm = state.status !== "idle" && state.initialFocus === "confirm"; |
| 70 | const onCancel = () => respondToConfirmDialog(false); |
| 71 | const onConfirm = () => respondToConfirmDialog(true); |
| 72 | |
| 73 | return ( |
| 74 | <AlertDialog |
| 75 | open={state.status === "confirming"} |
| 76 | onOpenChange={(open) => { |
| 77 | if (!open) onCancel(); |
| 78 | }} |
| 79 | onOpenChangeComplete={(open) => { |
| 80 | if (!open) completeConfirmDialogClose(); |
| 81 | }} |
| 82 | > |
| 83 | <AlertDialogPopup> |
| 84 | <AlertDialogHeader> |
| 85 | <AlertDialogTitle>{copy.title}</AlertDialogTitle> |
| 86 | {copy.description ? ( |
| 87 | <AlertDialogDescription className="whitespace-pre-line"> |
| 88 | {copy.description} |
| 89 | </AlertDialogDescription> |
| 90 | ) : null} |
| 91 | </AlertDialogHeader> |
| 92 | <AlertDialogFooter> |
| 93 | <AlertDialogClose render={<Button variant="outline" />}> |
| 94 | <T>Cancel</T> |
| 95 | </AlertDialogClose> |
| 96 | {focusConfirm && confirmVariant === "default" ? ( |
| 97 | <AlertDialogAction onClick={onConfirm}> |
| 98 | <T>Confirm</T> |
| 99 | </AlertDialogAction> |
| 100 | ) : ( |
| 101 | <Button variant={confirmVariant} onClick={onConfirm}> |
| 102 | <T>Confirm</T> |
| 103 | </Button> |
| 104 | )} |
| 105 | </AlertDialogFooter> |
| 106 | </AlertDialogPopup> |
| 107 | </AlertDialog> |
| 108 | ); |
| 109 | } |