import { useState, useEffect, useCallback } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Badge } from '@/components/ui/badge'; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Plus, Search, Trash2, Pencil, CloudCog, CheckCircle2, XCircle, ExternalLink, Loader2, } from 'lucide-react'; import { platformApi } from '@/services/api'; const typeLabels: Record = { public: '公有云', private: '私有云', hybrid: '混合云', }; const typeColors: Record = { public: 'text-blue-400', private: 'text-purple-400', hybrid: 'text-cyan-400', }; export function PlatformManager() { const [platforms, setPlatforms] = useState([]); const [loading, setLoading] = useState(true); const [searchQuery, setSearchQuery] = useState(''); const [selectedType, setSelectedType] = useState('all'); const [dialogOpen, setDialogOpen] = useState(false); const [editingItem, setEditingItem] = useState(null); const [submitting, setSubmitting] = useState(false); const [formData, setFormData] = useState({ name: '', code: '', type: 'public', status: 'active', website: '', description: '', }); const loadData = useCallback(async () => { setLoading(true); try { const res = await platformApi.list(); const items = (res as any)?.list || []; setPlatforms(items); } catch { setPlatforms([]); } finally { setLoading(false); } }, []); useEffect(() => { loadData(); }, [loadData]); const filtered = platforms.filter(p => { const matchSearch = p.name?.toLowerCase().includes(searchQuery.toLowerCase()) || p.code?.toLowerCase().includes(searchQuery.toLowerCase()); const matchType = selectedType === 'all' || p.type === selectedType; return matchSearch && matchType; }); const openCreate = () => { setEditingItem(null); setFormData({ name: '', code: '', type: 'public', status: 'active', website: '', description: '' }); setDialogOpen(true); }; const openEdit = (item: any) => { setEditingItem(item); setFormData({ name: item.name || '', code: item.code || '', type: item.type || 'public', status: item.status || 'active', website: item.website || '', description: item.description || '', }); setDialogOpen(true); }; const handleSubmit = async () => { if (!formData.name.trim() || !formData.code.trim()) return; setSubmitting(true); try { if (editingItem) { await platformApi.update(editingItem.id, formData); } else { await platformApi.create(formData); } setDialogOpen(false); loadData(); } catch { // error handled by http interceptor } finally { setSubmitting(false); } }; const [deleteId, setDeleteId] = useState(null); const handleDelete = (id: number) => setDeleteId(id); const confirmDelete = async () => { if (deleteId === null) return; await platformApi.delete(deleteId); setDeleteId(null); loadData(); }; return (
{/* Toolbar */}
setSearchQuery(e.target.value)} className="pl-9 h-9 bg-slate-800/50 border-slate-700" />
{/* List */} {loading ? (
) : filtered.length === 0 ? (

暂无云平台数据

) : (
{filtered.map((p) => (

{p.name}

{p.code}
{p.website && ( )}
{p.description && (

{p.description}

)}
{typeLabels[p.type] || p.type} {p.status === 'active' ? ( 启用 ) : ( 禁用 )}
))}
)} {/* Dialog */} {editingItem ? '编辑云平台' : '添加云平台'}
setFormData({ ...formData, name: e.target.value })} placeholder="如:阿里云" className="bg-slate-800/50 border-slate-700" />
setFormData({ ...formData, code: e.target.value })} placeholder="如:aliyun" className="bg-slate-800/50 border-slate-700" disabled={!!editingItem} />
setFormData({ ...formData, website: e.target.value })} placeholder="https://..." className="bg-slate-800/50 border-slate-700" />
setFormData({ ...formData, description: e.target.value })} placeholder="平台描述..." className="bg-slate-800/50 border-slate-700" />
{/* 删除确认 */} !v && setDeleteId(null)}> 确认删除

确定删除此云平台?此操作不可撤销。

); }