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

MarkdownThreadLink.tsx

46 lines1.8 KB

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