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

MarkdownThreadLink.tsx

58 lines2.1 KB

1import { scopeProjectRef, 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";
8import { Link } from "@tanstack/react-router";
9import { MessageSquareTextIcon } from "lucide-react";
10​
11import { useProject, useThreadRefs, useThreadShell } from "../../state/entities";
12import { ProjectFavicon } from "../ProjectFavicon";
13​
14export function MarkdownThreadLink(props: {
15 readonly environmentId: EnvironmentId;
16 readonly threadId: ThreadId;
17 readonly label: string;
18}) {
19 const decodedId = percentDecodedThreadLinkId(props.threadId);
20 const written = useThreadShell(scopeThreadRef(props.environmentId, props.threadId));
21 const decoded = useThreadShell(
22 written === null && decodedId !== null ? scopeThreadRef(props.environmentId, decodedId) : null,
23 );
24 const local = written ?? decoded;
25 const refs = useThreadRefs(local === null);
26 const remote = useThreadShell(
27 local === null
28 ? resolveThreadLinkRef(scopeThreadRef(props.environmentId, props.threadId), refs)
29 : null,
30 );
31 const thread = local ?? remote;
32 const environmentId = thread?.environmentId ?? props.environmentId;
33 const threadId = thread?.id ?? props.threadId;
34 const project = useProject(
35 thread === null ? null : scopeProjectRef(environmentId, thread.projectId),
36 );
37 const title = thread?.title.trim() || props.label;
38 return (
39 <Link
40 to="/$environmentId/$threadId"
41 params={{ environmentId, threadId }}
42 title={thread === null ? "Thread no longer available" : project?.title}
43 data-markdown-copy={formatThreadLink(threadId, title)}
44 >
45 <span
46 className="ms-[0.25em] me-[0.2em] inline-flex size-[14px] [vertical-align:-0.125em]"
47 aria-hidden
48 >
49 {project === null ? (
50 <MessageSquareTextIcon className="block size-full shrink-0" />
51 ) : (
52 <ProjectFavicon project={project} className="size-full" />
53 )}
54 </span>
55 {title}
56 </Link>
57 );