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

ThreadContextChip.tsx

89 lines3.4 KB

1import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2import type { ThreadContextRecord, ProjectId } from "@t3tools/contracts";
3import { Link, useNavigate } from "@tanstack/react-router";
4import { MessagesSquareIcon } from "lucide-react";
5import { useGT } from "gt-react";
6​
7import { resolveThreadOpenRoute } from "~/lib/sideChatPane";
8import { buildThreadRouteParams } from "~/threadRoutes";
9import { useProject, useThreadShell } from "~/state/entities";
10import { orchestrationEnvironment } from "~/state/orchestration";
11import { useEnvironmentQuery } from "~/state/query";
12import { ContextChip, ContextChipLabel } from "./ContextChip";
13import { ProjectFavicon } from "./ProjectFavicon";
14import { ThreadChipHoverCard } from "./ThreadChipHoverCard";
15import { ThreadHoverCardPopup } from "./ThreadHoverCard";
16import { Tooltip, TooltipTrigger } from "./ui/tooltip";
17​
18export function ThreadContextChip(props: {
19 record: Pick<ThreadContextRecord, "environmentId" | "threadId" | "title">;
20 projectId?: ProjectId | null;
21 copyMarkdown?: string;
22 unreadable?: boolean;
23}) {
24 const gt = useGT();
25 const { environmentId, threadId } = props.record;
26 const shell = useThreadShell({ environmentId, threadId });
27 const lookup = useEnvironmentQuery(
28 shell === null && props.projectId === undefined
29 ? orchestrationEnvironment.threadLinkMetadata({
30 environmentId,
31 input: { threadIds: [threadId] },
32 })
33 : null,
34 );
35 const metadata = lookup.data?.[threadId];
36 const projectId = shell?.projectId ?? props.projectId ?? metadata?.projectId;
37 const project = useProject(projectId ? scopeProjectRef(environmentId, projectId) : null);
38 const title = shell?.title?.trim() || metadata?.title.trim() || props.record.title;
39 const navigate = useNavigate();
40 return (
41 <Tooltip>
42 <TooltipTrigger
43 render={
44 <ContextChip
45 kind="thread"
46 {...(props.unreadable ? { state: "invalid" as const } : {})}
47 render={
48 <Link
49 to="/$environmentId/$threadId"
50 params={{ environmentId, threadId }}
51 onClick={(event) => {
52 if (shell?.lineage.sideChat !== true || event.button !== 0) return;
53 if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
54 // A side chat opens as a tab beside its parent, not as its own page.
55 event.preventDefault();
56 void navigate({
57 to: "/$environmentId/$threadId",
58 params: buildThreadRouteParams(
59 resolveThreadOpenRoute({ environmentId, threadId }, shell),
60 ),
61 });
62 }}
63 draggable={false}
64 />
65 }
66 aria-label={gt("Thread, {title}", { title })}
67 data-markdown-copy={props.copyMarkdown}
68 className="no-underline"
69 >
70 {project ? (
71 <ProjectFavicon project={project} className="size-[1em] self-center" />
72 ) : (
73 <MessagesSquareIcon />
74 )}
75 <ContextChipLabel>{title}</ContextChipLabel>
76 </ContextChip>
77 }
78 />
79 <ThreadHoverCardPopup side="top">
80 <ThreadChipHoverCard
81 title={title}
82 project={project}
83 shell={shell}
84 unreadable={props.unreadable === true}
85 />
86 </ThreadHoverCardPopup>
87 </Tooltip>
88 );