From a6266dec064b6cb90f9a9fa2ea3d25a67805d264 Mon Sep 17 00:00:00 2001 From: imqdee Date: Thu, 22 Oct 2026 11:09:45 +0200 Subject: [PATCH 1/3] feat(web): clicking a draft quote chip shows the quote with its comment ready to edit The draft chip in the composer now opens the same popover as a sent quote chip: the quoted text, the comment, and Go to source. The comment is a focused textarea with the caret at its end. The chip itself looks the same. Right after a selection, the comment box still opens under the highlighted text on its own. --- .../components/ComposerPromptEditorTiptap.tsx | 1 - .../AssistantCitationChip.browser.test.tsx | 103 +++++- .../components/chat/AssistantCitationChip.tsx | 304 +++++++++++------- .../chat/AssistantCitationCommentEditor.tsx | 61 ++-- 4 files changed, 302 insertions(+), 167 deletions(-) diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.tsx index af5a200e264af..a2844297abae8 100644 --- a/apps/web/src/components/ComposerPromptEditorTiptap.tsx +++ b/apps/web/src/components/ComposerPromptEditorTiptap.tsx @@ -462,7 +462,6 @@ function ComposerCitationNodeView({ node, editor, getPos }: NodeViewProps) { > ({ vi.mock(import("@tanstack/react-router"), async (importOriginal) => ({ ...(await importOriginal()), useNavigate: () => vi.fn(), - Link: (({ children }: { children: ReactNode }) => {children}) as never, + Link: (({ children }: { children: ReactNode }) => {children}) as never, })); import { AssistantCitationChip } from "./AssistantCitationChip"; @@ -50,14 +50,23 @@ function setup() { return { sourceAnchor, [Symbol.dispose]: () => source.remove() }; } -function mount(sourceAnchor: AssistantCitationSourceAnchor, onSave: () => boolean) { +function mount( + sourceAnchor: AssistantCitationSourceAnchor, + onSave: () => boolean, + onCancel?: () => void, +) { function Composer() { const [open, setOpen] = useState(true); return ( ); } @@ -83,12 +92,10 @@ async function removeSource() { await act(async () => onUnavailable()); } -async function clickButton(label: string) { - const button = [...document.querySelectorAll("button")].find( - (element) => element.textContent?.trim() === label, - ); - expect(button, `button ${label}`).toBeDefined(); - await act(async () => button!.click()); +// Once the source is gone the popup moves to the chip, which saves with Enter. +async function pressEnter() { + textarea()!.focus(); + await userEvent.keyboard("{Enter}"); } describe("citation comment source disappearance", () => { @@ -107,7 +114,7 @@ describe("citation comment source disappearance", () => { expect(mocks.observeSource).toHaveBeenCalledTimes(1); await typeComment("shortened comment"); - await clickButton("Save"); + await pressEnter(); expect(onSave).toHaveBeenCalledWith("shortened comment"); expect(textarea()).toBeNull(); }); @@ -122,10 +129,26 @@ describe("citation comment source disappearance", () => { expect(onSave).toHaveBeenCalledWith("keep this draft"); expect(textarea()?.value).toBe("keep this draft"); onSave.mockReturnValue(true); - await clickButton("Save"); + await pressEnter(); expect(onSave).toHaveBeenLastCalledWith("keep this draft"); expect(textarea()).toBeNull(); }); + + it("removes a just-inserted quote on Escape from Go to source", async () => { + using source = setup(); + const onCancel = vi.fn(); + await using _screen = await mount( + source.sourceAnchor, + vi.fn(() => true), + onCancel, + ); + await typeComment("x".repeat(ASSISTANT_CITATION_MAX_COMMENT_LENGTH + 1)); + await removeSource(); + + page.getByRole("link", { name: "Go to source" }).element().focus(); + await userEvent.keyboard("{Escape}"); + expect(onCancel).toHaveBeenCalledTimes(1); + }); }); describe("citation comment popup position", () => { @@ -175,3 +198,57 @@ describe("citation comment popup position", () => { expect(rect.right).toBeLessThanOrEqual(window.innerWidth); }); }); + +describe("draft citation chip", () => { + it("opens the quote with an editable comment, ready to type at its end", async () => { + using _source = setup(); + const onSave = vi.fn(() => true); + function Composer() { + const [open, setOpen] = useState(false); + return ( + + ); + } + await using _screen = await render(); + + await page.getByRole("button", { name: "Edit citation comment" }).click(); + + await expect.element(page.getByRole("blockquote")).toHaveTextContent("hello"); + await expect.element(page.getByText("Go to source")).toBeVisible(); + await vi.waitFor(() => expect(document.activeElement).toBe(textarea())); + expect(textarea()?.selectionStart).toBe("note".length); + expect(page.getByRole("button", { name: "Save" }).query()).toBeNull(); + + await userEvent.keyboard(" again{Enter}"); + expect(onSave).toHaveBeenCalledWith("note again"); + expect(textarea()).toBeNull(); + }); + + it("keeps the comment field and its caret in view under a long quote and comment", async () => { + using _source = setup(); + function Composer() { + const [open, setOpen] = useState(false); + return ( + true }} + /> + ); + } + await using _screen = await render(); + + await page.getByRole("button", { name: "Edit citation comment" }).click(); + + await vi.waitFor(() => expect(document.activeElement).toBe(textarea())); + await expect.element(page.getByLabelText("Comment on selected text")).toBeInViewport(); + const input = textarea()!; + expect(input.scrollTop + input.clientHeight).toBeGreaterThanOrEqual(input.scrollHeight - 1); + }); +}); diff --git a/apps/web/src/components/chat/AssistantCitationChip.tsx b/apps/web/src/components/chat/AssistantCitationChip.tsx index 1e97f00319c2e..a305aaf92d98a 100644 --- a/apps/web/src/components/chat/AssistantCitationChip.tsx +++ b/apps/web/src/components/chat/AssistantCitationChip.tsx @@ -11,6 +11,7 @@ import { useRef, useState, type MouseEvent as ReactMouseEvent, + type ReactNode, } from "react"; import { findAssistantCitationSourceAnchor, @@ -34,11 +35,10 @@ import { observeResize } from "~/lib/observeResize"; export function AssistantCitationChip({ citation, - composer = false, commentEditor, }: { citation: AssistantCitation; - composer?: boolean; + /** Present in the composer draft, where the comment can still change. */ commentEditor?: { open: boolean; sourceAnchor?: AssistantCitationSourceAnchor | undefined; @@ -50,7 +50,6 @@ export function AssistantCitationChip({ onRestoreFocus?: () => void; }; }) { - const navigate = useNavigate(); const commentInputRef = useRef(null); const commentPopupRef = useRef(null); const draftCommentRef = useRef(null); @@ -107,31 +106,7 @@ export function AssistantCitationChip({ } : undefined; const label = assistantCitationLabel(citation); - const sourceLinkProps = { - to: "/$environmentId/$threadId" as const, - params: { environmentId: citation.environmentId, threadId: citation.threadId }, - hash: assistantCitationHash(citation), - "data-markdown-copy": serializeAssistantCitation(citation), - resetScroll: false, - onClick: (event: ReactMouseEvent) => { - if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) { - return; - } - event.preventDefault(); - void navigate(assistantCitationNavigation(citation)); - }, - }; - const composerSourceLink = ( - -