import { ActionBarPrimitive, MessagePrimitive, useAssistantState } from "@assistant-ui/react"; import { useAtomValue } from "jotai"; import { FileText, PencilIcon } from "lucide-react"; import { type FC, useState } from "react"; import { messageDocumentsMapAtom } from "@/atoms/chat/mentioned-documents.atom"; import { TooltipIconButton } from "@/components/assistant-ui/tooltip-icon-button"; interface AuthorMetadata { displayName: string | null; avatarUrl: string | null; } const UserAvatar: FC = ({ displayName, avatarUrl }) => { const [hasError, setHasError] = useState(false); const initials = displayName ? displayName .split(" ") .map((n) => n[0]) .join("") .toUpperCase() .slice(0, 2) : "U"; if (avatarUrl && !hasError) { return ( {displayName setHasError(true)} /> ); } return (
{initials}
); }; export const UserMessage: FC = () => { const messageId = useAssistantState(({ message }) => message?.id); const messageDocumentsMap = useAtomValue(messageDocumentsMapAtom); const mentionedDocs = messageId ? messageDocumentsMap[messageId] : undefined; const metadata = useAssistantState(({ message }) => message?.metadata); const author = metadata?.custom?.author as AuthorMetadata | undefined; return (
{/* Display mentioned documents */} {mentionedDocs && mentionedDocs.length > 0 && (
{/* Mentioned documents as chips */} {mentionedDocs?.map((doc) => ( {doc.title} ))}
)} {/* Message bubble with action bar positioned relative to it */}
{/* User avatar - only shown in shared chats */} {author && (
)}
); }; const UserActionBar: FC = () => { const isThreadRunning = useAssistantState(({ thread }) => thread.isRunning); // Get current message ID const currentMessageId = useAssistantState(({ message }) => message?.id); // Find the last user message ID in the thread (computed once, memoized by selector) const lastUserMessageId = useAssistantState(({ thread }) => { const messages = thread.messages; for (let i = messages.length - 1; i >= 0; i--) { if (messages[i].role === "user") { return messages[i].id; } } return null; }); // Simple comparison - no iteration needed per message const isLastUserMessage = currentMessageId === lastUserMessageId; // Show edit button only on the last user message and when thread is not running const canEdit = isLastUserMessage && !isThreadRunning; return ( {/* Only allow editing the last user message */} {canEdit && ( )} ); };