import { useRef, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { useNavigate } from 'react-router-dom'; import userService from '../api/services/userService'; import Robot from '../assets/robot.svg'; import ThreeDots from '../assets/three-dots.svg'; import ContextMenu, { MenuOption } from '../components/ContextMenu'; import ConfirmationModal from '../modals/ConfirmationModal'; import { ActiveState } from '../models/misc'; import { selectToken, setAgents, setSelectedAgent, } from '../preferences/preferenceSlice'; import { Agent } from './types'; type AgentCardProps = { agent: Agent; agents: Agent[]; menuOptions?: MenuOption[]; onDelete?: (agentId: string) => void; }; export default function AgentCard({ agent, agents, menuOptions, onDelete, }: AgentCardProps) { const navigate = useNavigate(); const dispatch = useDispatch(); const token = useSelector(selectToken); const [isMenuOpen, setIsMenuOpen] = useState(false); const [deleteConfirmation, setDeleteConfirmation] = useState('INACTIVE'); const menuRef = useRef(null); const handleCardClick = () => { if (agent.status === 'published') { dispatch(setSelectedAgent(agent)); navigate('/'); } }; const defaultDelete = async (agentId: string) => { const response = await userService.deleteAgent(agentId, token); if (!response.ok) throw new Error('Failed to delete agent'); const data = await response.json(); dispatch(setAgents(agents.filter((prevAgent) => prevAgent.id !== data.id))); }; return (
{ e.stopPropagation(); setIsMenuOpen(true); }} className="absolute top-4 right-4 z-10 cursor-pointer" > options {menuOptions && ( )}
{`${agent.name}`} {agent.status === 'draft' && (

(Draft)

)}

{agent.name}

{agent.description}

{ onDelete ? onDelete(agent.id || '') : defaultDelete(agent.id || ''); setDeleteConfirmation('INACTIVE'); }} cancelLabel="Cancel" variant="danger" />
); }