Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

terminalCloseConfirm.ts

56 lines1.8 KB

1import { msg, plural, part } from "gt-react";
2​
3import { ensureClientSettingsHydrated, getClientSettings } from "~/hooks/useSettings";
4import { m } from "~/i18n/messages";
5import { readLocalApi } from "~/localApi";
6​
7let pendingConfirmations = 0;
8​
9/** Whether a terminal-close action is waiting on settings or user confirmation. */
10export function isTerminalCloseConfirmPending(): boolean {
11 return pendingConfirmations > 0;
13​
14export 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: labels[0] }}"?`),
33 m(msg("This stops the running process and clears its history.")),
34 ].join("\n")
35 : [
36 m(
37 msg`Close ${plural(
38 { count: labels.length },
39 {
40 one: part`${{ count: labels.length }} terminal`,
41 other: part`${{ count: labels.length }} terminals`,
42 },
43 )}?`,
44 ),
45 m(
46 msg`This stops their running processes and clears their histories: ${{ labels: labels.map((label) => `"${label}"`).join(", ") }}.`,
47 ),
48 ].join("\n"),
49 { variant: "destructive" },
50 );
51 } catch {
52 return false;
53 } finally {
54 pendingConfirmations -= 1;
55 }