"use client" import { useState, useEffect } from "react" import { Search, RefreshCw, Loader2 } from "lucide-react" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog" import { Input } from "@/components/ui/input" import { Button } from "@/components/ui/button" import { ScrollArea } from "@/components/ui/scroll-area" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Checkbox } from "@/components/ui/checkbox" import { Label } from "@/components/ui/label" import { Badge } from "@/components/ui/badge" import { api } from "@/lib/api" import { showToast } from "@/lib/toast" interface ContentLibrary { id: string name: string sourceType: number creatorName: string updateTime: string status: number } interface ContentLibrarySelectionDialogProps { open: boolean onOpenChange: (open: boolean) => void selectedLibraries: string[] onSelect: (libraries: string[]) => void } export function ContentLibrarySelectionDialog({ open, onOpenChange, selectedLibraries, onSelect, }: ContentLibrarySelectionDialogProps) { const [searchQuery, setSearchQuery] = useState("") const [loading, setLoading] = useState(false) const [libraries, setLibraries] = useState([]) const [tempSelected, setTempSelected] = useState([]) // 获取内容库列表 const fetchLibraries = async () => { setLoading(true) try { const queryParams = new URLSearchParams({ page: '1', limit: '100', ...(searchQuery ? { keyword: searchQuery } : {}) }) const response = await api.get<{ code: number msg: string data: { list: ContentLibrary[] total: number } }>(`/v1/content/library/list?${queryParams.toString()}`) if (response.code === 200 && response.data) { setLibraries(response.data.list) } else { showToast(response.msg || "获取内容库列表失败", "error") } } catch (error: any) { console.error("获取内容库列表失败:", error) showToast(error?.message || "请检查网络连接", "error") } finally { setLoading(false) } } useEffect(() => { if (open) { fetchLibraries() setTempSelected(selectedLibraries) } }, [open, searchQuery, selectedLibraries]) 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) } return ( 选择内容库
setSearchQuery(e.target.value)} />
已选择 {tempSelected.length} 个内容库
{loading ? (
加载中...
) : libraries.length === 0 ? (
暂无数据
) : ( libraries.map((library) => ( )) )}
{/* */}
) }