import { useState, useEffect, useCallback } from 'react'; import { useTranslation } from 'react-i18next'; import { httpClient } from '@/app/infra/http/HttpClient'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, } from '@/components/ui/dialog'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Trash2, Plus, ShieldCheck } from 'lucide-react'; import { toast } from 'sonner'; export interface BotAdmin { id: number; launcher_type: string; launcher_id: string; } interface BotAdminsDialogProps { botId: string; open: boolean; onOpenChange: (open: boolean) => void; admins: BotAdmin[]; onAdminsChange: () => void; } export default function BotAdminsDialog({ botId, open, onOpenChange, admins, onAdminsChange, }: BotAdminsDialogProps) { const { t } = useTranslation(); const [newType, setNewType] = useState('person'); const [newId, setNewId] = useState(''); const [adding, setAdding] = useState(false); async function handleAdd() { if (!newId.trim()) return; setAdding(true); try { await httpClient.addBotAdmin(botId, newType, newId.trim()); toast.success(t('bots.admins.addSuccess')); setNewId(''); onAdminsChange(); } catch (e: unknown) { const err = e as { msg?: string; message?: string }; toast.error(t('bots.admins.addError') + (err?.msg ?? err?.message ?? '')); } finally { setAdding(false); } } async function handleDelete(id: number) { try { await httpClient.deleteBotAdmin(botId, id); toast.success(t('bots.admins.deleteSuccess')); onAdminsChange(); } catch (e: unknown) { const err = e as { msg?: string; message?: string }; toast.error( t('bots.admins.deleteError') + (err?.msg ?? err?.message ?? ''), ); } } return ( {t('bots.admins.title')} {t('bots.admins.description')}
{/* Add row */}
setNewId(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && handleAdd()} />
{/* List */} {admins.length === 0 ? (
{t('bots.admins.noAdmins')}
) : (
{admins.map((admin) => ( ))}
{t('bots.admins.launcherType')} {t('bots.admins.launcherId')}
{admin.launcher_type === 'person' ? t('bots.admins.typePerson') : t('bots.admins.typeGroup')} {admin.launcher_id}
)}
); } // Shared hook so the session monitor and the dialog stay in sync. export function useBotAdmins(botId: string) { const [admins, setAdmins] = useState([]); const reload = useCallback(async () => { try { const res = await httpClient.getBotAdmins(botId); setAdmins(res.admins ?? []); } catch (error) { console.error('Failed to load bot admins:', error); } }, [botId]); useEffect(() => { reload(); }, [reload]); return { admins, reload }; }