import { usePrimaryAgentPresentation } from "@/components/primary-agent/PrimaryAgentPresentation"; import { useEffect, useState } from "react"; import { MessageCircle } from "lucide-react"; import { AgentAvatar } from "@/components/AgentAvatar"; import { Button } from "@/components/ui/button"; import { useBreadcrumbs } from "@/context/BreadcrumbContext"; import { useAgentChatNavigation, useOpenAgentChat } from "@/hooks/useAgentChatNavigation"; import { recordedAgentChatIssueId, useRecentAgentChats } from "@/lib/recent-agent-chats"; import { Link, useNavigate } from "@/lib/router"; import { agentRouteRef } from "@/lib/utils"; export function AgentChats() { const { setBreadcrumbs } = useBreadcrumbs(); const { companyId, userId, enabled, loaded, agents, chats, session } = useAgentChatNavigation(); useEffect(() => setBreadcrumbs([{ label: "Chat" }]), [setBreadcrumbs]); return ; } function AgentChatsContent({ companyId, userId, enabled, loaded, agents, chats, session }: Pick, "companyId" | "userId" | "enabled" | "loaded" | "agents" | "chats" | "session">) { const primary = usePrimaryAgentPresentation(companyId); const openChat = useOpenAgentChat(companyId, userId); const navigate = useNavigate(); const recentIds = useRecentAgentChats(companyId ?? "", userId); const [openingId, setOpeningId] = useState(null); const [openError, setOpenError] = useState(null); const blockingError = agents.error ?? session.error; const firstId = recentIds[0]; const firstAgent = agents.data?.find(agent => agent.id === firstId); // An empty chat with an active agent can reopen from the roster alone. A // missing agent or a saved issue needs history to validate that visit. const needsHistory = !!firstId && (!firstAgent || !!recordedAgentChatIssueId(companyId ?? "", userId, firstId)); const resolvingRecent = recentIds.length > 0 && !blockingError && (!agents.isFetched || !session.isFetched || (needsHistory && !chats.isFetched && !chats.error)); const recentChatPath = loaded && enabled && companyId && !blockingError && !resolvingRecent ? recentIds.map(id => { const agent = agents.data?.find(item => item.id === id); const historyIssue = chats.data?.find(chat => chat.conversationAgentId === id); const savedIssueId = recordedAgentChatIssueId(companyId, userId, id); // A deleted or inaccessible issue must not silently reopen as a new chat. if (savedIssueId && chats.isSuccess && historyIssue?.id !== savedIssueId) return null; if (agent) return `/chats/${encodeURIComponent(agentRouteRef(agent))}`; if (historyIssue) return `/chats/${encodeURIComponent(id)}`; return null; }).find((path): path is string => path !== null) : undefined; const primaryAgent = agents.data?.find(agent => agent.id === primary?.primaryAgentId && agent.status !== "terminated" && agent.status !== "pending_approval"); const entryPath = recentChatPath ?? (loaded && enabled && !blockingError && !resolvingRecent && agents.isFetched && session.isFetched && !primary?.loading && primaryAgent ? `/chats/${encodeURIComponent(agentRouteRef(primaryAgent))}` : undefined); useEffect(() => { if (entryPath) navigate(entryPath, { replace: true }); }, [navigate, entryPath]); if (!loaded) return

Loading chat…

; if (!enabled) return

Agent Chat is disabled. Enable it in Experimental settings.

; if (!companyId) return

Select a company to start a conversation.

; if (resolvingRecent || entryPath || primary?.loading) return

Opening chat…

; const error = blockingError ?? (recentIds.length > 0 ? chats.error : null); return

Who would you like to talk to?

Ask a question, think through an idea, or plan the next step with your team.

{error ?

Couldn’t load your chats.

: agents.isPending || session.isPending ?

Loading agents…

:
{(agents.data ?? []).slice(0, 6).map(agent => )}
} {openError &&

{openError}

} {agents.data?.length === 0 &&

Add an agent to start a conversation.

}
; }