import { useEffect, useState } from 'react'; import { useParams, Link } from 'react-router-dom'; import { useBotStore, type Bot, type SlashCommand } from '../stores/bot.ts'; import { useServerStore } from '../stores/server.ts'; export function CommandManager() { const { id: botId } = useParams<{ id: string }>(); const fetchCommands = useBotStore((s) => s.fetchCommands); const createCommand = useBotStore((s) => s.createCommand); const deleteCommand = useBotStore((s) => s.deleteCommand); const bots = useBotStore((s) => s.bots); const fetchBots = useBotStore((s) => s.fetchBots); const error = useBotStore((s) => s.error); const servers = useServerStore((s) => s.servers); const fetchServers = useServerStore((s) => s.fetchServers); const [commands, setCommands] = useState([]); const [loading, setLoading] = useState(false); const [bot, setBot] = useState(null); const [showAdd, setShowAdd] = useState(false); const [cmdName, setCmdName] = useState(''); const [cmdDesc, setCmdDesc] = useState(''); const [cmdServer, setCmdServer] = useState(''); useEffect(() => { fetchServers(); fetchBots(); }, [fetchServers, fetchBots]); useEffect(() => { if (!botId) return; const found = bots.find((b) => b.id === botId); if (found) setBot(found); }, [bots, botId]); const loadCommands = async () => { if (!botId) return; setLoading(true); const cmds = await fetchCommands(botId); setCommands(cmds); setLoading(false); }; useEffect(() => { loadCommands(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [botId]); const handleAdd = async (e: React.FormEvent) => { e.preventDefault(); if (!botId || !cmdName.trim() || !cmdServer) return; try { const cmd = await createCommand(botId, cmdServer, cmdName.trim(), cmdDesc.trim()); setCommands((prev) => [...prev, cmd]); setCmdName(''); setCmdDesc(''); setCmdServer(''); setShowAdd(false); } catch { // error in store } }; const handleDelete = async (cmdId: string) => { if (!botId) return; if (!window.confirm('Delete this command?')) return; try { await deleteCommand(botId, cmdId); setCommands((prev) => prev.filter((c) => c.id !== cmdId)); } catch { // error in store } }; const getServerName = (serverId: string) => { const s = servers.find((sv) => sv.id === serverId); return s ? s.name : serverId.slice(0, 8); }; return (
            {'┌──────────────────────────────────┐\n'}
            {'│   === SLASH COMMANDS ===         │\n'}
            {'└──────────────────────────────────┘'}
          
{bot && (

BOT: [{bot.name}]

)} {error && (

ERR: {error}

)} {/* Add command form */}
{!showAdd ? ( ) : (

{'>'} NEW SLASH COMMAND

setCmdName(e.target.value.replace(/\s/g, '').slice(0, 32))} maxLength={32} className="terminal-input w-full" placeholder="mycommand" autoFocus /> /{cmdName || 'command'}
setCmdDesc(e.target.value.slice(0, 256))} maxLength={256} className="terminal-input w-full" placeholder="what does this command do?" />
)}
{/* Command list */} {loading && (

[loading commands...]

)} {!loading && commands.length === 0 && (

[no commands registered]

)}
{commands.map((cmd) => (

/{cmd.name}

{cmd.description && (

{cmd.description}

)}

server: {getServerName(cmd.server_id)}

))}
{/* Footer */}
{'<'} [BACK TO BOTS] [CHAT]
); }