Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

MarkdownThreadLink.tsx

41 lines1.7 KB

1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import type { EnvironmentId } from "@t3tools/contracts";
3import {
4 formatThreadLink,
5 guessThreadLinkRef,
6 percentDecodedThreadLinkId,
7 type ThreadLink,
8 unknownThreadLinkLabel,
9} from "@t3tools/shared/threadLinks";
10​
11import { useThreadRefs, useThreadShell } from "../../state/entities";
12import { UnresolvedChip } from "../contextChipParts";
13import { ThreadContextChip } from "../ThreadContextChip";
14​
15/** `label` is the link's written text; an autolink has none. */
16export function MarkdownThreadLink(props: {
17 readonly environmentId: EnvironmentId;
18 readonly link: ThreadLink;
19 readonly label: string | null;
20}) {
21 const environmentId = props.link.environmentId ?? props.environmentId;
22 const decodedId = percentDecodedThreadLinkId(props.link.threadId);
23 const written = useThreadShell(scopeThreadRef(environmentId, props.link.threadId));
24 const decoded = useThreadShell(
25 written === null && decodedId !== null ? scopeThreadRef(environmentId, decodedId) : null,
26 );
27 const local = written ?? decoded;
28 const guess = local === null && props.link.environmentId === null;
29 const refs = useThreadRefs(guess);
30 const remote = useThreadShell(guess ? guessThreadLinkRef(props.link, refs) : null);
31 const thread = local ?? remote;
32 if (thread === null) {
33 const unknown = unknownThreadLinkLabel(props.link, props.environmentId);
34 return <UnresolvedChip label={props.label ?? unknown} tooltip={unknown} />;
35 }
36 const ref = { environmentId: thread.environmentId, threadId: thread.id };
37 const title = thread.title.trim() || props.label || "Untitled thread";
38 return (
39 <ThreadContextChip record={{ ...ref, title }} copyMarkdown={formatThreadLink(ref, title)} />
40 );