Skip to content

Commit e279264

Browse files
authored
fix(ui): send prompt on first click when switching branch (#3277)
1 parent aee1dac commit e279264

3 files changed

Lines changed: 41 additions & 12 deletions

File tree

packages/ui/src/features/task-detail/BranchMismatchDialog.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,14 @@ export function BranchMismatchDialog({
116116
Continue anyway
117117
</Button>
118118

119-
<AlertDialog.Action>
120-
<Button
121-
variant="solid"
122-
size="1"
123-
onClick={onSwitch}
124-
loading={isSwitching}
125-
>
126-
Switch branch
127-
</Button>
128-
</AlertDialog.Action>
119+
<Button
120+
variant="solid"
121+
size="1"
122+
onClick={onSwitch}
123+
loading={isSwitching}
124+
>
125+
Switch branch
126+
</Button>
129127
</Flex>
130128
</AlertDialog.Content>
131129
</AlertDialog.Root>

packages/ui/src/features/workspace/useBranchMismatchDialog.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ let capturedMutationOptions: {
4444
onError?: (e: Error) => void;
4545
} = {};
4646
const mockMutate = vi.fn();
47+
let mockIsPending = false;
4748

4849
vi.mock("@tanstack/react-query", () => ({
4950
useMutation: (opts: Record<string, unknown>) => {
5051
capturedMutationOptions = opts as typeof capturedMutationOptions;
51-
return { mutate: mockMutate, isPending: false };
52+
return { mutate: mockMutate, isPending: mockIsPending };
5253
},
5354
}));
5455

@@ -89,6 +90,7 @@ describe("useBranchMismatchDialog", () => {
8990
vi.clearAllMocks();
9091
capturedMutationOptions = {};
9192
mockShouldWarn = false;
93+
mockIsPending = false;
9294
});
9395

9496
describe("handleBeforeSubmit", () => {
@@ -184,6 +186,34 @@ describe("useBranchMismatchDialog", () => {
184186
},
185187
);
186188
});
189+
190+
it("does nothing while a switch is in flight, so a dismiss can't drop the pending message", () => {
191+
mockIsPending = true;
192+
const { result, onSendPrompt } = renderDialog({ shouldWarn: true });
193+
const clearEditor = vi.fn();
194+
195+
act(() => {
196+
result.current.handleBeforeSubmit("hello", clearEditor);
197+
});
198+
199+
act(() => {
200+
result.current.dialogProps?.onSwitch();
201+
});
202+
203+
mockTrack.mockClear();
204+
act(() => {
205+
result.current.dialogProps?.onCancel();
206+
});
207+
208+
expect(mockTrack).not.toHaveBeenCalled();
209+
expect(result.current.dialogProps?.open).toBe(true);
210+
211+
act(() => {
212+
capturedMutationOptions.onSuccess?.();
213+
});
214+
215+
expect(onSendPrompt).toHaveBeenCalledWith("hello");
216+
});
187217
});
188218

189219
describe("handleSwitch", () => {

packages/ui/src/features/workspace/useBranchMismatchDialog.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,13 @@ export function useBranchMismatchDialog({
117117
}, [dismissWarning, emitAction]);
118118

119119
const handleCancel = useCallback(() => {
120+
if (isSwitching) return;
120121
emitAction("cancel");
121122
setPendingMessage(null);
122123
pendingMessageRef.current = null;
123124
pendingClearRef.current = null;
124125
setSwitchError(null);
125-
}, [emitAction]);
126+
}, [emitAction, isSwitching]);
126127

127128
const dialogProps =
128129
linkedBranch && currentBranch

0 commit comments

Comments
 (0)