"use client" import { useEffect, useState } from "react" import { ArrowUpRight, Link as LinkIcon, MoreHorizontal, Trash2, StarOff, Plus, MessagesSquare, } from "lucide-react" import { toast } from "sonner" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" import { SidebarGroup, SidebarGroupLabel, SidebarMenu, SidebarMenuAction, SidebarMenuButton, SidebarMenuItem, useSidebar, } from "@/components/ui/sidebar" import { getProjects, getThreads } from "@/lib/api" import Link from "next/link" export function NavAgents() { const { isMobile, state } = useSidebar() const [agents, setAgents] = useState<{name: string, url: string}[]>([]) const [isLoading, setIsLoading] = useState(true) // Load agents dynamically from the API useEffect(() => { async function loadAgents() { try { const projectsData = await getProjects() const agentsList = [] const seenThreadIds = new Set() // Track unique thread IDs for (const project of projectsData) { const threads = await getThreads(project.id) if (threads && threads.length > 0) { // For each thread in the project, create an agent entry for (const thread of threads) { // Only add if we haven't seen this thread ID before if (!seenThreadIds.has(thread.thread_id)) { seenThreadIds.add(thread.thread_id) agentsList.push({ name: `${project.name} - ${thread.thread_id.slice(0, 4)}`, url: `/dashboard/agents/${thread.thread_id}` }) } } } } setAgents(agentsList) } catch (err) { console.error("Error loading agents for sidebar:", err) } finally { setIsLoading(false) } } loadAgents() }, []) return (
Agents New Agent
{isLoading ? ( // Show skeleton loaders while loading Array.from({length: 3}).map((_, index) => (
)) ) : agents.length > 0 ? ( // Show all agents <> {agents.map((item, index) => ( {item.name} {state !== "collapsed" && ( More { navigator.clipboard.writeText(window.location.origin + item.url) toast.success("Link copied to clipboard") }}> Copy Link Open in New Tab Delete )} ))} ) : ( // Empty state No agents yet )}
) }