The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 25, 06:15 UTC
Scope git action progress toasts to the originating thread (#1675)
Julius Marminge committed
Apr 2, 2026 at 11:39 UTC
5ce5472bc389e178132df40f316014d714f3275d
3 files changed
+290
-50
apps/web/src/components/GitActionsControl.browser.tsx
new
+238
@@ -0,0 +1,238 @@
1
+import { ThreadId } from "@t3tools/contracts";
2
+import { useState } from "react";
3
+import { afterEach, describe, expect, it, vi } from "vitest";
4
+import { render } from "vitest-browser-react";
5
+
6
+const THREAD_A = ThreadId.makeUnsafe("thread-a");
7
+const THREAD_B = ThreadId.makeUnsafe("thread-b");
8
+const GIT_CWD = "/repo/project";
9
+const BRANCH_NAME = "feature/toast-scope";
10
+
11
+const {
12
+ invalidateGitQueriesSpy,
13
+ invalidateGitStatusQuerySpy,
14
+ runStackedActionMutateAsyncSpy,
15
+ setThreadBranchSpy,
16
+ toastAddSpy,
17
+ toastCloseSpy,
18
+ toastPromiseSpy,
19
+ toastUpdateSpy,
20
+} = vi.hoisted(() => ({
21
+ invalidateGitQueriesSpy: vi.fn(() => Promise.resolve()),
22
+ invalidateGitStatusQuerySpy: vi.fn(() => Promise.resolve()),
23
+ runStackedActionMutateAsyncSpy: vi.fn(() => new Promise<never>(() => undefined)),
24
+ setThreadBranchSpy: vi.fn(),
25
+ toastAddSpy: vi.fn(() => "toast-1"),
26
+ toastCloseSpy: vi.fn(),
27
+ toastPromiseSpy: vi.fn(),
28
+ toastUpdateSpy: vi.fn(),
29
+}));
30
+
31
+vi.mock("@tanstack/react-query", async () => {
32
+ const actual =
33
+ await vi.importActual<typeof import("@tanstack/react-query")>("@tanstack/react-query");
34
+
35
+ return {
36
+ ...actual,
37
+ useIsMutating: vi.fn(() => 0),
38
+ useMutation: vi.fn((options: { __kind?: string }) => {
39
+ if (options.__kind === "run-stacked-action") {
40
+ return {
41
+ mutateAsync: runStackedActionMutateAsyncSpy,
42
+ isPending: false,
43
+ };
44
+ }
45
+
46
+ if (options.__kind === "pull") {
47
+ return {
48
+ mutateAsync: vi.fn(),
49
+ isPending: false,
50
+ };
51
+ }
52
+
53
+ return {
54
+ mutate: vi.fn(),
55
+ mutateAsync: vi.fn(),
56
+ isPending: false,
57
+ };
58
+ }),
59
+ useQuery: vi.fn((options: { queryKey?: string[] }) => {
60
+ if (options.queryKey?.[0] === "git-status") {
61
+ return {
62
+ data: {
63
+ branch: BRANCH_NAME,
64
+ hasWorkingTreeChanges: false,
65
+ workingTree: { files: [], insertions: 0, deletions: 0 },
66
+ hasUpstream: true,
67
+ aheadCount: 1,
68
+ behindCount: 0,
69
+ pr: null,
70
+ },
71
+ error: null,
72
+ };
73
+ }
74
+
75
+ if (options.queryKey?.[0] === "git-branches") {
76
+ return {
77
+ data: {
78
+ isRepo: true,
79
+ hasOriginRemote: true,
80
+ branches: [
81
+ {
82
+ name: BRANCH_NAME,
83
+ current: true,
84
+ isDefault: false,
85
+ worktreePath: null,
86
+ },
87
+ ],
88
+ },
89
+ error: null,
90
+ };
91
+ }
92
+
93
+ return { data: null, error: null };
94
+ }),
95
+ useQueryClient: vi.fn(() => ({})),
96
+ };
97
+});
98
+
99
+vi.mock("~/components/ui/toast", () => ({
100
+ toastManager: {
101
+ add: toastAddSpy,
102
+ close: toastCloseSpy,
103
+ promise: toastPromiseSpy,
104
+ update: toastUpdateSpy,
105
+ },
106
+}));
107
+
108
+vi.mock("~/editorPreferences", () => ({
109
+ openInPreferredEditor: vi.fn(),
110
+}));
111
+
112
+vi.mock("~/lib/gitReactQuery", () => ({
113
+ gitBranchesQueryOptions: vi.fn(() => ({ queryKey: ["git-branches"] })),
114
+ gitInitMutationOptions: vi.fn(() => ({ __kind: "init" })),
115
+ gitMutationKeys: {
116
+ pull: vi.fn(() => ["pull"]),
117
+ runStackedAction: vi.fn(() => ["run-stacked-action"]),
118
+ },
119
+ gitPullMutationOptions: vi.fn(() => ({ __kind: "pull" })),
120
+ gitRunStackedActionMutationOptions: vi.fn(() => ({ __kind: "run-stacked-action" })),
121
+ gitStatusQueryOptions: vi.fn(() => ({ queryKey: ["git-status"] })),
122
+ invalidateGitQueries: invalidateGitQueriesSpy,
123
+ invalidateGitStatusQuery: invalidateGitStatusQuerySpy,
124
+}));
125
+
126
+vi.mock("~/lib/utils", async () => {
127
+ const actual = await vi.importActual<typeof import("~/lib/utils")>("~/lib/utils");
128
+
129
+ return {
130
+ ...actual,
131
+ newCommandId: vi.fn(() => "command-1"),
132
+ randomUUID: vi.fn(() => "action-1"),
133
+ };
134
+});
135
+
136
+vi.mock("~/nativeApi", () => ({
137
+ readNativeApi: vi.fn(() => null),
138
+}));
139
+
140
+vi.mock("~/store", () => ({
141
+ useStore: (selector: (state: unknown) => unknown) =>
142
+ selector({
143
+ setThreadBranch: setThreadBranchSpy,
144
+ threads: [
145
+ { id: THREAD_A, branch: BRANCH_NAME, worktreePath: null },
146
+ { id: THREAD_B, branch: BRANCH_NAME, worktreePath: null },
147
+ ],
148
+ }),
149
+}));
150
+
151
+vi.mock("~/terminal-links", () => ({
152
+ resolvePathLinkTarget: vi.fn(),
153
+}));
154
+
155
+import GitActionsControl from "./GitActionsControl";
156
+
157
+function findButtonByText(text: string): HTMLButtonElement | null {
158
+ return (Array.from(document.querySelectorAll("button")).find((button) =>
159
+ button.textContent?.includes(text),
160
+ ) ?? null) as HTMLButtonElement | null;
161
+}
162
+
163
+function Harness() {
164
+ const [activeThreadId, setActiveThreadId] = useState(THREAD_A);
165
+
166
+ return (
167
+ <>
168
+ <button type="button" onClick={() => setActiveThreadId(THREAD_B)}>
169
+ Switch thread
170
+ </button>
171
+ <GitActionsControl gitCwd={GIT_CWD} activeThreadId={activeThreadId} />
172
+ </>
173
+ );
174
+}
175
+
176
+describe("GitActionsControl thread-scoped progress toast", () => {
177
+ afterEach(() => {
178
+ vi.useRealTimers();
179
+ vi.clearAllMocks();
180
+ document.body.innerHTML = "";
181
+ });
182
+
183
+ it("keeps an in-flight git action toast pinned to the thread that started it", async () => {
184
+ vi.useFakeTimers();
185
+
186
+ const host = document.createElement("div");
187
+ document.body.append(host);
188
+ const screen = await render(<Harness />, { container: host });
189
+
190
+ try {
191
+ const quickActionButton = findButtonByText("Push & create PR");
192
+ expect(quickActionButton, 'Unable to find button containing "Push & create PR"').toBeTruthy();
193
+ if (!(quickActionButton instanceof HTMLButtonElement)) {
194
+ throw new Error('Unable to find button containing "Push & create PR"');
195
+ }
196
+ quickActionButton.click();
197
+
198
+ expect(toastAddSpy).toHaveBeenCalledWith(
199
+ expect.objectContaining({
200
+ data: { threadId: THREAD_A },
201
+ title: "Pushing...",
202
+ type: "loading",
203
+ }),
204
+ );
205
+
206
+ await vi.advanceTimersByTimeAsync(1_000);
207
+
208
+ expect(toastUpdateSpy).toHaveBeenLastCalledWith(
209
+ "toast-1",
210
+ expect.objectContaining({
211
+ data: { threadId: THREAD_A },
212
+ title: "Pushing...",
213
+ type: "loading",
214
+ }),
215
+ );
216
+
217
+ const switchThreadButton = findButtonByText("Switch thread");
218
+ expect(switchThreadButton, 'Unable to find button containing "Switch thread"').toBeTruthy();
219
+ if (!(switchThreadButton instanceof HTMLButtonElement)) {
220
+ throw new Error('Unable to find button containing "Switch thread"');
221
+ }
222
+ switchThreadButton.click();
223
+ await vi.advanceTimersByTimeAsync(1_000);
224
+
225
+ expect(toastUpdateSpy).toHaveBeenLastCalledWith(
226
+ "toast-1",
227
+ expect.objectContaining({
228
+ data: { threadId: THREAD_A },
229
+ title: "Pushing...",
230
+ type: "loading",
231
+ }),
232
+ );
233
+ } finally {
234
+ await screen.unmount();
235
+ host.remove();
236
+ }
237
+ });
238
+});
apps/web/src/components/GitActionsControl.tsx
+51
-49
@@ -38,7 +38,7 @@ import { Menu, MenuItem, MenuPopup, MenuTrigger } from "~/components/ui/menu";
38
import { Popover, PopoverPopup, PopoverTrigger } from "~/components/ui/popover";
39
import { ScrollArea } from "~/components/ui/scroll-area";
40
import { Textarea } from "~/components/ui/textarea";
41
-import { toastManager } from "~/components/ui/toast";
41
+import { toastManager, type ThreadToastData } from "~/components/ui/toast";
42
import { openInPreferredEditor } from "~/editorPreferences";
43
import {
44
gitBranchesQueryOptions,
@@ -73,6 +73,7 @@ type GitActionToastId = ReturnType<typeof toastManager.add>;
73
74
interface ActiveGitActionProgress {
75
toastId: GitActionToastId;
76
+ toastData: ThreadToastData | undefined;
77
actionId: string;
78
title: string;
79
phaseStartedAtMs: number | null;
@@ -223,6 +224,7 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions
224
const [pendingDefaultBranchAction, setPendingDefaultBranchAction] =
225
useState<PendingDefaultBranchAction | null>(null);
226
const activeGitActionProgressRef = useRef<ActiveGitActionProgress | null>(null);
227
+ let runGitActionWithToast: (input: RunGitActionWithToastInput) => Promise<void>;
228
229
const updateActiveProgressToast = useCallback(() => {
230
const progress = activeGitActionProgressRef.current;
@@ -234,9 +236,9 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions
236
title: progress.title,
237
description: resolveProgressDescription(progress),
238
timeout: 0,
237
- data: threadToastData,
239
+ data: progress.toastData,
240
});
239
- }, [threadToastData]);
241
+ }, []);
242
243
const persistThreadBranchSync = useCallback(
244
(branch: string | null) => {
@@ -402,7 +404,7 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions
404
});
405
}, [gitStatusForActions, threadToastData]);
406
405
- const runGitActionWithToast = useEffectEvent(
407
+ runGitActionWithToast = useEffectEvent(
408
async ({
409
action,
410
commitMessage,
@@ -455,6 +457,7 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions
457
action === "create_pr" &&
458
(!actionStatus?.hasUpstream || (actionStatus?.aheadCount ?? 0) > 0),
459
});
460
+ const scopedToastData = threadToastData ? { ...threadToastData } : undefined;
461
const actionId = randomUUID();
462
const resolvedProgressToastId =
463
progressToastId ??
@@ -463,11 +466,12 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions
466
title: progressStages[0] ?? "Running git action...",
467
description: "Waiting for Git...",
468
timeout: 0,
466
- data: threadToastData,
469
+ data: scopedToastData,
470
});
471
472
activeGitActionProgressRef.current = {
473
toastId: resolvedProgressToastId,
474
+ toastData: scopedToastData,
475
actionId,
476
title: progressStages[0] ?? "Running git action...",
477
phaseStartedAtMs: null,
@@ -483,7 +487,7 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions
487
title: progressStages[0] ?? "Running git action...",
488
description: "Waiting for Git...",
489
timeout: 0,
486
- data: threadToastData,
490
+ data: scopedToastData,
491
});
492
}
493
@@ -587,30 +591,38 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions
591
};
592
}
593
590
- toastManager.update(resolvedProgressToastId, {
594
+ const successToastBase = {
595
type: "success",
596
title: result.toast.title,
597
description: result.toast.description,
598
timeout: 0,
599
data: {
596
- ...threadToastData,
600
+ ...scopedToastData,
601
dismissAfterVisibleMs: 10_000,
602
},
599
- ...(toastActionProps ? { actionProps: toastActionProps } : {}),
600
- });
603
+ } as const;
604
+
605
+ if (toastActionProps) {
606
+ toastManager.update(resolvedProgressToastId, {
607
+ ...successToastBase,
608
+ actionProps: toastActionProps,
609
+ });
610
+ } else {
611
+ toastManager.update(resolvedProgressToastId, successToastBase);
612
+ }
613
} catch (err) {
614
activeGitActionProgressRef.current = null;
615
toastManager.update(resolvedProgressToastId, {
616
type: "error",
617
title: "Action failed",
618
description: err instanceof Error ? err.message : "An error occurred.",
607
- data: threadToastData,
619
+ data: scopedToastData,
620
});
621
}
622
},
623
);
624
613
- const continuePendingDefaultBranchAction = useCallback(() => {
625
+ const continuePendingDefaultBranchAction = () => {
626
if (!pendingDefaultBranchAction) return;
627
const { action, commitMessage, onConfirmed, filePaths } = pendingDefaultBranchAction;
628
setPendingDefaultBranchAction(null);
@@ -621,9 +633,9 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions
633
...(filePaths ? { filePaths } : {}),
634
skipDefaultBranchPrompt: true,
635
});
624
- }, [pendingDefaultBranchAction]);
636
+ };
637
626
- const checkoutFeatureBranchAndContinuePendingAction = useCallback(() => {
638
+ const checkoutFeatureBranchAndContinuePendingAction = () => {
639
if (!pendingDefaultBranchAction) return;
640
const { action, commitMessage, onConfirmed, filePaths } = pendingDefaultBranchAction;
641
setPendingDefaultBranchAction(null);
@@ -635,9 +647,9 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions
647
featureBranch: true,
648
skipDefaultBranchPrompt: true,
649
});
638
- }, [pendingDefaultBranchAction]);
650
+ };
651
640
- const runDialogActionOnNewBranch = useCallback(() => {
652
+ const runDialogActionOnNewBranch = () => {
653
if (!isCommitDialogOpen) return;
654
const commitMessage = dialogCommitMessage.trim();
655
@@ -653,9 +665,9 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions
665
featureBranch: true,
666
skipDefaultBranchPrompt: true,
667
});
656
- }, [allSelected, isCommitDialogOpen, dialogCommitMessage, selectedFiles]);
668
+ };
669
658
- const runQuickAction = useCallback(() => {
670
+ const runQuickAction = () => {
671
if (quickAction.kind === "open_pr") {
672
void openExistingPr();
673
return;
@@ -693,31 +705,28 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions
705
if (quickAction.action) {
706
void runGitActionWithToast({ action: quickAction.action });
707
}
696
- }, [openExistingPr, pullMutation, quickAction, threadToastData]);
708
+ };
709
698
- const openDialogForMenuItem = useCallback(
699
- (item: GitActionMenuItem) => {
700
- if (item.disabled) return;
701
- if (item.kind === "open_pr") {
702
- void openExistingPr();
703
- return;
704
- }
705
- if (item.dialogAction === "push") {
706
- void runGitActionWithToast({ action: "push" });
707
- return;
708
- }
709
- if (item.dialogAction === "create_pr") {
710
- void runGitActionWithToast({ action: "create_pr" });
711
- return;
712
- }
713
- setExcludedFiles(new Set());
714
- setIsEditingFiles(false);
715
- setIsCommitDialogOpen(true);
716
- },
717
- [openExistingPr, setIsCommitDialogOpen],
718
- );
710
+ const openDialogForMenuItem = (item: GitActionMenuItem) => {
711
+ if (item.disabled) return;
712
+ if (item.kind === "open_pr") {
713
+ void openExistingPr();
714
+ return;
715
+ }
716
+ if (item.dialogAction === "push") {
717
+ void runGitActionWithToast({ action: "push" });
718
+ return;
719
+ }
720
+ if (item.dialogAction === "create_pr") {
721
+ void runGitActionWithToast({ action: "create_pr" });
722
+ return;
723
+ }
724
+ setExcludedFiles(new Set());
725
+ setIsEditingFiles(false);
726
+ setIsCommitDialogOpen(true);
727
+ };
728
720
- const runDialogAction = useCallback(() => {
729
+ const runDialogAction = () => {
730
if (!isCommitDialogOpen) return;
731
const commitMessage = dialogCommitMessage.trim();
732
setIsCommitDialogOpen(false);
@@ -729,14 +738,7 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions
738
...(commitMessage ? { commitMessage } : {}),
739
...(!allSelected ? { filePaths: selectedFiles.map((f) => f.path) } : {}),
740
});
732
- }, [
733
- allSelected,
734
- dialogCommitMessage,
735
- isCommitDialogOpen,
736
- selectedFiles,
737
- setDialogCommitMessage,
738
- setIsCommitDialogOpen,
739
- ]);
741
+ };
742
743
const openChangedFileInEditor = useCallback(
744
(filePath: string) => {
apps/web/src/components/ui/toast.tsx
+1
-1
@@ -19,7 +19,7 @@ import { buttonVariants } from "~/components/ui/button";
19
import { useCopyToClipboard } from "~/hooks/useCopyToClipboard";
20
import { buildVisibleToastLayout, shouldHideCollapsedToastContent } from "./toast.logic";
21
22
-type ThreadToastData = {
22
+export type ThreadToastData = {
23
threadId?: ThreadId | null;
24
tooltipStyle?: boolean;
25
dismissAfterVisibleMs?: number;