1import { readLocalApi } from "~/localApi";
2
3let pendingConfirmations = 0;
4
5export function isTerminalCloseConfirmPending(): boolean {
6 return pendingConfirmations > 0;
7}
8
9export async function confirmTerminalClose(
10 labels: readonly [string, ...string[]],
11): Promise<boolean> {
12 const localApi = readLocalApi();
13 if (!localApi) return true;
14 pendingConfirmations += 1;
15 try {
16 return await localApi.dialogs.confirm(
17 labels.length === 1
18 ? [
19 `Close terminal "${labels[0]}"?`,
20 "This stops the running process and clears its history.",
21 ].join("\n")
22 : [
23 `Close ${labels.length} terminals?`,
24 `This stops their running processes and clears their histories: ${labels
25 .map((label) => `"${label}"`)
26 .join(", ")}.`,
27 ].join("\n"),
28 { variant: "destructive" },
29 );
30 } catch {
31 return false;
32 } finally {
33 pendingConfirmations -= 1;
34 }
35}