import { Modal, ModalContent, ModalHeader, ModalBody, ModalFooter } from '@heroui/modal'; import { Button } from '@heroui/button'; import { Input } from '@heroui/input'; import { Select, SelectItem } from '@heroui/select'; import { Switch } from '@heroui/switch'; import { useEffect, useState } from 'react'; import toast from 'react-hot-toast'; import PluginManager, { PluginConfigSchemaItem } from '@/controllers/plugin_manager'; interface Props { isOpen: boolean; onOpenChange: () => void; pluginName: string; } export default function PluginConfigModal ({ isOpen, onOpenChange, pluginName }: Props) { const [loading, setLoading] = useState(false); const [schema, setSchema] = useState([]); const [config, setConfig] = useState({}); const [saving, setSaving] = useState(false); useEffect(() => { if (isOpen && pluginName) { loadConfig(); } }, [isOpen, pluginName]); const loadConfig = async () => { setLoading(true); setSchema([]); setConfig({}); try { const data = await PluginManager.getPluginConfig(pluginName); setSchema(data.schema || []); setConfig(data.config || {}); } catch (e: any) { toast.error('Load config failed: ' + e.message); } finally { setLoading(false); } }; const handleSave = async () => { setSaving(true); try { await PluginManager.setPluginConfig(pluginName, config); toast.success('Configuration saved'); onOpenChange(); } catch (e: any) { toast.error('Save failed: ' + e.message); } finally { setSaving(false); } }; const updateConfig = (key: string, value: any) => { setConfig((prev: any) => ({ ...prev, [key]: value })); }; const renderField = (item: PluginConfigSchemaItem) => { const value = config[item.key] ?? item.default; switch (item.type) { case 'string': return ( updateConfig(item.key, val)} description={item.description} className="mb-4" /> ); case 'number': return ( updateConfig(item.key, Number(val))} description={item.description} className="mb-4" /> ); case 'boolean': return (
{item.label} {item.description && {item.description}}
updateConfig(item.key, val)} />
); case 'select': // Handle value matching for default selected keys const selectedValue = value !== undefined ? String(value) : undefined; return ( ); case 'multi-select': const selectedKeys = Array.isArray(value) ? value.map(String) : []; return ( ); case 'html': return (
{item.label &&

{item.label}

}
{item.description &&

{item.description}

}
); case 'text': return (
{item.label &&

{item.label}

}
{item.default || ''}
{item.description &&

{item.description}

}
); default: return null; } }; return ( {(onClose) => ( <> Configuration: {pluginName} {loading ? (
Loading configuration...
) : (
{schema.length === 0 ? (
No configuration schema available.
) : ( schema.map(renderField) )}
)}
)}
); }