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

MarkdownThreadLink.tsx

39 lines1.4 KB

1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
3import {
4 formatThreadLink,
5 percentDecodedThreadLinkId,
6 resolveThreadLinkRef,
7} from "@t3tools/shared/threadLinks";
8​
9import { useThreadRefs, useThreadShell } from "../../state/entities";
10import { ThreadContextChip } from "../ThreadContextChip";
11​
12export function MarkdownThreadLink(props: {
13 readonly environmentId: EnvironmentId;
14 readonly threadId: ThreadId;
15 readonly label: string;
16}) {
17 const decodedId = percentDecodedThreadLinkId(props.threadId);
18 const written = useThreadShell(scopeThreadRef(props.environmentId, props.threadId));
19 const decoded = useThreadShell(
20 written === null && decodedId !== null ? scopeThreadRef(props.environmentId, decodedId) : null,
21 );
22 const local = written ?? decoded;
23 const refs = useThreadRefs(local === null);
24 const remote = useThreadShell(
25 local === null
26 ? resolveThreadLinkRef(scopeThreadRef(props.environmentId, props.threadId), refs)
27 : null,
28 );
29 const thread = local ?? remote;
30 const environmentId = thread?.environmentId ?? props.environmentId;
31 const threadId = thread?.id ?? props.threadId;
32 const title = thread?.title.trim() || props.label;
33 return (
34 <ThreadContextChip
35 record={{ environmentId, threadId, title: props.label }}
36 copyMarkdown={formatThreadLink(threadId, title)}
37 />
38 );