import { useMemo, useState } from "react"; import { ChevronDown } from "lucide-react"; import type { Agent } from "@paperclipai/shared"; import { AgentChatPicker } from "./AgentChatPicker"; import { AgentAvatar } from "./AgentAvatar"; import { Button } from "./ui/button"; import { useAgentChatNavigation } from "@/hooks/useAgentChatNavigation"; import { useNavigate } from "@/lib/router"; import { agentRouteRef } from "@/lib/utils"; /** Switching opens the existing conversation or its unsaved draft view. * The production chat controller persists it only when the user sends. */ export function CoordinatorAgentSwitcher({ agent }: { agent: Agent }) { const [open, setOpen] = useState(false); const { agents, chats } = useAgentChatNavigation(); const navigate = useNavigate(); const chatAgentIds = useMemo(() => { const existing: string[] = []; const unread = new Map(); for (const chat of chats.data ?? []) { if (!chat.conversationAgentId) continue; existing.push(chat.conversationAgentId); if (chat.unreadMessageCount) unread.set(chat.conversationAgentId, chat.unreadMessageCount); } return { existing, unread }; }, [chats.data]); return ( {agent.name} } agents={(agents.data ?? []).filter(item => item.status !== "terminated")} loading={agents.isPending} error={agents.error} onRetry={() => { void agents.refetch(); }} existingChatAgentIds={chatAgentIds.existing} unreadMessageCounts={chatAgentIds.unread} onSelect={next => { navigate(`/chats/${encodeURIComponent(agentRouteRef(next))}`); }} /> ); }