"use client" import { useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Card, CardContent } from "@/components/ui/card" import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" import { Input } from "@/components/ui/input" import { Search, Plus, Trash2 } from "lucide-react" import type { ContentLibrary } from "@/types/group-push" import { api } from "@/lib/api" import { showToast } from "@/lib/toast" interface ContentSelectorProps { selectedLibraries: ContentLibrary[] onLibrariesChange: (libraries: ContentLibrary[]) => void onPrevious: () => void onNext: () => void onSave: () => void onCancel: () => void } export function ContentSelector({ selectedLibraries = [], onLibrariesChange, onPrevious, onNext, onSave, onCancel, }: ContentSelectorProps) { const [isDialogOpen, setIsDialogOpen] = useState(false) const [searchTerm, setSearchTerm] = useState("") const [libraries, setLibraries] = useState([]) const [loading, setLoading] = useState(false) // 拉取内容库列表 const fetchLibraries = async (keyword = "") => { setLoading(true) try { const params = new URLSearchParams({ page: "1", limit: "100", keyword: keyword.trim(), }) const res = await api.get(`/v1/content/library/list?${params.toString()}`) as any if (res.code === 200 && Array.isArray(res.data?.list)) { setLibraries(res.data.list) } else { setLibraries([]) showToast(res.msg || "获取内容库失败", "error") } } catch (e) { setLibraries([]) showToast((e as any)?.message || "网络错误", "error") } finally { setLoading(false) } } // 弹窗打开/搜索时拉取 useEffect(() => { if (isDialogOpen) { fetchLibraries(searchTerm) } // eslint-disable-next-line }, [isDialogOpen]) const handleSearch = () => { fetchLibraries(searchTerm) } const handleAddLibrary = (library: ContentLibrary) => { if (!selectedLibraries.some((l) => l.id === library.id)) { onLibrariesChange([...selectedLibraries, library]) } setIsDialogOpen(false) } const handleRemoveLibrary = (libraryId: string) => { onLibrariesChange(selectedLibraries.filter((library) => library.id !== libraryId)) } const filteredLibraries = libraries.filter((library) => library.name.toLowerCase().includes(searchTerm.toLowerCase()), ) return (
* 选择内容库:
{selectedLibraries.length > 0 ? ( 序号 内容库名称 采集对象 操作 {selectedLibraries.map((library, index) => ( {index + 1} {library.name}
{(((library as any).sourceType === 1 ? (library as any).selectedFriends : (library as any).selectedGroups) || []).map((target: any) => (
{target.nickname
))}
))}
) : (
请点击"选择内容库"按钮添加内容库
)}
选择内容库
setSearchTerm(e.target.value)} onKeyDown={e => e.key === 'Enter' && handleSearch()} />
{loading ? (
加载中...
) : filteredLibraries.length === 0 ? (
暂无内容库
) : ( 序号 内容库名称 采集对象 操作 {filteredLibraries.map((library, index) => ( {index + 1} {library.name}
{(((library as any).sourceType === 1 ? (library as any).selectedFriends : (library as any).selectedGroups) || []).map((target: any) => (
{target.nickname
))}
))}
)}
) }