import { App, Button, Dropdown, Popconfirm } from "antd"; import type { MenuProps } from "antd"; import { AppModal } from "@/components/ui/product/app-modal"; import { Check, ChevronDown, FileText, FolderOpen, HardDrive, Image as ImageIcon, LoaderCircle, Music2, Puzzle, RotateCcw, Search, Trash2, Upload, UserRound, Video } from "lucide-react"; import { useEffect, useMemo, useRef, useState, type ReactNode } from "react"; import { useQuery } from "@tanstack/react-query"; import { useUserStore } from "@/stores/use-user-store"; import { AssetMediaPreview } from "@/components/asset-media-preview"; import { AssetLibraryCard } from "@/components/assets/asset-library-card"; import { CachedResourceImage } from "@/components/cached-resource-image"; import { PaginationBar } from "@/components/layout/workspace-page"; import { cn } from "@/lib/utils"; import type { ExternalAssetPickerReference } from "@/lib/plugins/plugin-types"; import { flushAssetStorePersistence, useAssetStore, type Asset } from "@/stores/use-asset-store"; import { deleteAssetWithRemoteSync, loadAssetLibraryPage, localSavedRemotePendingMessage, saveRemoteUserDataNow } from "@/services/user-data-sync"; export type AssetPickerMediaKind = "image" | "video" | "audio" | "text"; export const ASSET_PICKER_MEDIA_KIND_LABELS: Record = { image: "图片", video: "视频", audio: "音频", text: "文本" }; const DEFAULT_MEDIA_KINDS: AssetPickerMediaKind[] = ["image", "video", "audio"]; export type AssetLibraryPickerItem = { id: string; title: string; category: string; archived?: boolean; kindLabel: string; /** 媒体类型筛选依据;缺省时按本地素材或插件素材的 kind 推断。 */ mediaKind?: AssetPickerMediaKind; asset?: Asset; imageUrl?: string; imageStorageKey?: string; imageFit?: "cover" | "contain"; description?: string; searchText?: string; disabledReason?: string; folderId?: string; external?: ExternalAssetPickerReference; }; export type AssetLibraryPickerFolder = { id: string; parentId?: string; name: string; }; type Props = { remoteLibrary?: boolean; remoteKind?: string; /** 左侧「媒体类型」筛选项;只有一种类型或已由 remoteKind 固定时不展示该分组。 */ mediaKinds?: AssetPickerMediaKind[]; open: boolean; items: AssetLibraryPickerItem[]; categoryLabels: Record; initialCategory?: string; initialFolderId?: string; folders?: AssetLibraryPickerFolder[]; initialSelectedIds?: Iterable; multiple?: boolean; title?: string; eyebrow?: string; confirmLabel?: (count: number) => string; emptyTitle?: string; emptyDescription?: string; footerNote?: string; loading?: boolean; pagination?: { current: number; pageSize: number; total: number; onChange: (page: number, pageSize: number) => void }; folderActionLabel?: string; folderActionSource?: "local" | "all"; upload?: { accept: string; description: string; onUpload: (files: FileList) => Promise; external?: { accept: string; description: string; onUpload: (files: FileList, folderId?: string) => Promise; }; }; onClose: () => void; onConfirm: (ids: string[]) => Promise | void; onFolderAction?: (folderId: string) => Promise | void; }; export function AssetLibraryPickerModal({ remoteLibrary = false, remoteKind, mediaKinds = DEFAULT_MEDIA_KINDS, open, items, categoryLabels, initialCategory = "all", initialFolderId = "all", folders = [], initialSelectedIds, multiple = true, title = "素材库", eyebrow = "参考内容", confirmLabel = (count) => `使用已选素材${count ? `(${count})` : ""}`, emptyTitle = "这个分类还没有素材", emptyDescription = "换个分类后再试。", footerNote, loading = false, pagination, folderActionLabel = "将文件夹放到画布", folderActionSource = "all", upload, onClose, onConfirm, onFolderAction, }: Props) { const { message } = App.useApp(); const [category, setCategory] = useState(initialCategory); const [mediaKind, setMediaKind] = useState("all"); const [folderId, setFolderId] = useState(initialFolderId); const [source, setSource] = useState<"local" | "plugin">("local"); const [sourceMenuOpen, setSourceMenuOpen] = useState(false); const [keyword, setKeyword] = useState(""); const [selected, setSelected] = useState>(new Set()); const [uploadedItems, setUploadedItems] = useState([]); const [working, setWorking] = useState(false); const [uploadingCount, setUploadingCount] = useState(0); const [error, setError] = useState(""); const userId = useUserStore((state) => state.user?.id); const sessionHydrated = useUserStore((state) => state.hydrated); const [remotePage, setRemotePage] = useState(1); const [remotePageSize, setRemotePageSize] = useState(40); const [remoteKeyword, setRemoteKeyword] = useState(""); const remoteEnabled = remoteLibrary && Boolean(userId) && source === "local"; useEffect(() => { const timer = window.setTimeout(() => setRemoteKeyword(keyword.trim()), 250); return () => window.clearTimeout(timer); }, [keyword]); useEffect(() => setRemotePage(1), [category, mediaKind, remoteKeyword, open]); // remoteKind 是调用方写死的能力约束;媒体类型筛选只在没有该约束时参与服务端查询。 const remoteQueryKind = remoteKind || (mediaKind === "all" ? undefined : mediaKind); const remoteQuery = useQuery({ queryKey: ["asset-picker", userId, remotePage, remotePageSize, category, remoteKeyword, remoteQueryKind], queryFn: ({ signal }) => loadAssetLibraryPage({ page: remotePage, pageSize: remotePageSize, kind: remoteQueryKind, category: category === "all" || category === "archived" || category === remoteQueryKind ? undefined : category, status: category === "archived" ? "archived" : "active", query: remoteKeyword, signal }), enabled: remoteEnabled && open && sessionHydrated, }); const remoteItems = useMemo(() => (remoteQuery.data?.assets || []).filter((asset) => asset.kind !== "entity" && asset.kind !== "model").map((asset) => ({ id: asset.id, title: asset.title, category: asset.category || "other", archived: asset.status === "archived", asset, kindLabel: asset.kind === "image" ? "图片" : asset.kind === "video" ? "视频" : asset.kind === "audio" ? "音频" : "文本", searchText: (asset.tags ?? []).join(" "), ...(items.find((item) => item.id === asset.id) || { disabledReason: "此素材不适用于当前操作" }), })), [remoteQuery.data, items]); const uploadInputRef = useRef(null); const initialSelectedIdsRef = useRef(initialSelectedIds); const itemsRef = useRef(items); initialSelectedIdsRef.current = initialSelectedIds; const allItems = useMemo(() => { const known = new Set(items.map((item) => item.id)); return [...items, ...uploadedItems.filter((item) => !known.has(item.id))]; }, [items, uploadedItems]); itemsRef.current = allItems; const localItems = useMemo(() => allItems.filter((item) => !item.external), [allItems]); // 远端成功且有可展示素材时用远端。真正的空结果保持空列表。 // 仅在远端空而本地仍有素材、或远端总数>0 但本页全被排除时回退本地,避免合法空搜索被缓存铺满。 const remoteTotal = remoteQuery.data?.total ?? 0; const remoteReady = remoteEnabled && remoteQuery.isSuccess; const preferLocalUnsynced = remoteReady && remoteTotal === 0 && localItems.length > 0; const remoteEntityOnlyPage = remoteReady && remoteItems.length === 0 && remoteTotal > 0; const useRemoteItems = remoteReady && !preferLocalUnsynced && !remoteEntityOnlyPage && (remoteItems.length > 0 || remoteTotal === 0); const effectivePagination = useRemoteItems ? { current: remotePage, pageSize: remotePageSize, total: remoteTotal, onChange: (page: number, pageSize: number) => { setRemotePage(page); setRemotePageSize(pageSize); } } : pagination; const pluginItems = useMemo(() => allItems.filter((item) => Boolean(item.external)), [allItems]); const hasPluginSource = useMemo(() => Object.keys(categoryLabels).some((value) => value.startsWith("external:")) || pluginItems.some((item) => item.category.startsWith("external:")), [categoryLabels, pluginItems]); // 媒体类型在分类之前收窄数据源,让左侧分类计数、网格和分页始终描述同一批素材。 const sourceItems = useMemo(() => { const base = source === "plugin" ? pluginItems : useRemoteItems ? remoteItems : localItems; if (mediaKind === "all") return base; return base.filter((item) => pickerItemMediaKind(item) === mediaKind); }, [localItems, mediaKind, pluginItems, useRemoteItems, remoteItems, source]); const activeSourceItems = useMemo(() => sourceItems.filter((item) => !item.archived), [sourceItems]); const archivedItems = useMemo(() => sourceItems.filter((item) => item.archived), [sourceItems]); const mediaKindOptions = useMemo(() => (remoteKind ? [] : Array.from(new Set(mediaKinds))), [mediaKinds, remoteKind]); const sourceFolders = source === "plugin" ? folders : []; const showCategories = source === "local" || !sourceFolders.length; const normalCategories = useMemo(() => useRemoteItems ? Object.keys(categoryLabels).filter((value) => value !== "archived" && !value.startsWith("external:")) : ["all", ...Array.from(new Set(activeSourceItems.map((item) => item.category || "other"))).filter((value) => value !== "all")], [activeSourceItems, categoryLabels, useRemoteItems]); const archivedCount = archivedItems.length; const isRecycleBin = category === "archived"; const visibleItems = useMemo(() => { const query = keyword.trim().toLowerCase(); return sourceItems.filter((item) => { if (category === "archived") { if (!item.archived) return false; } else if (item.archived || (category !== "all" && item.category !== category)) { return false; } if (folderId !== "all" && (item.folderId || "") !== folderId) return false; return useRemoteItems || !query || [item.title, item.searchText || "", item.description || ""].join(" ").toLowerCase().includes(query); }); }, [category, folderId, keyword, sourceItems, useRemoteItems]); const selectedIds = useMemo( () => Array.from(selected).filter((id) => { const item = allItems.find((entry) => entry.id === id); return !item?.disabledReason; }), [allItems, selected], ); const archivedSelectedIds = useMemo(() => selectedIds.filter((id) => allItems.find((item) => item.id === id)?.archived), [allItems, selectedIds]); useEffect(() => { if (!open) return; setFolderId(initialFolderId); setCategory(initialCategory); setMediaKind("all"); setSource("local"); setKeyword(""); setUploadedItems([]); 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, initialFolderId, open]); useEffect(() => { if (category === "all" || category === "archived" || normalCategories.includes(category)) return; setCategory("all"); }, [normalCategories, category]); useEffect(() => { if (hasPluginSource || source === "local") return; setSource("local"); }, [hasPluginSource, source]); useEffect(() => { if (mediaKind === "all" || mediaKindOptions.includes(mediaKind)) return; setMediaKind("all"); }, [mediaKind, mediaKindOptions]); const selectSource = (nextSource: "local" | "plugin") => { if (nextSource === "plugin" && !hasPluginSource) return; setSource(nextSource); setCategory("all"); setFolderId("all"); setError(""); }; 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 handleRestoreSelected = async () => { if (!archivedSelectedIds.length) return; setWorking(true); try { for (const id of archivedSelectedIds) { useAssetStore.getState().updateAsset(id, { status: "confirmed" }); } await flushAssetStorePersistence(); await saveRemoteUserDataNow(); setSelected(new Set()); message.success(`已还原 ${archivedSelectedIds.length} 个素材至素材库`); setCategory("all"); } catch (error) { message.warning(localSavedRemotePendingMessage("已在本地还原", error)); } finally { setWorking(false); if (remoteEnabled) void remoteQuery.refetch(); } }; const handleDeleteSelected = async () => { if (!archivedSelectedIds.length) return; setWorking(true); try { for (const id of archivedSelectedIds) await deleteAssetWithRemoteSync(id); setSelected(new Set()); message.success(`已彻底删除 ${archivedSelectedIds.length} 个素材`); } catch (err) { message.error(err instanceof Error ? err.message : "删除失败"); } finally { setWorking(false); if (remoteEnabled) void remoteQuery.refetch(); } }; const handleEmptyRecycleBin = async () => { const toDelete = archivedItems; if (!toDelete.length) return; setWorking(true); try { for (const item of toDelete) await deleteAssetWithRemoteSync(item.id); setSelected(new Set()); message.success(`已删除${remoteEnabled ? "当前页" : "回收站"} ${toDelete.length} 个素材`); setCategory("all"); } catch (err) { message.error(err instanceof Error ? err.message : "清空回收站失败"); } finally { setWorking(false); if (remoteEnabled) void remoteQuery.refetch(); } }; const handleUpload = async (files: FileList | null) => { if (!files?.length || working || (source === "local" && !upload) || (source === "plugin" && !upload?.external)) return; setWorking(true); setError(""); setUploadingCount(files.length); try { if (source === "plugin") { const uploaded = await upload!.external!.onUpload(files, folderId === "all" ? undefined : folderId); setUploadedItems((current) => [...current, ...uploaded]); const ids = uploaded.map((item) => item.id); if (ids.length) setSelected((current) => new Set(multiple ? [...current, ...ids] : ids.slice(-1))); } else { 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 runFolderAction = async () => { if (!onFolderAction || folderId === "all" || working) return; setWorking(true); setError(""); try { await onFolderAction(folderId); } catch (reason) { setError(reason instanceof Error ? reason.message : "文件夹操作失败,请重试"); } finally { setWorking(false); } }; const countFor = (value: string) => (value === "all" ? activeSourceItems.length : activeSourceItems.filter((item) => item.category === value).length); const sourceLabel = source === "plugin" ? "插件来源" : "本地素材"; const sourceMenuItems: MenuProps["items"] = [ { key: "local", icon: