import React, { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { useAuth } from '../contexts/AuthContext'; import { Box, Typography, Button, Card, CardContent, Grid, IconButton, Chip, CircularProgress, Dialog, DialogTitle, DialogContent, DialogActions, Alert, Tooltip, Container } from '@mui/material'; import { Add as AddIcon, Chat as ChatIcon, Delete as DeleteIcon, Storage as StorageIcon, Timeline as TimelineIcon, Logout as LogoutIcon, Diamond as DiamondIcon } from '@mui/icons-material'; import { listAgents, deleteAgent } from '../api/client'; function AgentList() { const navigate = useNavigate(); const { logout } = useAuth(); const [agents, setAgents] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [deleteDialog, setDeleteDialog] = useState({ open: false, agent: null }); const [deleting, setDeleting] = useState(false); useEffect(() => { loadAgents(); }, []); const loadAgents = async () => { try { setLoading(true); const response = await listAgents(); setAgents(response.agents || []); setError(null); } catch (err) { setError('Failed to load agents. Make sure the backend is running.'); console.error(err); } finally { setLoading(false); } }; const handleDelete = async () => { if (!deleteDialog.agent) return; try { setDeleting(true); await deleteAgent(deleteDialog.agent.name); setDeleteDialog({ open: false, agent: null }); loadAgents(); } catch (err) { setError(`Failed to delete agent: ${err.message}`); } finally { setDeleting(false); } }; const getDomainColor = (domain) => { const colors = { medical: '#ef4444', legal: '#3b82f6', cooking: '#f59e0b', technology: '#22c55e', finance: '#8b5cf6', education: '#06b6d4', }; return colors[domain?.toLowerCase()] || '#6b7280'; }; return ( {/* Glass Header - Consistent with Dashboard */} MEXAR Ultimate Agent Management { logout(); navigate('/login'); }} color="error" sx={{ bgcolor: 'rgba(239, 68, 68, 0.1)', '&:hover': { bgcolor: 'rgba(239, 68, 68, 0.2)' } }} > {/* Error Alert */} {error && ( setError(null)}> {error} )} {/* Loading State */} {loading && ( )} {/* Empty State */} {!loading && agents.length === 0 && ( No Agents Yet Create your first AI agent by uploading knowledge files. )} {/* Agent Grid */} {!loading && agents.length > 0 && ( {agents.map((agent) => ( navigate(`/chat/${agent.name}`)} > { e.stopPropagation(); setDeleteDialog({ open: true, agent }); }} > {agent.name.replace(/_/g, ' ').replace(/\b\w/g, (c) => c.toUpperCase())} {agent.stats?.nodes_count || 0} nodes {agent.stats?.edges_count || 0} edges {agent.created_at && ( Created: {new Date(agent.created_at).toLocaleDateString()} )} ))} )} {/* Delete Confirmation Dialog */} setDeleteDialog({ open: false, agent: null })} PaperProps={{ sx: { bgcolor: '#1a1a2e', color: 'white', border: '1px solid rgba(255,255,255,0.1)' } }} > Delete Agent Are you sure you want to delete {deleteDialog.agent?.name}? This action cannot be undone. ); } export default AgentList;