Skip to content

Commit 0e3c586

Browse files
committed
fix(web): use composer scope for side-chat citation activation
1 parent 82581b2 commit 0e3c586

4 files changed

Lines changed: 36 additions & 4 deletions

File tree

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
12
import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts";
23
import {
34
serializeAssistantCitation,
45
withAssistantCitationComment,
56
} from "@t3tools/shared/assistantCitations";
6-
import { createRef, useState } from "react";
7+
import { createRef, use, useState } from "react";
78
import { expect, it, vi } from "vite-plus/test";
89

910
import { page, userEvent } from "vite-plus/test/browser";
@@ -18,6 +19,7 @@ vi.mock(import("@tanstack/react-router"), async (importOriginal) => ({
1819
useParams: (() => navigation.params) as typeof import("@tanstack/react-router").useParams,
1920
}));
2021

22+
import { ComposerContextActionsContext } from "./composerContextActions";
2123
import { findAssistantCitationSourceAnchor } from "~/lib/assistantTextSelection";
2224
import { act, render } from "../../test/render";
2325
import {
@@ -296,3 +298,27 @@ it.each(["blocked", "refusedLater"] as const)(
296298
.toHaveAttribute("data-prompt", `See ${serializeAssistantCitation(citation)}x `);
297299
},
298300
);
301+
302+
it("reveals a side-chat citation using the composer scope on its parent route", async () => {
303+
navigation.navigate.mockClear();
304+
navigation.params = { environmentId: "environment", threadId: "parent-thread" };
305+
const editorRef = createRef<ComposerPromptEditorHandle>();
306+
function SideChatComposer() {
307+
const actions = use(ComposerContextActionsContext);
308+
return (
309+
<ComposerContextActionsContext
310+
value={{ ...actions, threadRef: scopeThreadRef(citation.environmentId, citation.threadId) }}
311+
>
312+
<Composer question={false} editorRef={editorRef} sourceVisible={false} />
313+
</ComposerContextActionsContext>
314+
);
315+
}
316+
try {
317+
await using screen = await render(<SideChatComposer />);
318+
await screen.getByRole("link", { name: "View cited assistant text: hello" }).click();
319+
await expect.element(page.getByLabelText("Comment on selected text")).toBeVisible();
320+
expect(navigation.navigate).toHaveBeenCalledOnce();
321+
} finally {
322+
navigation.params = { environmentId: "environment", threadId: "thread" };
323+
}
324+
});

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { Link, useNavigate, useParams } from "@tanstack/react-router";
88
import { QuoteIcon } from "lucide-react";
99
import {
10+
use,
1011
useEffect,
1112
useEffectEvent,
1213
useRef,
@@ -30,10 +31,12 @@ import { resolveAssistantCitationCommentDismissal } from "./assistantCitationCom
3031
import { observeAssistantCitationCommentSource } from "./AssistantCitationSource";
3132
import { composerFloatingLayerProps } from "./composerEventScope";
3233
import { textDirection } from "~/textDirection";
34+
import { ComposerContextActionsContext } from "../composerContextActions";
3335

3436
function useCitationSourceLinkProps(citation: AssistantCitation, editComment = false) {
3537
const navigate = useNavigate();
36-
const { environmentId, threadId } = useParams({ strict: false });
38+
const route = useParams({ strict: false });
39+
const composerScope = use(ComposerContextActionsContext).threadRef ?? route;
3740
const sourceThread = useQuietThreadShell(
3841
scopeThreadRef(citation.environmentId, citation.threadId),
3942
);
@@ -48,7 +51,8 @@ function useCitationSourceLinkProps(citation: AssistantCitation, editComment = f
4851
// Keep the draft editor mounted when editing a quote from another scope.
4952
if (
5053
editComment &&
51-
(citation.environmentId !== environmentId || citation.threadId !== threadId)
54+
(citation.environmentId !== composerScope.environmentId ||
55+
citation.threadId !== composerScope.threadId)
5256
)
5357
return;
5458
void navigate(assistantCitationNavigation(citation, sourceThread));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
700700
const previewFile = composerFiles.find((file) => file.id === previewFileId);
701701
const composerContextActions = {
702702
environmentId,
703+
threadRef: routeThreadRef,
703704
cwd: gitCwd ?? undefined,
704705
projectId: activeProjectId ?? undefined,
705706
expandImage: (imageId: string) => {

‎apps/web/src/components/composerContextActions.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import type { EnvironmentId, ProjectId } from "@t3tools/contracts";
1+
import type { EnvironmentId, ProjectId, ScopedThreadRef } from "@t3tools/contracts";
22
import { createContext, type MouseEvent } from "react";
33

44
export interface ComposerContextActions {
55
environmentId: EnvironmentId | null;
6+
threadRef?: ScopedThreadRef | undefined;
67
cwd?: string | undefined;
78
projectId?: ProjectId | undefined;
89
expandImage: (imageId: string) => void;

0 commit comments

Comments
 (0)