Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion apps/web/src/components/BranchToolbarBranchSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ interface BranchToolbarBranchSelectorProps {

const WORKTREE_CHANGE_BLOCKED_MESSAGE = msg("Stop the current turn to switch worktrees.");

// Picker checkouts by environment and checkout: null while in flight, then the branch it landed
// on. Shared because a draft thread mounts two selectors on one checkout, and the switch's own
// settle reloads refs for both.
const pickerCheckouts = new Map<string, string | null>();
const pickerCheckoutKey = (environmentId: string, cwd: string) => `${environmentId}\u0000${cwd}`;

export function BranchToolbarBranchSelector({
forceNewWorktree = false,
ref,
Expand Down Expand Up @@ -505,6 +511,8 @@ export function BranchToolbarBranchSelector({
runBranchAction(async () => {
const previousBranch = resolvedActiveBranch;
setOptimisticBranch(selectedBranchName);
const checkoutKey = pickerCheckoutKey(environmentId, selectionTarget.checkoutCwd);
pickerCheckouts.set(checkoutKey, null);
const checkoutResult = await switchRef({
environmentId,
input: {
Expand All @@ -516,6 +524,7 @@ export function BranchToolbarBranchSelector({
const nextBranchName = refName.isRemote
? (checkoutResult.value.refName ?? selectedBranchName)
: selectedBranchName;
pickerCheckouts.set(checkoutKey, nextBranchName);
const movesCheckout = selectionTarget.checkoutCwd !== branchCwd;
let hold: BranchHold | null = null;
const applied = setThreadBranch(
Expand Down Expand Up @@ -549,6 +558,7 @@ export function BranchToolbarBranchSelector({
setOptimisticBranch(applied ? nextBranchName : previousBranch);
return;
}
pickerCheckouts.delete(checkoutKey);
setOptimisticBranch(previousBranch);
if (!isAtomCommandInterrupted(checkoutResult)) {
toastManager.add(
Expand All @@ -574,6 +584,8 @@ export function BranchToolbarBranchSelector({
runBranchAction(async () => {
const previousBranch = resolvedActiveBranch;
setOptimisticBranch(name);
const checkoutKey = pickerCheckoutKey(environmentId, branchCwd);
pickerCheckouts.set(checkoutKey, null);
const createBranchResult = await createRefMutation({
environmentId,
input: {
Expand All @@ -584,6 +596,7 @@ export function BranchToolbarBranchSelector({
});
if (createBranchResult._tag === "Success") {
const nextBranchName = createBranchResult.value.refName;
pickerCheckouts.set(checkoutKey, nextBranchName);
const applied = setThreadBranch(nextBranchName, activeWorktreePath);
if (applied) {
setBranchHold({
Expand All @@ -595,6 +608,7 @@ export function BranchToolbarBranchSelector({
setOptimisticBranch(applied ? nextBranchName : previousBranch);
return;
}
pickerCheckouts.delete(checkoutKey);
setOptimisticBranch(previousBranch);
if (!isAtomCommandInterrupted(createBranchResult)) {
toastManager.add(
Expand Down Expand Up @@ -627,7 +641,15 @@ export function BranchToolbarBranchSelector({
// A checkout made outside T3 Code, such as a clone finishing, leaves refs listed before it.
const statusBranch = branchStatusQuery.data?.refName ?? null;
const refreshRefsMissingStatusBranch = useEffectEvent((branch: string | null) => {
if (isInitialBranchesLoadPending || branch === null) return;
if (branch === null || branchCwd === null) return;
// Kept until the checkout reports another branch, so every selector on it skips the refresh.
const checkoutKey = pickerCheckoutKey(environmentId, branchCwd);
if (pickerCheckouts.has(checkoutKey)) {
const pickerBranch = pickerCheckouts.get(checkoutKey);
if (pickerBranch === null || pickerBranch === branch) return;
pickerCheckouts.delete(checkoutKey);
}
if (isInitialBranchesLoadPending) return;
if (branchByName.get(branch)?.current !== true) refreshStaleRefs();
});
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- reloads refs from the server once per branch live status reports that they do not list as checked out
Expand Down
Loading