@devries-cameron20 / t3code-1 / commits / 46015ec958
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): Go to source on a side chat quote keeps you on its parent thread (#33160)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 10, 2026 at 05:06 UTC 46015ec958f9fe5a71856a564950aec3493b1945
5 files changed +238 -22
apps/web/src/components/ChatView.tsx
+14 -7
@@ -117,7 +117,10 @@ import { type SetStateAction, use, useEffect, useLayoutEffect, useRef, useState
117 import { BackgroundThreadContext } from "./chat/backgroundThread";
118 import { WorkspaceMutationIdContext } from "./chat/ChatMarkdownMedia";
119 import { useLocation, useNavigate } from "@tanstack/react-router";
120 -import { assistantCitationFromLocation } from "../lib/assistantCitationNavigation";
120 +import {
121 + assistantCitationFromLocation,
122 + useSideChatCitationReveal,
123 +} from "../lib/assistantCitationNavigation";
124 import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelection";
125 import { useStore } from "zustand";
126 import { shallow } from "zustand/vanilla/shallow";
@@ -568,12 +571,16 @@ export default function ChatView(props: ChatViewProps) {
571 select: (location) => location.state.assistantCitationActivation ?? location.state.__TSR_key,
572 });
573 const citationLocation = { href: citationHref, key: citationKey };
571 - const citationRequest = (() => {
572 - const citation = assistantCitationFromLocation(citationLocation.href);
573 - return citation && citation.environmentId === environmentId && citation.threadId === threadId
574 - ? { citation, key: citationLocation.key ?? citationLocation.href }
575 - : null;
576 - })() as AssistantCitationRequest | null;
574 + const locationCitation = assistantCitationFromLocation(citationLocation.href);
575 + const citationActivation = citationLocation.key ?? citationLocation.href;
576 + const citationRequest = (
577 + locationCitation &&
578 + locationCitation.environmentId === environmentId &&
579 + locationCitation.threadId === threadId
580 + ? { citation: locationCitation, key: citationActivation }
581 + : null
582 + ) as AssistantCitationRequest | null;
583 + useSideChatCitationReveal(embedded ? null : routeThreadRef, locationCitation, citationActivation);
584 const citationHistory = useCitedHistory(citationRequest);
585 const { resolvedTheme } = useTheme();
586 const composerRuntimeMode = useComposerDraftStore(
apps/web/src/components/chat/AssistantCitationChip.tsx
+8 -6
@@ -1,3 +1,4 @@
1 +import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2 import type { AssistantCitation } from "@t3tools/contracts";
3 import {
4 assistantCitationLabel,
@@ -17,10 +18,11 @@ import {
18 type AssistantCitationSourceAnchor,
19 } from "~/lib/assistantTextSelection";
20 import {
20 - assistantCitationHash,
21 assistantCitationNavigation,
22 + assistantCitationRoute,
23 } from "../../lib/assistantCitationNavigation";
24 import { cn } from "~/lib/utils";
25 +import { useQuietThreadShell } from "~/state/entities";
26 import { ContextChip, ContextChipAction, ContextChipLabel } from "../ContextChip";
27 import { ContextChipPopover } from "../contextChipParts";
28 import { Button } from "../ui/button";
@@ -52,6 +54,9 @@ export function AssistantCitationChip({
54 };
55 }) {
56 const navigate = useNavigate();
57 + const sourceThread = useQuietThreadShell(
58 + scopeThreadRef(citation.environmentId, citation.threadId),
59 + );
60 const commentInputRef = useRef<HTMLTextAreaElement>(null);
61 const commentPopupRef = useRef<HTMLDivElement>(null);
62 const draftCommentRef = useRef<string | null>(null);
@@ -112,17 +117,14 @@ export function AssistantCitationChip({
117 : undefined;
118 const label = assistantCitationLabel(citation);
119 const sourceLinkProps = {
115 - to: "/$environmentId/$threadId" as const,
116 - params: { environmentId: citation.environmentId, threadId: citation.threadId },
117 - hash: assistantCitationHash(citation),
120 + ...assistantCitationRoute(citation, sourceThread),
121 "data-markdown-copy": serializeAssistantCitation(citation),
119 - resetScroll: false,
122 onClick: (event: ReactMouseEvent<HTMLAnchorElement>) => {
123 if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
124 return;
125 }
126 event.preventDefault();
125 - void navigate(assistantCitationNavigation(citation));
127 + void navigate(assistantCitationNavigation(citation, sourceThread));
128 },
129 };
130 const composerSourceLink = (
apps/web/src/components/sidebar/mainAppLocation.browser.test.tsx
+1 -1
@@ -58,7 +58,7 @@ describe("main app return location", () => {
58 using _tracking = { [Symbol.dispose]: startMainAppLocationTracking(router) };
59
60 const returnLocation = async (from: string, control: string) => {
61 - await settled(router, router.navigate(assistantCitationNavigation(citation)));
61 + await settled(router, router.navigate(assistantCitationNavigation(citation, null)));
62 expect(assistantCitationFromLocation(router.state.location.href)).toEqual(citation);
63 await settled(router, router.navigate({ href: from }));
64 await userEvent.click(screen.getByRole("button", { name: control }));
apps/web/src/lib/assistantCitationNavigation.test.ts
+112 -3
@@ -5,11 +5,14 @@ import {
5 createRoute,
6 createRouter,
7 } from "@tanstack/react-router";
8 +import { scopeThreadRef } from "@t3tools/client-runtime/environment";
9 import { describe, expect, it } from "vite-plus/test";
10
11 +import { selectActiveRightPanelSurface, useRightPanelStore } from "../rightPanelStore";
12 import {
13 assistantCitationFromLocation,
14 assistantCitationNavigation,
15 + revealSideChatCitation,
16 } from "./assistantCitationNavigation";
17
18 const citation: AssistantCitation = {
@@ -36,7 +39,7 @@ function createCitationRouter(initialEntry = "/environment-one/thread-one") {
39 describe("assistant citation navigation", () => {
40 it("preserves encoded quote whitespace through router navigation and reload", async () => {
41 const router = createCitationRouter();
39 - await router.navigate(assistantCitationNavigation(citation));
42 + await router.navigate(assistantCitationNavigation(citation, null));
43
44 expect(assistantCitationFromLocation(router.state.location.href)).toEqual(citation);
45 let href = router.history.location.href;
@@ -56,9 +59,9 @@ describe("assistant citation navigation", () => {
59
60 it("reactivates an identical citation instead of deduplicating its navigation", async () => {
61 const router = createCitationRouter();
59 - await router.navigate(assistantCitationNavigation(citation));
62 + await router.navigate(assistantCitationNavigation(citation, null));
63 const previous = router.state.location;
61 - await router.navigate(assistantCitationNavigation(citation));
64 + await router.navigate(assistantCitationNavigation(citation, null));
65
66 expect(router.state.location.href).toBe(previous.href);
67 expect(router.state.location.state.assistantCitationActivation).not.toBe(
@@ -67,4 +70,110 @@ describe("assistant citation navigation", () => {
70 expect(router.history.length).toBe(3);
71 expect(assistantCitationFromLocation(router.state.location.href)).toEqual(citation);
72 });
73 +
74 + it("opens a side chat citation as a tab of its parent thread", async () => {
75 + resetRightPanels();
76 + const router = createCitationRouter("/environment-one/thread-parent");
77 + await router.navigate(assistantCitationNavigation(sideChatCitation, sideChatShell));
78 + expect(router.state.location.pathname).toBe("/environment-one/thread-parent");
79 + await revealAtRoute(router);
80 +
81 + expectSideChatRevealedOnParent(router);
82 + });
83 +
84 + it("opens the side chat tab when arriving on the parent with its citation", async () => {
85 + resetRightPanels();
86 + const linked = createCitationRouter("/environment-one/thread-parent");
87 + await linked.navigate(assistantCitationNavigation(sideChatCitation, sideChatShell));
88 + const router = createCitationRouter(linked.history.location.href);
89 + await router.load();
90 + await revealAtRoute(router);
91 +
92 + expectSideChatRevealedOnParent(router);
93 + });
94 +
95 + it("moves a citation activated before its side chat shell loaded onto the parent", async () => {
96 + resetRightPanels();
97 + const router = createCitationRouter("/environment-one/thread-parent");
98 + await router.navigate(assistantCitationNavigation(sideChatCitation, null));
99 + const activation = router.state.location.state.assistantCitationActivation;
100 + expect(router.state.location.pathname).toBe("/environment-one/side-chat-one");
101 +
102 + await revealAtRoute(router);
103 +
104 + expectSideChatRevealedOnParent(router);
105 + expect(router.state.location.state.assistantCitationActivation).toBe(activation);
106 + expect(router.history.length).toBe(2);
107 + });
108 +
109 + it("keeps quotes from forks on their own thread", () => {
110 + const forkShell = { lineage: { ...sideChatShell.lineage, sideChat: false } };
111 +
112 + expect(assistantCitationNavigation(sideChatCitation, forkShell).params.threadId).toBe(
113 + sideChatCitation.threadId,
114 + );
115 + });
116 +
117 + it("leaves an unrelated route alone", async () => {
118 + resetRightPanels();
119 + const router = createCitationRouter("/environment-one/thread-other");
120 + await router.navigate({
121 + ...assistantCitationNavigation(sideChatCitation, null),
122 + params: { environmentId: citation.environmentId, threadId: ThreadId.make("thread-other") },
123 + });
124 + await revealAtRoute(router);
125 +
126 + expect(router.state.location.pathname).toBe("/environment-one/thread-other");
127 + expect(useRightPanelStore.getState().byThreadKey).toEqual({});
128 + });
129 +
130 + it("ignores a reveal for a side chat the location no longer quotes", async () => {
131 + resetRightPanels();
132 + const router = createCitationRouter("/environment-one/thread-parent");
133 + await router.navigate(assistantCitationNavigation(sideChatCitation, sideChatShell));
134 + await revealAtRoute(router, ThreadId.make("side-chat-two"));
135 +
136 + expect(useRightPanelStore.getState().byThreadKey).toEqual({});
137 + });
138 });
139 +
140 +const parentThreadId = ThreadId.make("thread-parent");
141 +const sideChatCitation = { ...citation, threadId: ThreadId.make("side-chat-one") };
142 +const sideChatShell = {
143 + lineage: {
144 + parentThreadId,
145 + relationshipToParent: "fork" as const,
146 + rootThreadId: parentThreadId,
147 + sideChat: true,
148 + },
149 +};
150 +
151 +function resetRightPanels() {
152 + useRightPanelStore.setState({ byThreadKey: {}, threadPanelVisibilityByThreadKey: {} });
153 +}
154 +
155 +/** What the route-level ChatView does once the side chat shell is known. */
156 +function revealAtRoute(
157 + router: ReturnType<typeof createCitationRouter>,
158 + sideChatThreadId = sideChatCitation.threadId,
159 +) {
160 + const { pathname, state } = router.state.location;
161 + return revealSideChatCitation(
162 + router,
163 + scopeThreadRef(citation.environmentId, ThreadId.make(pathname.split("/").at(-1) ?? "")),
164 + sideChatThreadId,
165 + parentThreadId,
166 + state.assistantCitationActivation ?? state.__TSR_key ?? "",
167 + );
168 +}
169 +
170 +function expectSideChatRevealedOnParent(router: ReturnType<typeof createCitationRouter>) {
171 + expect(router.state.location.pathname).toBe("/environment-one/thread-parent");
172 + expect(assistantCitationFromLocation(router.state.location.href)).toEqual(sideChatCitation);
173 + expect(
174 + selectActiveRightPanelSurface(
175 + useRightPanelStore.getState().byThreadKey,
176 + scopeThreadRef(citation.environmentId, parentThreadId),
177 + ),
178 + ).toMatchObject({ kind: "side-chat", threadId: sideChatCitation.threadId });
179 +}
apps/web/src/lib/assistantCitationNavigation.ts
+103 -5
@@ -1,12 +1,22 @@
1 +import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2 import { pipe } from "effect/Function";
2 -import type { AssistantCitation } from "@t3tools/contracts";
3 +import type {
4 + AssistantCitation,
5 + OrchestrationV2ThreadShell,
6 + ScopedThreadRef,
7 + ThreadId,
8 +} from "@t3tools/contracts";
9 +import { useRouter, type AnyRouter } from "@tanstack/react-router";
10 import {
11 formatAssistantCitationHref,
12 parseAssistantCitationHref,
13 } from "@t3tools/shared/assistantCitations";
14 import * as Base64Url from "effect/encoding/Base64Url";
15 import * as Result from "effect/Result";
16 +import { useEffect } from "react";
17
18 +import { useRightPanelStore } from "../rightPanelStore";
19 +import { useQuietThreadShell } from "~/state/entities";
20 import { randomUUID } from "./utils";
21
22 declare module "@tanstack/react-router" {
@@ -18,7 +28,7 @@ declare module "@tanstack/react-router" {
28 const CITATION_HASH_PREFIX = "assistant-citation=";
29
30 /** Base64url keeps router hash normalization from decoding quote whitespace or source IDs. */
21 -export function assistantCitationHash(citation: AssistantCitation) {
31 +function assistantCitationHash(citation: AssistantCitation) {
32 return `${CITATION_HASH_PREFIX}${Base64Url.encode(formatAssistantCitationHref(citation))}`;
33 }
34
@@ -46,13 +56,101 @@ export function withoutAssistantCitationHash(href: string) {
56 return href.slice(0, hashIndex);
57 }
58
49 -/** A fresh activation lets the same link reveal its source again after dismissal. */
50 -export function assistantCitationNavigation(citation: AssistantCitation) {
59 +type SourceThread = Pick<OrchestrationV2ThreadShell, "lineage"> | null;
60 +
61 +function sideChatParentId(sourceThread: SourceThread) {
62 + return sourceThread?.lineage.sideChat ? sourceThread.lineage.parentThreadId : null;
63 +}
64 +
65 +/** Side chat quotes open on their parent thread, where the side chat is a tab. */
66 +export function assistantCitationRoute(citation: AssistantCitation, sourceThread: SourceThread) {
67 return {
68 to: "/$environmentId/$threadId" as const,
53 - params: { environmentId: citation.environmentId, threadId: citation.threadId },
69 + params: {
70 + environmentId: citation.environmentId,
71 + threadId: sideChatParentId(sourceThread) ?? citation.threadId,
72 + },
73 hash: assistantCitationHash(citation),
74 resetScroll: false,
75 + };
76 +}
77 +
78 +/** A fresh activation lets the same link reveal its source again after dismissal. */
79 +export function assistantCitationNavigation(
80 + citation: AssistantCitation,
81 + sourceThread: SourceThread,
82 +) {
83 + return {
84 + ...assistantCitationRoute(citation, sourceThread),
85 state: { assistantCitationActivation: randomUUID() },
86 };
87 }
88 +
89 +/**
90 + * Reveals the location's side chat quote in its tab on `parentThreadId`, moving off the side
91 + * chat's own route. Links, reloads and late-loading shells all land here, not only clicks.
92 + */
93 +export function revealSideChatCitation(
94 + router: Pick<AnyRouter, "navigate" | "state">,
95 + routeThreadRef: ScopedThreadRef,
96 + sideChatThreadId: ThreadId,
97 + parentThreadId: ThreadId,
98 + activation: string,
99 +) {
100 + const { location } = router.state;
101 + const citation = assistantCitationFromLocation(location.href);
102 + if (
103 + citation === null ||
104 + citation.threadId !== sideChatThreadId ||
105 + citation.environmentId !== routeThreadRef.environmentId ||
106 + (routeThreadRef.threadId !== parentThreadId && routeThreadRef.threadId !== citation.threadId)
107 + ) {
108 + return;
109 + }
110 + useRightPanelStore
111 + .getState()
112 + .openSideChat(scopeThreadRef(citation.environmentId, parentThreadId), citation.threadId);
113 + if (routeThreadRef.threadId === parentThreadId) return;
114 + return router.navigate({
115 + to: "/$environmentId/$threadId",
116 + params: { environmentId: citation.environmentId, threadId: parentThreadId },
117 + hash: true,
118 + state: (state) => ({ ...state, assistantCitationActivation: activation }),
119 + replace: true,
120 + resetScroll: false,
121 + });
122 +}
123 +
124 +export function useSideChatCitationReveal(
125 + routeThreadRef: ScopedThreadRef | null,
126 + citation: AssistantCitation | null,
127 + activation: string,
128 +) {
129 + const router = useRouter();
130 + const sourceRef =
131 + routeThreadRef && citation?.environmentId === routeThreadRef.environmentId
132 + ? scopeThreadRef(citation.environmentId, citation.threadId)
133 + : null;
134 + const parentThreadId = sideChatParentId(useQuietThreadShell(sourceRef));
135 + const environmentId = routeThreadRef?.environmentId;
136 + const threadId = routeThreadRef?.threadId;
137 + const sideChatThreadId = sourceRef?.threadId;
138 + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- moves a side chat quote's router location and parent right panel once per activation, including after its shell loads
139 + useEffect(() => {
140 + if (
141 + environmentId === undefined ||
142 + threadId === undefined ||
143 + sideChatThreadId === undefined ||
144 + parentThreadId === null
145 + ) {
146 + return;
147 + }
148 + void revealSideChatCitation(
149 + router,
150 + scopeThreadRef(environmentId, threadId),
151 + sideChatThreadId,
152 + parentThreadId,
153 + activation,
154 + );
155 + }, [router, environmentId, threadId, sideChatThreadId, parentThreadId, activation]);
156 +}