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

MarkdownThreadLink.tsx

45 lines1.8 KB

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