Skip to content

Commit 58fc468

Browse files
committed
fix(web): draft quote chip names its quote and stays put with an over-length comment
1 parent ee36e89 commit 58fc468

3 files changed

Lines changed: 52 additions & 9 deletions

File tree

‎apps/web/src/components/ComposerPromptEditorTiptap.citationDraft.browser.test.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,7 @@ it("keeps an unsaved citation comment when a question takes the composer", async
9292
expect(drafts()?.prompt).toBe(
9393
`See ${serializeAssistantCitation(withAssistantCitationComment(citation, "check this"))} `,
9494
);
95-
await expect.element(page.getByLabelText("Edit citation comment")).toBeInTheDocument();
95+
await expect
96+
.element(page.getByRole("button", { name: "Edit citation comment: check this" }))
97+
.toBeInTheDocument();
9698
});

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

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@ import {
44
MessageId,
55
ThreadId,
66
} from "@t3tools/contracts";
7-
import { useState, type ReactNode } from "react";
7+
import { useState, type MouseEventHandler, type ReactNode } from "react";
88
import { describe, expect, it, vi } from "vite-plus/test";
99
import { page, userEvent } from "vite-plus/test/browser";
1010

1111
import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelection";
1212
import "~/index.css";
1313
import { act, render } from "../../../test/render";
1414

15-
const mocks = vi.hoisted(() => ({ observeSource: vi.fn(), dispose: vi.fn() }));
15+
const mocks = vi.hoisted(() => ({ observeSource: vi.fn(), dispose: vi.fn(), navigate: vi.fn() }));
1616
vi.mock(import("./AssistantCitationSource"), async (importOriginal) => ({
1717
...(await importOriginal()),
1818
observeAssistantCitationCommentSource: mocks.observeSource,
1919
}));
2020
vi.mock(import("@tanstack/react-router"), async (importOriginal) => ({
2121
...(await importOriginal()),
22-
useNavigate: () => vi.fn(),
23-
Link: (({ children }: { children: ReactNode }) => <a href="#source">{children}</a>) as never,
22+
useNavigate: () => mocks.navigate,
23+
Link: (({ children, onClick }: { children: ReactNode; onClick?: MouseEventHandler }) => (
24+
<a href="#source" onClick={onClick}>
25+
{children}
26+
</a>
27+
)) as never,
2428
}));
2529

2630
import { AssistantCitationChip } from "./AssistantCitationChip";
@@ -41,6 +45,7 @@ const citation = {
4145
function setup() {
4246
mocks.observeSource.mockReset().mockReturnValue(mocks.dispose);
4347
mocks.dispose.mockClear();
48+
mocks.navigate.mockClear();
4449
const source = document.createElement("p");
4550
source.textContent = "hello";
4651
document.body.append(source);
@@ -214,7 +219,7 @@ describe("draft citation chip", () => {
214219
}
215220
await using _screen = await render(<Composer />);
216221

217-
await page.getByRole("button", { name: "Edit citation comment" }).click();
222+
await page.getByRole("button", { name: /^Edit citation comment:/ }).click();
218223

219224
await expect.element(page.getByRole("blockquote")).toHaveTextContent("hello");
220225
await expect.element(page.getByText("Go to source")).toBeVisible();
@@ -244,11 +249,36 @@ describe("draft citation chip", () => {
244249
}
245250
await using _screen = await render(<Composer />);
246251

247-
await page.getByRole("button", { name: "Edit citation comment" }).click();
252+
await page.getByRole("button", { name: /^Edit citation comment:/ }).click();
248253

249254
await vi.waitFor(() => expect(document.activeElement).toBe(textarea()));
250255
await expect.element(page.getByLabelText("Comment on selected text")).toBeInViewport();
251256
const input = textarea()!;
252257
expect(input.scrollTop + input.clientHeight).toBeGreaterThanOrEqual(input.scrollHeight - 1);
253258
});
259+
260+
it("stays on the page while the draft is too long to save", async () => {
261+
using _source = setup();
262+
function Composer() {
263+
const [open, setOpen] = useState(false);
264+
return (
265+
<AssistantCitationChip
266+
citation={citation}
267+
commentEditor={{ open, onOpenChange: setOpen, onSave: () => true }}
268+
/>
269+
);
270+
}
271+
await using _screen = await render(<Composer />);
272+
await page.getByRole("button", { name: /^Add comment to citation:/ }).click();
273+
await vi.waitFor(() => expect(textarea()).not.toBeNull());
274+
275+
await typeComment("x".repeat(ASSISTANT_CITATION_MAX_COMMENT_LENGTH + 1));
276+
await page.getByRole("link", { name: "Go to source" }).click();
277+
expect(mocks.navigate).not.toHaveBeenCalled();
278+
expect(textarea()).not.toBeNull();
279+
280+
await typeComment("short");
281+
await page.getByRole("link", { name: "Go to source" }).click();
282+
expect(mocks.navigate).toHaveBeenCalledTimes(1);
283+
});
254284
});

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export function AssistantCitationChip({
173173
data-assistant-citation-chip="true"
174174
data-markdown-copy={serializeAssistantCitation(citation)}
175175
data-citation-comment-trigger="true"
176-
aria-label={citation.comment ? "Edit citation comment" : "Add comment to citation"}
176+
aria-label={`${citation.comment ? "Edit citation comment" : "Add comment to citation"}: ${label}`}
177177
/>
178178
}
179179
>
@@ -213,7 +213,7 @@ export function AssistantCitationChip({
213213
: undefined
214214
}
215215
ref={commentPopupRef}
216-
aria-label="Edit citation comment"
216+
aria-label={`Edit citation comment: ${label}`}
217217
width={activeSourceAnchor ? "md" : "lg"}
218218
padding="compact"
219219
onPointerDown={(event) => event.stopPropagation()}
@@ -233,6 +233,14 @@ 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+
}
236244
showQuote={!activeSourceAnchor}
237245
/>
238246
</PopoverPopup>
@@ -245,10 +253,12 @@ function AssistantCitationDetails({
245253
citation,
246254
editor,
247255
showQuote = true,
256+
canLeave,
248257
}: {
249258
citation: AssistantCitation;
250259
editor?: ReactNode;
251260
showQuote?: boolean;
261+
canLeave?: () => boolean;
252262
}) {
253263
const navigate = useNavigate();
254264
return (
@@ -285,6 +295,7 @@ function AssistantCitationDetails({
285295
return;
286296
}
287297
event.preventDefault();
298+
if (canLeave && !canLeave()) return;
288299
void navigate(assistantCitationNavigation(citation));
289300
}}
290301
/>

0 commit comments

Comments
 (0)