import { useEffect, useState } from 'react' import { useInfiniteQuery } from '@tanstack/react-query' import { useInView } from 'react-intersection-observer' import { client } from '@/ui/api/client' import { m } from '@/ui/paraglide/messages.js' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/ui/components/ui/card' import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/ui/components/ui/table' import { Avatar, AvatarFallback, AvatarImage } from '@/ui/components/ui/avatar' import { Badge } from '@/ui/components/ui/badge' import { Loader2, MessagesSquare } from 'lucide-react' import { formatTimeAgo } from '@/ui/lib/time' import { AgentSessionLogsDialog } from '../agent/agent-session-logs-dialog' interface AgentSessionsDashboardProps { teamId: string } export function AgentSessionsDashboard({ teamId }: AgentSessionsDashboardProps) { const [selectedSessionId, setSelectedSessionId] = useState(null) const { ref: loadMoreRef, inView } = useInView() const { data, isLoading, isError, fetchNextPage, hasNextPage, isFetchingNextPage } = useInfiniteQuery({ queryKey: ['teams', teamId, 'agent-sessions'], queryFn: async ({ pageParam }) => { const res = await client.api.teams[':teamId']['agent-sessions'].$get({ param: { teamId }, query: { first: '20', after: pageParam ? String(pageParam) : undefined, }, }) if (!res.ok) throw new Error('Failed to fetch agent sessions') return await res.json() }, initialPageParam: '', getNextPageParam: (lastPage) => lastPage.pageInfo.cursor || undefined, enabled: !!teamId, }) useEffect(() => { if (inView && hasNextPage && !isFetchingNextPage) { fetchNextPage() } }, [inView, hasNextPage, isFetchingNextPage, fetchNextPage]) const allSessions = data?.pages.flatMap((page) => page.data).filter((s) => s.id !== 'pending') || [] const getSessionTypeBadge = (type: string) => { switch (type) { case 'comment': return ( {m.session_type_comment()} ) case 'chat': return ( {m.session_type_chat()} ) case 'naming': return ( {m.session_type_naming()} ) default: return ( {type} ) } } return (
{m.sessions()} {m.sessions_description()}
{isLoading ? (
) : isError ? (
Failed to load agent sessions.
) : allSessions.length === 0 ? (

{m.no_sessions_found()}

) : (
{m.session_name()} {m.creator()} {m.session_type()} {m.create_time()} {allSessions.map((session) => ( setSelectedSessionId(session.id)} className="cursor-pointer hover:bg-muted/50 transition-colors" >
{session.name || m.untitled_session()} {session.id}
{session.creator?.name?.[0]?.toUpperCase() || '?'} {session.creator?.name || 'System / Agent'}
{getSessionTypeBadge(session.type)} {formatTimeAgo(session.createdAt)}
))}
{isFetchingNextPage && ( )}
)}
{ if (!open) setSelectedSessionId(null) }} />
) }