The best way to control Claude Code, Codex and more from any computer or phone
Use React Query mutations for git worktree operations
- Route worktree create/remove calls through shared git mutation options - Invalidate git queries on mutation settle to keep branch/worktree data fresh - Update chat and sidebar flows to use `mutateAsync` for worktree actions
Julius Marminge committed
Feb 15, 2026 at 00:00 UTC
a6b9ece840989e615e3cfc443524d71f4cac6f42
3 files changed
+60
-9
apps/web/src/components/ChatView.tsx
+8
-3
@@ -19,8 +19,11 @@ import {
19
useRef,
20
useState,
21
} from "react";
22
-import { useQuery } from "@tanstack/react-query";
23
-import { gitBranchesQueryOptions } from "~/lib/gitReactQuery";
22
+import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
23
+import {
24
+ gitBranchesQueryOptions,
25
+ gitCreateWorktreeMutationOptions,
26
+} from "~/lib/gitReactQuery";
27
28
import { EDITORS, type EditorId } from "@t3tools/contracts";
29
import { isElectron } from "../env";
@@ -125,6 +128,8 @@ function readFileAsDataUrl(file: File): Promise<string> {
128
export default function ChatView() {
129
const { state, dispatch } = useStore();
130
const api = useNativeApi();
131
+ const queryClient = useQueryClient();
132
+ const createWorktreeMutation = useMutation(gitCreateWorktreeMutationOptions({ api, queryClient }));
133
const [prompt, setPrompt] = useState("");
134
const [composerImages, setComposerImages] = useState<ComposerImageAttachment[]>([]);
135
const [isDragOverComposer, setIsDragOverComposer] = useState(false);
@@ -663,7 +668,7 @@ export default function ChatView() {
668
) {
669
try {
670
const newBranch = `codething/${crypto.randomUUID().slice(0, 8)}`;
666
- const result = await api.git.createWorktree({
671
+ const result = await createWorktreeMutation.mutateAsync({
672
cwd: activeProject.cwd,
673
branch: activeThread.branch,
674
newBranch,
apps/web/src/components/Sidebar.tsx
+6
-2
@@ -1,5 +1,6 @@
1
import { MonitorIcon, MoonIcon, SunIcon, TerminalIcon } from "lucide-react";
2
import { useCallback, useEffect, useMemo, useState } from "react";
3
+import { useMutation, useQueryClient } from "@tanstack/react-query";
4
import { isElectron } from "../env";
5
import { useTheme } from "../hooks/useTheme";
6
import { DEFAULT_MODEL } from "../model-logic";
@@ -12,6 +13,7 @@ import {
13
type Thread,
14
} from "../types";
15
import { useNativeApi } from "../hooks/useNativeApi";
16
+import { gitRemoveWorktreeMutationOptions } from "../lib/gitReactQuery";
17
import { formatWorktreePathForDisplay, getOrphanedWorktreePathForThread } from "../worktreeCleanup";
18
19
const THEME_CYCLE = { system: "light", light: "dark", dark: "system" } as const;
@@ -110,6 +112,8 @@ function terminalStatusIndicator(thread: Thread): TerminalStatusIndicator | null
112
export default function Sidebar() {
113
const { state, dispatch } = useStore();
114
const api = useNativeApi();
115
+ const queryClient = useQueryClient();
116
+ const removeWorktreeMutation = useMutation(gitRemoveWorktreeMutationOptions({ api, queryClient }));
117
const { theme, setTheme } = useTheme();
118
const [addingProject, setAddingProject] = useState(false);
119
const [newCwd, setNewCwd] = useState("");
@@ -307,7 +311,7 @@ export default function Sidebar() {
311
}
312
313
try {
310
- await api.git.removeWorktree({
314
+ await removeWorktreeMutation.mutateAsync({
315
cwd: threadProject.cwd,
316
path: orphanedWorktreePath,
317
});
@@ -315,7 +319,7 @@ export default function Sidebar() {
319
// Worktree deletion is best-effort and should not block thread deletion.
320
}
321
},
318
- [api, dispatch, state.projects, state.threads],
322
+ [api, dispatch, removeWorktreeMutation, state.projects, state.threads],
323
);
324
325
useEffect(() => {
apps/web/src/lib/gitReactQuery.ts
+46
-4
@@ -108,8 +108,8 @@ export function gitRunStackedActionMutationOptions(input: {
108
...(commitMessage ? { commitMessage } : {}),
109
});
110
},
111
- onSuccess: () => {
112
- void invalidateGitQueries(input.queryClient);
111
+ onSettled: async () => {
112
+ await invalidateGitQueries(input.queryClient);
113
},
114
});
115
}
@@ -124,8 +124,50 @@ export function gitPullMutationOptions(input: {
124
if (!input.api || !input.cwd) throw new Error("Git pull is unavailable.");
125
return input.api.git.pull({ cwd: input.cwd });
126
},
127
- onSuccess: () => {
128
- void invalidateGitQueries(input.queryClient);
127
+ onSettled: async () => {
128
+ await invalidateGitQueries(input.queryClient);
129
+ },
130
+ });
131
+}
132
+
133
+export function gitCreateWorktreeMutationOptions(input: {
134
+ api: NativeApi | undefined;
135
+ queryClient: QueryClient;
136
+}) {
137
+ return mutationOptions({
138
+ mutationFn: async ({
139
+ cwd,
140
+ branch,
141
+ newBranch,
142
+ }: {
143
+ cwd: string;
144
+ branch: string;
145
+ newBranch: string;
146
+ }) => {
147
+ if (!input.api) {
148
+ throw new Error("Git worktree creation is unavailable.");
149
+ }
150
+ return input.api.git.createWorktree({ cwd, branch, newBranch });
151
+ },
152
+ onSettled: async () => {
153
+ await invalidateGitQueries(input.queryClient);
154
+ },
155
+ });
156
+}
157
+
158
+export function gitRemoveWorktreeMutationOptions(input: {
159
+ api: NativeApi | undefined;
160
+ queryClient: QueryClient;
161
+}) {
162
+ return mutationOptions({
163
+ mutationFn: async ({ cwd, path }: { cwd: string; path: string }) => {
164
+ if (!input.api) {
165
+ throw new Error("Git worktree removal is unavailable.");
166
+ }
167
+ return input.api.git.removeWorktree({ cwd, path });
168
+ },
169
+ onSettled: async () => {
170
+ await invalidateGitQueries(input.queryClient);
171
},
172
});
173
}