Skip to content

Commit 4c9706c

Browse files
committed
fix(web): edit cross-thread citation comments without leaving the draft
Co-Authored-By: GPT-6.1 Sol via T3 Code
1 parent 0ae431e commit 4c9706c

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import { expect, it, vi } from "vite-plus/test";
88

99
import { page, userEvent } from "vite-plus/test/browser";
1010

11+
const navigation = vi.hoisted(() => ({
12+
navigate: vi.fn(),
13+
params: { environmentId: "environment", threadId: "thread" },
14+
}));
15+
vi.mock(import("@tanstack/react-router"), async (importOriginal) => ({
16+
...(await importOriginal()),
17+
useNavigate: () => navigation.navigate,
18+
useParams: (() => navigation.params) as typeof import("@tanstack/react-router").useParams,
19+
}));
20+
1121
import { findAssistantCitationSourceAnchor } from "~/lib/assistantTextSelection";
1222
import { act, render } from "../../test/render";
1323
import {
@@ -179,3 +189,35 @@ it("reanchors the comment when the cited source mounts after activation", async
179189
expect(highlight && [...highlight][0]?.startContainer.parentElement).toBe(source);
180190
});
181191
});
192+
193+
it.each([
194+
["environment", "other-thread", "click"],
195+
["environment", "other-thread", "Enter"],
196+
["environment", "other-thread", " "],
197+
["other-environment", "thread", "click"],
198+
["other-environment", "thread", "Enter"],
199+
["other-environment", "thread", " "],
200+
])(
201+
"edits a citation from draft %s/%s with %s without navigating",
202+
async (environmentId, threadId, activation) => {
203+
navigation.navigate.mockClear();
204+
navigation.params = { environmentId, threadId };
205+
try {
206+
const editorRef = createRef<ComposerPromptEditorHandle>();
207+
await using screen = await render(
208+
<Composer question={false} editorRef={editorRef} sourceVisible={false} />,
209+
);
210+
const chip = screen.getByRole("link", { name: "View cited assistant text: hello" });
211+
if (activation === "click") {
212+
await chip.click();
213+
} else {
214+
chip.element().focus();
215+
await userEvent.keyboard(activation === "Enter" ? "{Enter}" : " ");
216+
}
217+
await expect.element(page.getByLabelText("Comment on selected text")).toBeVisible();
218+
expect(navigation.navigate).not.toHaveBeenCalled();
219+
} finally {
220+
navigation.params = { environmentId: "environment", threadId: "thread" };
221+
}
222+
},
223+
);

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
assistantCitationLabel,
44
serializeAssistantCitation,
55
} from "@t3tools/shared/assistantCitations";
6-
import { Link, useNavigate } from "@tanstack/react-router";
6+
import { Link, useNavigate, useParams } from "@tanstack/react-router";
77
import { ArrowUpRightIcon, QuoteIcon } from "lucide-react";
88
import {
99
useEffect,
@@ -52,6 +52,7 @@ export function AssistantCitationChip({
5252
};
5353
}) {
5454
const navigate = useNavigate();
55+
const { environmentId, threadId } = useParams({ strict: false });
5556
const chipRef = useRef<HTMLSpanElement>(null);
5657
const commentInputRef = useRef<HTMLTextAreaElement>(null);
5758
const commentPopupRef = useRef<HTMLDivElement>(null);
@@ -143,6 +144,13 @@ export function AssistantCitationChip({
143144
return;
144145
}
145146
event.preventDefault();
147+
// Leaving the draft's route unmounts its comment editor.
148+
if (
149+
composer &&
150+
commentEditor &&
151+
(citation.environmentId !== environmentId || citation.threadId !== threadId)
152+
)
153+
return;
146154
void navigate(assistantCitationNavigation(citation));
147155
},
148156
};

0 commit comments

Comments
 (0)