import { useQueryState } from 'nuqs' import { SessionEntry } from '@/types/playground' import { Button } from '../../../ui/button' import useSessionLoader from '@/hooks/useSessionLoader' import { deletePlaygroundSessionAPI } from '@/api/playground' import { usePlaygroundStore } from '@/store' import { toast } from 'sonner' import Icon from '@/components/ui/icon' import { useState } from 'react' import DeleteSessionModal from './DeleteSessionModal' import useChatActions from '@/hooks/useChatActions' import { truncateText, cn } from '@/lib/utils' type SessionItemProps = SessionEntry & { isSelected: boolean onSessionClick: () => void } const SessionItem = ({ title, session_id, isSelected, onSessionClick }: SessionItemProps) => { const [agentId] = useQueryState('agent') const { getSession } = useSessionLoader() const [, setSessionId] = useQueryState('session') const { selectedEndpoint, sessionsData, setSessionsData } = usePlaygroundStore() const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false) const { clearChat } = useChatActions() const handleGetSession = async () => { if (agentId) { onSessionClick() await getSession(session_id, agentId) setSessionId(session_id) } } const handleDeleteSession = async () => { if (agentId) { try { const response = await deletePlaygroundSessionAPI( selectedEndpoint, agentId, session_id ) if (response.status === 200 && sessionsData) { setSessionsData( sessionsData.filter((session) => session.session_id !== session_id) ) clearChat() toast.success('Session deleted') } else { toast.error('Failed to delete session') } } catch { toast.error('Failed to delete session') } finally { setIsDeleteModalOpen(false) } } } return ( <>

{truncateText(title, 20)}

setIsDeleteModalOpen(false)} onDelete={handleDeleteSession} isDeleting={false} /> ) } export default SessionItem