Skip to content

Commit e86fdae

Browse files
committed
fix(web): Go to source saves the draft first and stays when the save fails
1 parent 58fc468 commit e86fdae

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

‎apps/web/src/components/chat/AssistantCitationChip.browser.test.tsx‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,27 @@ describe("draft citation chip", () => {
281281
await page.getByRole("link", { name: "Go to source" }).click();
282282
expect(mocks.navigate).toHaveBeenCalledTimes(1);
283283
});
284+
285+
it("stays on the page when saving the draft fails", async () => {
286+
using _source = setup();
287+
const onSave = vi.fn(() => false);
288+
function Composer() {
289+
const [open, setOpen] = useState(false);
290+
return (
291+
<AssistantCitationChip
292+
citation={citation}
293+
commentEditor={{ open, onOpenChange: setOpen, onSave }}
294+
/>
295+
);
296+
}
297+
await using _screen = await render(<Composer />);
298+
await page.getByRole("button", { name: /^Add comment to citation:/ }).click();
299+
await vi.waitFor(() => expect(textarea()).not.toBeNull());
300+
301+
await typeComment("changed");
302+
await page.getByRole("link", { name: "Go to source" }).click();
303+
expect(onSave).toHaveBeenCalledWith("changed");
304+
expect(mocks.navigate).not.toHaveBeenCalled();
305+
expect(textarea()?.value).toBe("changed");
306+
});
284307
});

‎apps/web/src/components/chat/AssistantCitationChip.tsx‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,13 @@ export function AssistantCitationChip({
233233
<AssistantCitationDetails
234234
citation={citation}
235235
editor={editor}
236-
// An over-length draft keeps the popup open, so the page must stay too.
237-
canLeave={() =>
238-
resolveAssistantCitationCommentDismissal({
239-
reason: "close-press",
240-
draft: draftCommentRef.current,
241-
savedComment: citation.comment,
242-
}).kind !== "keep-open"
243-
}
236+
// Save before leaving. A draft that cannot be saved keeps the popup open, so the
237+
// page must stay too.
238+
canLeave={() => {
239+
const settled = settleDraftOnClose("close-press");
240+
if (settled) draftCommentRef.current = null;
241+
return settled;
242+
}}
244243
showQuote={!activeSourceAnchor}
245244
/>
246245
</PopoverPopup>

0 commit comments

Comments
 (0)