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, 11:00 UTC
Show optimistic active branch during checkout and branch creation
- use `useOptimistic` in `BranchToolbar` to update active branch immediately - keep optimistic branch in sync on success and roll back on mutation errors
Julius Marminge committed
Mar 2, 2026 at 16:27 UTC
b81ec02984c92c9506c7859ab5c967f3b8afae1d
1 file changed
+16
-6
apps/web/src/components/BranchToolbar.tsx
+16
-6
@@ -1,6 +1,6 @@
1
import type { GitBranch, ThreadId } from "@t3tools/contracts";
2
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
3
-import { useState } from "react";
3
+import { useOptimistic, useState } from "react";
4
5
import { newCommandId } from "../lib/utils";
6
import {
@@ -72,10 +72,14 @@ export default function BranchToolbar({
72
73
const branches = dedupeRemoteBranchesWithLocalMatches(branchesQuery.data?.branches ?? []);
74
const currentGitBranch = branches.find((branch) => branch.current)?.name ?? null;
75
- const resolvedActiveBranch =
75
+ const canonicalActiveBranch =
76
effectiveEnvMode === "worktree" && !activeWorktreePath
77
? activeThreadBranch
78
: (currentGitBranch ?? activeThreadBranch);
79
+ const [resolvedActiveBranch, setOptimisticBranch] = useOptimistic(
80
+ canonicalActiveBranch,
81
+ (_currentBranch: string | null, optimisticBranch: string | null) => optimisticBranch,
82
+ );
83
const branchNames = branches.map((branch) => branch.name);
84
const branchByName = new Map(branches.map((branch) => [branch.name, branch]));
85
const trimmedBranchQuery = branchQuery.trim();
@@ -168,6 +172,7 @@ export default function BranchToolbar({
172
const selectedBranchName = branch.isRemote
173
? deriveLocalBranchNameFromRemoteRef(branch.name)
174
: branch.name;
175
+ setOptimisticBranch(selectedBranchName);
176
177
checkoutMutation.mutate(branch.name, {
178
onSuccess: async () => {
@@ -179,11 +184,13 @@ export default function BranchToolbar({
184
nextBranchName = status.branch;
185
}
186
}
187
+ setOptimisticBranch(nextBranchName);
188
setThreadBranch(nextBranchName, activeWorktreePath);
189
setIsBranchMenuOpen(false);
190
onComposerFocusRequest?.();
191
},
192
onError: (error) => {
193
+ setOptimisticBranch(canonicalActiveBranch);
194
setThreadError(error instanceof Error ? error.message : "Failed to checkout branch.");
195
setIsBranchMenuOpen(true);
196
},
@@ -194,8 +201,10 @@ export default function BranchToolbar({
201
const name = rawName.trim();
202
const api = readNativeApi();
203
if (!api || !activeThreadId || !branchCwd || !name || createBranchMutation.isPending) return;
204
+ setOptimisticBranch(name);
205
createBranchMutation.mutate(name, {
206
onSuccess: () => {
207
+ setOptimisticBranch(name);
208
setThreadError(null);
209
setThreadBranch(name, activeWorktreePath);
210
setBranchQuery("");
@@ -203,6 +212,7 @@ export default function BranchToolbar({
212
onComposerFocusRequest?.();
213
},
214
onError: (error) => {
215
+ setOptimisticBranch(canonicalActiveBranch);
216
setThreadError(error instanceof Error ? error.message : "Failed to create branch.");
217
},
218
});
@@ -223,9 +233,7 @@ export default function BranchToolbar({
233
variant="ghost"
234
className="text-muted-foreground/70 hover:text-foreground/80"
235
size="xs"
226
- onClick={() =>
227
- onEnvModeChange(effectiveEnvMode === "local" ? "worktree" : "local")
228
- }
236
+ onClick={() => onEnvModeChange(effectiveEnvMode === "local" ? "worktree" : "local")}
237
>
238
{effectiveEnvMode === "worktree" ? "New worktree" : "Local"}
239
</Button>
@@ -313,7 +321,9 @@ export default function BranchToolbar({
321
>
322
<div className="flex w-full items-center justify-between gap-2">
323
<span className="truncate">{itemValue}</span>
316
- {badge && <span className="shrink-0 text-[10px] text-muted-foreground/45">{badge}</span>}
324
+ {badge && (
325
+ <span className="shrink-0 text-[10px] text-muted-foreground/45">{badge}</span>
326
+ )}
327
</div>
328
</ComboboxItem>
329
);