import { Modal } from "antd"; import { Check, FileText, FolderOpen, Image as ImageIcon, LoaderCircle, Music2, Search, Upload, UserRound, Video } from "lucide-react"; import { useEffect, useMemo, useRef, useState, type ReactNode } from "react"; import { AssetMediaPreview } from "@/components/asset-media-preview"; import { cn } from "@/lib/utils"; import type { Asset } from "@/stores/use-asset-store"; export type AssetLibraryPickerItem = { id: string; title: string; category: string; kindLabel: string; asset?: Asset; imageUrl?: string; imageFit?: "cover" | "contain"; description?: string; searchText?: string; disabledReason?: string; }; type Props = { open: boolean; items: AssetLibraryPickerItem[]; categoryLabels: Record; initialCategory?: string; initialSelectedIds?: Iterable; multiple?: boolean; title?: string; eyebrow?: string; confirmLabel?: (count: number) => string; emptyTitle?: string; emptyDescription?: string; footerNote?: string; upload?: { accept: string; description: string; onUpload: (files: FileList) => Promise; }; onClose: () => void; onConfirm: (ids: string[]) => Promise | void; }; export function AssetLibraryPickerModal({ open, items, categoryLabels, initialCategory = "all", initialSelectedIds, multiple = true, title = "素材库", eyebrow = "参考内容", confirmLabel = (count) => `使用已选素材${count ? `(${count})` : ""}`, emptyTitle = "这个分类还没有素材", emptyDescription = "换个分类后再试。", footerNote, upload, onClose, onConfirm, }: Props) { const [category, setCategory] = useState(initialCategory); const [keyword, setKeyword] = useState(""); const [selected, setSelected] = useState>(new Set()); const [working, setWorking] = useState(false); const [uploadingCount, setUploadingCount] = useState(0); const [error, setError] = useState(""); const uploadInputRef = useRef(null); const initialSelectedIdsRef = useRef(initialSelectedIds); const itemsRef = useRef(items); initialSelectedIdsRef.current = initialSelectedIds; itemsRef.current = items; const categories = useMemo(() => ["all", ...Array.from(new Set(items.map((item) => item.category || "other")))], [items]); const visibleItems = useMemo(() => { const query = keyword.trim().toLowerCase(); return items.filter((item) => { if (category !== "all" && item.category !== category) return false; return !query || [item.title, item.searchText || "", item.description || ""].join(" ").toLowerCase().includes(query); }); }, [category, items, keyword]); const selectedIds = useMemo( () => items.filter((item) => !item.disabledReason && selected.has(item.id)).map((item) => item.id), [items, selected], ); useEffect(() => { if (!open) return; setCategory(initialCategory); setKeyword(""); const selectableIds = new Set(itemsRef.current.filter((item) => !item.disabledReason).map((item) => item.id)); setSelected(new Set(Array.from(initialSelectedIdsRef.current || []).filter((id) => selectableIds.has(id)))); setWorking(false); setUploadingCount(0); setError(""); }, [initialCategory, open]); useEffect(() => { if (category === "all" || categories.includes(category)) return; setCategory("all"); }, [categories, category]); const toggle = (item: AssetLibraryPickerItem) => { if (item.disabledReason || working) return; setError(""); setSelected((current) => { if (!multiple) return current.has(item.id) ? new Set() : new Set([item.id]); const next = new Set(current); if (next.has(item.id)) next.delete(item.id); else next.add(item.id); return next; }); }; const confirm = async () => { if (!selectedIds.length || working) return; setWorking(true); setError(""); try { await onConfirm(selectedIds); } catch (reason) { setError(reason instanceof Error ? reason.message : "素材操作失败,请重试"); } finally { setWorking(false); } }; const handleUpload = async (files: FileList | null) => { if (!upload || !files?.length || working) return; setWorking(true); setError(""); setUploadingCount(files.length); try { const ids = await upload.onUpload(files); if (ids.length) setSelected((current) => new Set(multiple ? [...current, ...ids] : ids.slice(-1))); } catch (reason) { setError(reason instanceof Error ? reason.message : "素材上传失败,请重试"); } finally { if (uploadInputRef.current) uploadInputRef.current.value = ""; setWorking(false); setUploadingCount(0); } }; const countFor = (value: string) => value === "all" ? items.length : items.filter((item) => item.category === value).length; const uploading = uploadingCount > 0; return ( { if (!working) onClose(); }} width="min(1040px, calc(100vw - 24px))" className="asset-library-picker-modal" styles={{ container: { padding: 0 }, body: { padding: 0 } }} >
{eyebrow}{title}
已选 {selectedIds.length} · {visibleItems.length} 个素材
{visibleItems.length ? visibleItems.map((item) => ( toggle(item)} /> )) : (
{emptyTitle}{upload ? "换个分类,或从底部上传一份新素材。" : emptyDescription}
)}
{upload ? ( <> void handleUpload(event.target.files)} /> ) : footerNote ? {footerNote} : } {error ? {error} : null}
); } function PickerCard({ item, selected, onToggle }: { item: AssetLibraryPickerItem; selected: boolean; onToggle: () => void }) { const disabled = Boolean(item.disabledReason); return ( ); } function kindIcon(label: string): ReactNode { if (label.includes("角色")) return ; if (label.includes("视频")) return