import React, { useState, useEffect, useCallback } from 'react'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Search, RefreshCw, Loader2 } from 'lucide-react'; import { fetchContentLibraryList } from '@/api/content'; import { ContentLibrary } from '@/api/content'; import { useToast } from '@/components/ui/toast'; interface ContentLibrarySelectionDialogProps { open: boolean; onOpenChange: (open: boolean) => void; selectedLibraries: string[]; onSelect: (libraries: string[]) => void; } export function ContentLibrarySelectionDialog({ open, onOpenChange, selectedLibraries, onSelect, }: ContentLibrarySelectionDialogProps) { const { toast } = useToast(); const [searchQuery, setSearchQuery] = useState(''); const [loading, setLoading] = useState(false); const [libraries, setLibraries] = useState([]); const [tempSelected, setTempSelected] = useState([]); // 获取内容库列表 const fetchLibraries = useCallback(async () => { setLoading(true); try { const response = await fetchContentLibraryList(1, 100, searchQuery); if (response.code === 200 && response.data) { setLibraries(response.data.list); } else { toast({ title: '获取内容库列表失败', description: response.msg, variant: 'destructive' }); } } catch (error) { console.error('获取内容库列表失败:', error); toast({ title: '获取内容库列表失败', description: '请检查网络连接', variant: 'destructive' }); } finally { setLoading(false); } }, [searchQuery, toast]); useEffect(() => { if (open) { fetchLibraries(); setTempSelected(selectedLibraries); } }, [open, selectedLibraries, fetchLibraries]); const handleRefresh = () => { fetchLibraries(); }; const handleSelectAll = () => { if (tempSelected.length === libraries.length) { setTempSelected([]); } else { setTempSelected(libraries.map(lib => lib.id)); } }; const handleLibraryToggle = (libraryId: string) => { setTempSelected(prev => prev.includes(libraryId) ? prev.filter(id => id !== libraryId) : [...prev, libraryId] ); }; const handleDialogOpenChange = (open: boolean) => { if (!open) { setTempSelected(selectedLibraries); } onOpenChange(open); }; const handleConfirm = () => { onSelect(tempSelected); onOpenChange(false); }; return ( 选择内容库
setSearchQuery(e.target.value)} />
已选择 {tempSelected.length} 个内容库
{loading ? (
加载中...
) : libraries.length === 0 ? (
暂无数据
) : ( libraries.map((library) => ( )) )}
已选择 {tempSelected.length} 个内容库
); }