import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
import { formatThreadLink, percentDecodedThreadLinkId } from "@t3tools/shared/threadLinks";
import { Link } from "@tanstack/react-router";
import { MessageSquareTextIcon } from "lucide-react";
import { useProject, useThreadShell } from "../../state/entities";
import { ProjectFavicon } from "../ProjectFavicon";
/**
* A `t3-thread://` link in chat. It shows the thread's current title, so a rename
* reaches every message that links to it, and leads with the thread's project icon
* the way a web link leads with its favicon. `label` is what the message wrote,
* shown only when this client cannot see the thread. Opens the thread in the app.
*/
export function MarkdownThreadLink(props: {
readonly environmentId: EnvironmentId;
readonly threadId: ThreadId;
readonly label: string;
}) {
const decodedId = percentDecodedThreadLinkId(props.threadId);
const written = useThreadShell(scopeThreadRef(props.environmentId, props.threadId));
const decoded = useThreadShell(
written === null && decodedId !== null ? scopeThreadRef(props.environmentId, decodedId) : null,
);
const thread = written ?? decoded;
const threadId = thread?.id ?? props.threadId;
const project = useProject(
thread === null ? null : scopeProjectRef(props.environmentId, thread.projectId),
);
const title = thread?.title.trim() || props.label;
return (
{project === null ? (
) : (
)}
{title}
);
}