1import type { ThreadContextRecord } from "@t3tools/contracts";
2import { Link } from "@tanstack/react-router";
3import { MessagesSquareIcon } from "lucide-react";
4
5import { useThreadShell } from "~/state/entities";
6import { ContextChip, ContextChipLabel } from "./ContextChip";
7import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
8
9/**
10 * Inline chip for an attached thread, in the composer and in sent messages. Prefers the
11 * live title so a renamed thread never shows a stale label, and opens the thread on click.
12 */
13export function ThreadContextChip(props: {
14 record: Pick<ThreadContextRecord, "environmentId" | "threadId" | "title">;
15 copyMarkdown?: string;
16}) {
17 const { environmentId, threadId } = props.record;
18 const shell = useThreadShell({ environmentId, threadId });
19 const title = shell?.title?.trim() || props.record.title;
20 return (
21 <Tooltip>
22 <TooltipTrigger
23 render={
24 <ContextChip
25 kind="thread"
26 render={<Link to="/$environmentId/$threadId" params={{ environmentId, threadId }} />}
27 aria-label={`Thread, ${title}`}
28 data-markdown-copy={props.copyMarkdown}
29 className="no-underline"
30 >
31 <MessagesSquareIcon />
32 <ContextChipLabel>{title}</ContextChipLabel>
33 </ContextChip>
34 }
35 />
36 <TooltipPopup side="top">{shell ? "Open thread" : "Thread no longer available"}</TooltipPopup>
37 </Tooltip>
38 );
39}