Open History to see the last commit
1import type { WorktreeLayoutCommandKind } from "@t3tools/contracts";
2import { msg, T, useMessages, Var } from "gt-react";
3import { useSyncExternalStore } from "react";
4
5import {
6 readWorktreeLayoutApprovalState,
7 respondToWorktreeLayoutApproval,
8 subscribeWorktreeLayoutApprovalHost,
9} from "../worktreeLayoutApproval";
10import {
11 AlertDialog,
12 AlertDialogClose,
13 AlertDialogDescription,
14 AlertDialogFooter,
15 AlertDialogHeader,
16 AlertDialogPopup,
17 AlertDialogTitle,
18} from "./ui/alert-dialog";
19import { Button } from "./ui/button";
20
21export const WORKTREE_LAYOUT_COMMAND_LABELS: Record<WorktreeLayoutCommandKind, string> = {
22 create: msg("Runs when a worktree thread starts, and when its folder is restored"),
23 remove: msg("Runs when a thread's worktree is removed"),
24};
25
26/** Asks before a project's own worktree commands run for the first time, or after they change. */
27export function WorktreeLayoutApprovalDialogHost() {
28 const m = useMessages();
29 const { request, open } = useSyncExternalStore(
30 subscribeWorktreeLayoutApprovalHost,
31 readWorktreeLayoutApprovalState,
32 readWorktreeLayoutApprovalState,
33 );
34
35 return (
36 <AlertDialog
37 open={open}
38 onOpenChange={(next) => {
39 if (!next && request !== null) respondToWorktreeLayoutApproval(request.id, false);
40 }}
41 >
42 <AlertDialogPopup className="max-w-2xl">
43 <AlertDialogHeader>
44 <AlertDialogTitle>
45 <T>Let this project run its worktree commands?</T>
46 </AlertDialogTitle>
47 <AlertDialogDescription>
48 <T>
49 <Var>{request?.projectTitle ?? ""}</Var> sets up worktree threads with its own
50 commands from <code>t3.json</code>. They run on this environment's machine with your
51 permissions, outside any agent sandbox. You are asked again whenever their text
52 changes.
53 </T>
54 </AlertDialogDescription>
55 <div className="flex flex-col gap-3 pt-2 text-left">
56 {request?.commands.map((command) => (
57 <div key={command.kind} className="flex min-w-0 flex-col gap-1">
58 <span className="text-xs text-muted-foreground">
59 {m(WORKTREE_LAYOUT_COMMAND_LABELS[command.kind])}
60 </span>
61 <pre className="max-h-48 overflow-auto whitespace-pre-wrap wrap-anywhere rounded-md border bg-muted/40 px-3 py-2 font-mono text-xs">
62 {command.command}
63 </pre>
64 </div>
65 ))}
66 </div>
67 </AlertDialogHeader>
68 {/* New buttons for each request: a press begun on one request cannot answer the next. */}
69 <AlertDialogFooter key={request?.id}>
70 <AlertDialogClose render={<Button variant="outline" />}>
71 <T>Cancel</T>
72 </AlertDialogClose>
73 <Button
74 onClick={() => {
75 // The request whose commands this render shows, not whichever is pending by now.
76 if (request !== null) respondToWorktreeLayoutApproval(request.id, true);
77 }}
78 >
79 <T>Approve and continue</T>
80 </Button>
81 </AlertDialogFooter>
82 </AlertDialogPopup>
83 </AlertDialog>
84 );
85}