Repository navigation
Expand file tree
/
Copy pathassistantCitationNavigation.ts
More file actions
156 lines (144 loc) · 5.24 KB
/
Copy pathassistantCitationNavigation.ts
File metadata and controls
156 lines (144 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
import { pipe } from "effect/Function";
import type {
AssistantCitation,
OrchestrationV2ThreadShell,
ScopedThreadRef,
ThreadId,
} from "@t3tools/contracts";
import { useRouter, type AnyRouter } from "@tanstack/react-router";
import {
formatAssistantCitationHref,
parseAssistantCitationHref,
} from "@t3tools/shared/assistantCitations";
import * as Base64Url from "effect/encoding/Base64Url";
import * as Result from "effect/Result";
import { useEffect } from "react";
import { useRightPanelStore } from "../rightPanelStore";
import { useQuietThreadShell } from "~/state/entities";
import { randomUUID } from "./utils";
declare module "@tanstack/react-router" {
interface HistoryState {
assistantCitationActivation?: string;
}
}
const CITATION_HASH_PREFIX = "assistant-citation=";
/** Base64url keeps router hash normalization from decoding quote whitespace or source IDs. */
function assistantCitationHash(citation: AssistantCitation) {
return `${CITATION_HASH_PREFIX}${Base64Url.encode(formatAssistantCitationHref(citation))}`;
}
export function assistantCitationFromLocation(href: string) {
const hashIndex = href.indexOf("#");
if (hashIndex === -1) return null;
const hash = href.slice(hashIndex + 1);
if (!hash.startsWith(CITATION_HASH_PREFIX) || hash.length > 140_000) return null;
try {
return pipe(
hash.slice(CITATION_HASH_PREFIX.length),
Base64Url.decodeString,
Result.getOrThrow,
parseAssistantCitationHref,
);
} catch {
return null;
}
}
/** Return paths drop the quote hash so going back does not reveal its source again. */
export function withoutAssistantCitationHash(href: string) {
const hashIndex = href.indexOf("#");
if (hashIndex === -1 || !href.startsWith(CITATION_HASH_PREFIX, hashIndex + 1)) return href;
return href.slice(0, hashIndex);
}
type SourceThread = Pick<OrchestrationV2ThreadShell, "lineage"> | null;
function sideChatParentId(sourceThread: SourceThread) {
return sourceThread?.lineage.sideChat ? sourceThread.lineage.parentThreadId : null;
}
/** Side chat quotes open on their parent thread, where the side chat is a tab. */
export function assistantCitationRoute(citation: AssistantCitation, sourceThread: SourceThread) {
return {
to: "/$environmentId/$threadId" as const,
params: {
environmentId: citation.environmentId,
threadId: sideChatParentId(sourceThread) ?? citation.threadId,
},
hash: assistantCitationHash(citation),
resetScroll: false,
};
}
/** A fresh activation lets the same link reveal its source again after dismissal. */
export function assistantCitationNavigation(
citation: AssistantCitation,
sourceThread: SourceThread,
) {
return {
...assistantCitationRoute(citation, sourceThread),
state: { assistantCitationActivation: randomUUID() },
};
}
/**
* Reveals the location's side chat quote in its tab on `parentThreadId`, moving off the side
* chat's own route. Links, reloads and late-loading shells all land here, not only clicks.
*/
export function revealSideChatCitation(
router: Pick<AnyRouter, "navigate" | "state">,
routeThreadRef: ScopedThreadRef,
sideChatThreadId: ThreadId,
parentThreadId: ThreadId,
activation: string,
) {
const { location } = router.state;
const citation = assistantCitationFromLocation(location.href);
if (
citation === null ||
citation.threadId !== sideChatThreadId ||
citation.environmentId !== routeThreadRef.environmentId ||
(routeThreadRef.threadId !== parentThreadId && routeThreadRef.threadId !== citation.threadId)
) {
return;
}
useRightPanelStore
.getState()
.openSideChat(scopeThreadRef(citation.environmentId, parentThreadId), citation.threadId);
if (routeThreadRef.threadId === parentThreadId) return;
return router.navigate({
to: "/$environmentId/$threadId",
params: { environmentId: citation.environmentId, threadId: parentThreadId },
hash: true,
state: (state) => ({ ...state, assistantCitationActivation: activation }),
replace: true,
resetScroll: false,
});
}
export function useSideChatCitationReveal(
routeThreadRef: ScopedThreadRef | null,
citation: AssistantCitation | null,
activation: string,
) {
const router = useRouter();
const sourceRef =
routeThreadRef && citation?.environmentId === routeThreadRef.environmentId
? scopeThreadRef(citation.environmentId, citation.threadId)
: null;
const parentThreadId = sideChatParentId(useQuietThreadShell(sourceRef));
const environmentId = routeThreadRef?.environmentId;
const threadId = routeThreadRef?.threadId;
const sideChatThreadId = sourceRef?.threadId;
// 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
useEffect(() => {
if (
environmentId === undefined ||
threadId === undefined ||
sideChatThreadId === undefined ||
parentThreadId === null
) {
return;
}
void revealSideChatCitation(
router,
scopeThreadRef(environmentId, threadId),
sideChatThreadId,
parentThreadId,
activation,
);
}, [router, environmentId, threadId, sideChatThreadId, parentThreadId, activation]);
}