The best way to control Claude Code, Codex and more from any computer or phone

main
ts 54 lines 1.67 KB
Raw
1 import { msg } from "gt-react";
2
3 import { ensureClientSettingsHydrated, getClientSettings } from "~/hooks/useSettings";
4 import { m } from "~/i18n/messages";
5 import { readLocalApi } from "~/localApi";
6
7 let pendingConfirmations = 0;
8
9 /** Whether a terminal-close action is waiting on settings or user confirmation. */
10 export function isTerminalCloseConfirmPending(): boolean {
11 return pendingConfirmations > 0;
12 }
13
14 export async function confirmTerminalClose(
15 labels: readonly [string, ...string[]],
16 ): Promise<boolean> {
17 const localApi = readLocalApi();
18 if (!localApi) return true;
19 pendingConfirmations += 1;
20 try {
21 let shouldConfirm = true;
22 try {
23 await ensureClientSettingsHydrated();
24 shouldConfirm = getClientSettings().confirmTerminalClose;
25 } catch {
26 // If saved settings cannot be read, keep the destructive confirmation.
27 }
28 if (!shouldConfirm) return true;
29 return await localApi.dialogs.confirm(
30 labels.length === 1
31 ? [
32 m(msg('Close terminal "{label}"?', { label: labels[0] })),
33 m(msg("This stops the running process and clears its history.")),
34 ].join("\n")
35 : [
36 m(
37 msg("Close {count, plural, one {{count} terminal} other {{count} terminals}}?", {
38 count: labels.length,
39 }),
40 ),
41 m(
42 msg("This stops their running processes and clears their histories: {labels}.", {
43 labels: labels.map((label) => `"${label}"`).join(", "),
44 }),
45 ),
46 ].join("\n"),
47 { variant: "destructive" },
48 );
49 } catch {
50 return false;
51 } finally {
52 pendingConfirmations -= 1;
53 }
54 }