forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassistantCitationNavigation.ts
More file actions
47 lines (41 loc) · 1.59 KB
/
Copy pathassistantCitationNavigation.ts
File metadata and controls
47 lines (41 loc) · 1.59 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
import type { AssistantCitation } from "@t3tools/contracts";
import {
formatAssistantCitationHref,
parseAssistantCitationHref,
} from "@t3tools/shared/assistantCitations";
import * as Encoding from "effect/Encoding";
import * as Result from "effect/Result";
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. */
export function assistantCitationHash(citation: AssistantCitation) {
return `${CITATION_HASH_PREFIX}${Encoding.encodeBase64Url(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 parseAssistantCitationHref(
Result.getOrThrow(Encoding.decodeBase64UrlString(hash.slice(CITATION_HASH_PREFIX.length))),
);
} catch {
return null;
}
}
/** A fresh activation lets the same link reveal its source again after dismissal. */
export function assistantCitationNavigation(citation: AssistantCitation) {
return {
to: "/$environmentId/$threadId" as const,
params: { environmentId: citation.environmentId, threadId: citation.threadId },
hash: assistantCitationHash(citation),
resetScroll: false,
state: { assistantCitationActivation: randomUUID() },
};
}