import { useState, useEffect, useRef, useCallback } from 'react'; import { useNavigate } from 'react-router-dom'; import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs'; import { Button } from '@/components/ui/button'; import { Switch } from '@/components/ui/switch'; import { Label } from '@/components/ui/label'; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from '@/components/ui/card'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, } from '@/components/ui/dialog'; import BotForm from '@/app/home/bots/components/bot-form/BotForm'; import { BotLogListComponent } from '@/app/home/bots/components/bot-log/view/BotLogListComponent'; import BotSessionMonitor from '@/app/home/bots/components/bot-session/BotSessionMonitor'; import BotAdminsPanel from '@/app/home/bots/components/bot-admins/BotAdminsPanel'; import type { BotSessionMonitorHandle } from '@/app/home/bots/components/bot-session/BotSessionMonitor'; import { httpClient } from '@/app/infra/http/HttpClient'; import { useSidebarData } from '@/app/home/components/home-sidebar/SidebarDataContext'; import { useTranslation } from 'react-i18next'; import { Settings, FileText, Users, RefreshCw, Trash2, ShieldCheck } from 'lucide-react'; import { cn } from '@/lib/utils'; import { toast } from 'sonner'; export default function BotDetailContent({ id }: { id: string }) { const isCreateMode = id === 'new'; const navigate = useNavigate(); const { t } = useTranslation(); const { refreshBots, bots, setDetailEntityName } = useSidebarData(); // Set breadcrumb entity name useEffect(() => { if (isCreateMode) { setDetailEntityName(t('bots.createBot')); } else { const bot = bots.find((b) => b.id === id); setDetailEntityName(bot?.name ?? id); } return () => setDetailEntityName(null); }, [id, isCreateMode, bots, setDetailEntityName, t]); const [activeTab, setActiveTab] = useState('config'); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const [isRefreshingSessions, setIsRefreshingSessions] = useState(false); const sessionMonitorRef = useRef(null); // Track whether the form has unsaved changes const [formDirty, setFormDirty] = useState(false); // Enable state managed here so the header switch works const [botEnabled, setBotEnabled] = useState(true); const [enableLoaded, setEnableLoaded] = useState(false); // Fetch bot enable state useEffect(() => { if (!isCreateMode) { httpClient.getBot(id).then((res) => { setBotEnabled(res.bot.enable ?? true); setEnableLoaded(true); }); } }, [id, isCreateMode]); const handleEnableToggle = useCallback( async (checked: boolean) => { const prev = botEnabled; setBotEnabled(checked); try { // Fetch current bot data to send a complete update const res = await httpClient.getBot(id); const bot = res.bot; await httpClient.updateBot(id, { name: bot.name, description: bot.description, adapter: bot.adapter, adapter_config: bot.adapter_config, enable: checked, }); refreshBots(); } catch { setBotEnabled(prev); toast.error(t('bots.setBotEnableError')); } }, [id, botEnabled, refreshBots, t], ); function handleFormSubmit() { // Re-sync enable state after form save (form may update enable too) httpClient.getBot(id).then((res) => { setBotEnabled(res.bot.enable ?? true); }); refreshBots(); } function handleBotDeleted() { refreshBots(); navigate('/home/bots'); } function handleNewBotCreated(newBotId: string) { refreshBots(); navigate(`/home/bots?id=${encodeURIComponent(newBotId)}`); } function confirmDelete() { httpClient .deleteBot(id) .then(() => { setShowDeleteConfirm(false); toast.success(t('bots.deleteSuccess')); handleBotDeleted(); }) .catch((err) => { toast.error(t('bots.deleteError') + err.msg); }); } // ==================== Create Mode ==================== if (isCreateMode) { return (
{/* Header */}

{t('bots.createBot')}

{/* Content */}
); } // ==================== Edit Mode ==================== return ( <>
{/* Sticky Header: title + enable switch + save button */}

{t('bots.editBot')}

{enableLoaded && (
)}
{/* Horizontal Tabs */} {t('bots.configuration')} {t('bots.logs')} {t('bots.sessionMonitor.title')} {activeTab === 'sessions' && ( )} {t('bots.admins.title')} {/* Tab: Configuration */}
{/* Card: Danger Zone */} {t('bots.dangerZone')} {t('bots.dangerZoneDescription')}

{t('bots.deleteBotAction')}

{t('bots.deleteBotHint')}

{/* Tab: Logs */} {/* Tab: Sessions */} {/* Tab: Admins */}
{/* Delete confirmation dialog */} {t('common.confirmDelete')} {t('bots.deleteConfirmation')}
{t('bots.deleteConfirmation')}
); }