"use client"; import { useState, useCallback, useMemo, useRef } from "react"; import { Upload, X, FileText, ChevronDown, ChevronRight, FolderOpen, Cloud, } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Tabs, TabsList, TabsTrigger, TabsContent } from "@/components/ui/tabs"; import { Checkbox } from "@/components/ui/checkbox"; import { useUploadDocuments } from "@/hooks/use-upload-documents"; import { useOssImport } from "@/hooks/use-oss-import"; import { showApiError } from "@/lib/api-error-toast"; import { filterFolderFiles, parseExtensionList, type RecursiveExtensionRules, } from "@/lib/extension-filter"; import { buildFolderGlobs } from "@/lib/glob-filter"; import { partitionSelection, type OssSelection } from "@/lib/oss-selection"; import { cn } from "@/lib/utils"; import { convertMetadataValues } from "@/lib/metadata-form"; import { MetadataFieldInput } from "@/components/shared/metadata-field-input"; import { useI18n } from "@/i18n/i18n-context"; import type { MetadataField } from "@/types/knowledge-base"; import { OssImportTab, EMPTY_OSS_FILTER, type OssImportFilterState, } from "./oss-import-tab"; interface DocUploadDialogProps { knowledgeBaseName: string; metadataFields?: MetadataField[]; defaultSubspace?: string; open: boolean; onOpenChange: (open: boolean) => void; } type UploadMode = "file" | "folder" | "oss"; /** * Minimal subset of the FileSystemEntry API we rely on. Typescript ships * loose types under DOM lib, but they don't reliably expose * `createReader().readEntries(...)` — declare what we use. */ interface FsEntry { isFile: boolean; isDirectory: boolean; name: string; fullPath: string; } interface FsFileEntry extends FsEntry { isFile: true; file(success: (file: File) => void, error?: (e: Error) => void): void; } interface FsDirEntry extends FsEntry { isDirectory: true; createReader(): { readEntries( success: (entries: FsEntry[]) => void, error?: (e: Error) => void, ): void; }; } /** Tag a File with a relative path so the rest of the pipeline (filter, list, * display) can treat drag-dropped folder uploads identically to picker-based * ones. `webkitRelativePath` is normally a read-only inherited getter, but * defining a same-named own data property shadows it cleanly across browsers. */ function tagRelativePath(file: File, path: string): File { try { Object.defineProperty(file, "webkitRelativePath", { value: path, configurable: true, writable: false, }); } catch { // Some engines may freeze the prototype shape; fall back to a parallel // own property so filterFolderFiles' fallback (`|| file.name`) at least // surfaces a reasonable label. } return file; } /** Recursively walk a FileSystemEntry tree and resolve a flat File[] with * webkitRelativePath populated to the root-relative path. */ async function walkEntry(entry: FsEntry, parentPath = ""): Promise { if (entry.isFile) { const fileEntry = entry as FsFileEntry; return new Promise((resolve) => { fileEntry.file( (file) => { const relPath = parentPath ? `${parentPath}/${file.name}` : file.name; resolve([tagRelativePath(file, relPath)]); }, () => resolve([]), ); }); } if (entry.isDirectory) { const dirEntry = entry as FsDirEntry; const reader = dirEntry.createReader(); // readEntries returns at most ~100 entries per call; loop until empty. const collected: FsEntry[] = []; await new Promise((resolve) => { const readBatch = () => { reader.readEntries( (batch) => { if (batch.length === 0) { resolve(); return; } collected.push(...batch); readBatch(); }, () => resolve(), ); }; readBatch(); }); const here = parentPath ? `${parentPath}/${entry.name}` : entry.name; const nested = await Promise.all( collected.map((child) => walkEntry(child, here)), ); return nested.flat(); } return []; } /** Pull entries off a DataTransferItemList. Items without `webkitGetAsEntry` * (older browsers) silently fall back to whatever the dataTransfer.files * list resolved to. */ function extractEntries(items: DataTransferItemList): FsEntry[] { const out: FsEntry[] = []; for (let i = 0; i < items.length; i++) { const item = items[i]; if (item.kind !== "file") continue; const entry = ( item as DataTransferItem & { webkitGetAsEntry?: () => FsEntry | null; } ).webkitGetAsEntry?.(); if (entry) out.push(entry); } return out; } export function DocUploadDialog({ knowledgeBaseName, metadataFields, defaultSubspace, open, onOpenChange, }: DocUploadDialogProps) { const { t } = useI18n(); const [mode, setMode] = useState("file"); const [files, setFiles] = useState([]); const [subspace, setSubspace] = useState(defaultSubspace ?? ""); const [metadataJson, setMetadataJson] = useState(""); const [metadataValues, setMetadataValues] = useState>( {}, ); const [isDragOver, setIsDragOver] = useState(false); const [jsonPreviewOpen, setJsonPreviewOpen] = useState(false); // Folder-mode state. Raw picks are kept separately so that tweaking // include/exclude/recursive re-filters without re-opening the picker. const [folderRaw, setFolderRaw] = useState([]); const [includeInput, setIncludeInput] = useState(""); const [excludeInput, setExcludeInput] = useState(""); const [recursive, setRecursive] = useState(true); const folderInputRef = useRef(null); const fileInputRef = useRef(null); // OSS-mode state. Selection lives here (not in OssImportTab) so that // tab-mode switches can reset it cleanly via resetForm(). const [ossSelection, setOssSelection] = useState([]); const [ossFilter, setOssFilter] = useState(EMPTY_OSS_FILTER); const ossImport = useOssImport(); const hasSchemaFields = metadataFields && metadataFields.length > 0; const uploadDocuments = useUploadDocuments(); const includeList = useMemo( () => parseExtensionList(includeInput), [includeInput], ); const excludeList = useMemo( () => parseExtensionList(excludeInput), [excludeInput], ); const folderFiltered = useMemo( () => filterFolderFiles(folderRaw, { include: includeList, exclude: excludeList, recursive, } satisfies RecursiveExtensionRules), [folderRaw, includeList, excludeList, recursive], ); const effectiveFiles = mode === "folder" ? folderFiltered : files; const handleDragOver = useCallback((event: React.DragEvent) => { event.preventDefault(); // `move` cursor confuses folder drops; `copy` reads as "I'll add this". if (event.dataTransfer) event.dataTransfer.dropEffect = "copy"; setIsDragOver(true); }, []); const handleDragLeave = useCallback(() => { setIsDragOver(false); }, []); /** Walk a list of FileSystemDirectoryEntry trees and store the resulting * File[] in folderRaw. Shared between drag-drop and the toast action. * Declared before handleDropFileMode so its callback identity is stable * for that hook's dependency array. */ const importDirEntries = useCallback(async (dirs: FsEntry[]) => { const all = await Promise.all(dirs.map((d) => walkEntry(d))); const flat = all.flat(); if (flat.length > 0) { setFolderRaw((prev) => [...prev, ...flat]); } }, []); /** * File-mode drop. Tolerates the user dragging folders by (a) silently * extracting all leaf files from any dropped folder via webkitGetAsEntry * and (b) hinting via toast that 「按文件夹」 mode supports filtering. The * old behaviour of forwarding raw `dataTransfer.files` would have surfaced * a 0-byte directory entry to the API and failed the upload. */ const handleDropFileMode = useCallback( async (event: React.DragEvent) => { event.preventDefault(); setIsDragOver(false); const entries = extractEntries(event.dataTransfer.items); if (entries.length === 0) { // Browser without webkitGetAsEntry — fall back to the file list. Any // raw "directory" entry will show up as a zero-byte File with empty // type, which we filter out below as a defensive measure. const fallback = Array.from(event.dataTransfer.files).filter( (f) => f.size > 0 || f.type !== "", ); if (fallback.length > 0) { setFiles((prev) => [...prev, ...fallback]); } return; } const fileEntries = entries.filter((e) => e.isFile); const dirEntries = entries.filter((e) => e.isDirectory); // Resolve File objects from the file entries directly (no recursion in // file mode — the user explicitly chose 「按文件」). const directFiles = ( await Promise.all( fileEntries.map( (e) => new Promise((resolve) => { (e as FsFileEntry).file( (file) => resolve(file), () => resolve(null), ); }), ), ) ).filter((f): f is File => f !== null); if (directFiles.length > 0) { setFiles((prev) => [...prev, ...directFiles]); } if (dirEntries.length > 0) { const folderNames = dirEntries.map((d) => d.name).join("、"); toast.info( directFiles.length > 0 ? t("documents.upload.ignoredFolders", { count: dirEntries.length, names: folderNames, }) : t("documents.upload.detectedFolders", { names: folderNames }), { description: t("documents.upload.detectedFoldersDesc"), action: { label: t("documents.upload.switchToFolder"), onClick: () => { setMode("folder"); // Auto-import the dropped folders so the user doesn't repeat // the gesture. void importDirEntries(dirEntries); }, }, }, ); // Auto-switch and import when the user dropped only folders. They // clearly intended a folder upload — don't make them click the toast. if (directFiles.length === 0) { setMode("folder"); await importDirEntries(dirEntries); } } }, [importDirEntries, t], ); /** * Folder-mode drop. Walks any dropped FileSystemDirectoryEntry tree to * produce a flat File[] with webkitRelativePath populated, so the existing * include/exclude/recursive filter pipeline works identically to a folder * picked through ``. */ const handleDropFolderMode = useCallback( async (event: React.DragEvent) => { event.preventDefault(); setIsDragOver(false); const entries = extractEntries(event.dataTransfer.items); if (entries.length === 0) { toast.error(t("documents.upload.folderDragUnsupported")); return; } const dirs = entries.filter((e) => e.isDirectory); const looseFiles = entries.filter((e) => e.isFile); if (dirs.length === 0 && looseFiles.length > 0) { toast.info(t("documents.upload.looseFilesMerged")); } const dirFiles = ( await Promise.all(dirs.map((d) => walkEntry(d))) ).flat(); const looseFileObjs = ( await Promise.all( looseFiles.map( (e) => new Promise((resolve) => { (e as FsFileEntry).file( (file) => resolve(tagRelativePath(file, file.name)), () => resolve(null), ); }), ), ) ).filter((f): f is File => f !== null); const merged = [...dirFiles, ...looseFileObjs]; if (merged.length > 0) { setFolderRaw((prev) => [...prev, ...merged]); } }, [t], ); const handleFileSelect = useCallback( (event: React.ChangeEvent) => { const selectedFiles = event.target.files; if (selectedFiles) { setFiles((prev) => [...prev, ...Array.from(selectedFiles)]); } }, [], ); const handleFolderSelect = useCallback( (event: React.ChangeEvent) => { const picked = event.target.files; if (picked) { setFolderRaw(Array.from(picked)); } // Allow re-selecting the same folder later. if (folderInputRef.current) folderInputRef.current.value = ""; }, [], ); const removeFile = useCallback((index: number) => { setFiles((prev) => prev.filter((_, fileIndex) => fileIndex !== index)); }, []); const resetForm = useCallback(() => { setMode("file"); setFiles([]); setFolderRaw([]); setOssSelection([]); setOssFilter(EMPTY_OSS_FILTER); setIncludeInput(""); setExcludeInput(""); setRecursive(true); setSubspace(defaultSubspace ?? ""); setMetadataJson(""); setMetadataValues({}); setJsonPreviewOpen(false); }, [defaultSubspace]); const handleModeChange = useCallback((next: string) => { const nextMode = next as UploadMode; setMode(nextMode); // Wipe other tabs' picks to avoid accidental submission of stale data. if (nextMode === "file") { setFolderRaw([]); setOssSelection([]); } else if (nextMode === "folder") { setFiles([]); setOssSelection([]); } else { setFiles([]); setFolderRaw([]); } }, []); const updateMetadataField = useCallback( (fieldName: string, value: string) => { setMetadataValues((prev) => ({ ...prev, [fieldName]: value })); }, [], ); // Build metadata preview JSON from form fields const metadataPreview = useMemo(() => { if (!hasSchemaFields) return null; const converted = convertMetadataValues(metadataValues, metadataFields!); return Object.keys(converted).length > 0 ? converted : null; }, [hasSchemaFields, metadataValues, metadataFields]); async function handleSubmit() { // ---- OSS branch — early return, leaves the local-files/folder branch below intact ---- if (mode === "oss") { const { fileKeys, folderPrefixes } = partitionSelection(ossSelection); if (fileKeys.length === 0 && folderPrefixes.length === 0) { toast.error(t("documents.upload.ossSelectRequired")); return; } let metadata: Record | undefined; if (hasSchemaFields) { const converted = convertMetadataValues( metadataValues, metadataFields!, ); if (Object.keys(converted).length > 0) metadata = converted; } else if (metadataJson.trim()) { try { metadata = JSON.parse(metadataJson) as Record; } catch { toast.error(t("documents.upload.metadataJsonInvalid")); return; } } try { // Translate the four free-text inputs into the server's glob form. // Filters only travel on folder items; the hook ignores them when // there is no folder selection, so the "filter applies to folder // selections only" semantic is enforced once, in one place. const { inclusionFilters, exclusionFilters } = buildFolderGlobs(ossFilter); const result = await ossImport.mutateAsync({ knowledgeBaseName, fileKeys, folderPrefixes, inclusionFilters, exclusionFilters, subspace: subspace || undefined, metadata, }); const details = result.documentDetails ?? []; const failed = details.filter( (d) => d.status.toLowerCase() === "failed", ); // Distinguish "filter excluded everything" from a successful import. if (details.length === 0) { toast.warning(t("documents.upload.noFilesImportedTitle"), { description: folderPrefixes.length > 0 ? t("documents.upload.noFilesImportedFiltered") : t("documents.upload.noFilesImportedEmpty"), }); resetForm(); onOpenChange(false); return; } if (failed.length === 0) { toast.success( t("documents.upload.importSuccess", { count: details.length }), ); } else if (failed.length < details.length) { const reasons = [ ...new Set(failed.map((d) => d.failureReason).filter(Boolean)), ]; toast.warning( t("documents.upload.importPartial", { ok: details.length - failed.length, fail: failed.length, }), { description: reasons.length > 0 ? reasons.slice(0, 3).join("; ") : undefined, }, ); } else { // All failed: show the first few distinct failureReasons so the user // can diagnose without digging into devtools. The most common cause // (until we surface it in the UI) is a server-side validation: e.g. // unsupported file type, invalid OSS URI, bucket-endpoint mismatch. const reasons = [ ...new Set(failed.map((d) => d.failureReason).filter(Boolean)), ]; toast.error( t("documents.upload.importAllFailed", { count: failed.length }), { description: reasons.length > 0 ? reasons.slice(0, 3).join("; ") : undefined, }, ); } resetForm(); onOpenChange(false); } catch (error) { showApiError(error, { fallbackTitle: t("documents.upload.ossImportFailed"), }); } return; } // ---- end OSS branch; the existing handleSubmit body continues below this point unchanged ---- if (effectiveFiles.length === 0) { toast.error( mode === "folder" ? t("documents.upload.noFolderOrFiltered") : t("documents.upload.selectAtLeastOneFile"), ); return; } let metadata: Record | undefined; if (hasSchemaFields) { const converted = convertMetadataValues(metadataValues, metadataFields!); if (Object.keys(converted).length > 0) { metadata = converted; } } else if (metadataJson.trim()) { try { metadata = JSON.parse(metadataJson); } catch { toast.error(t("documents.upload.metadataJsonInvalid")); return; } } try { const result = await uploadDocuments.mutateAsync({ knowledgeBaseName, files: effectiveFiles, subspace: subspace || undefined, metadata, }); // Classify per-file results when available. const details = result.documentDetails ?? []; const failed = details.filter((d) => d.status.toLowerCase() === "failed"); if (failed.length === 0) { toast.success( t("documents.upload.uploadSuccess", { count: effectiveFiles.length }), ); } else if (failed.length < details.length) { const reasons = [ ...new Set(failed.map((d) => d.failureReason).filter(Boolean)), ]; toast.warning( t("documents.upload.uploadPartial", { ok: details.length - failed.length, fail: failed.length, }), { description: reasons.length > 0 ? reasons.slice(0, 3).join("; ") : undefined, }, ); } else { toast.error( t("documents.upload.uploadAllFailed", { count: failed.length }), ); } // Also surface batch-level errors collected during upload. const batchErrors = uploadDocuments.progress?.errors ?? []; if (batchErrors.length > 0) { toast.warning( t("documents.upload.batchRequestErrors", { count: batchErrors.length, }), { description: batchErrors.slice(0, 2).join("; "), }, ); } resetForm(); onOpenChange(false); } catch (error) { const message = error instanceof Error ? error.message : t("documents.upload.uploadFailed"); toast.error(message); } } return ( {t("documents.upload.title")} {t("documents.upload.descriptionPrefix")} {knowledgeBaseName}
{t("documents.upload.tabFile")} {t("documents.upload.tabFolder")} {t("documents.upload.tabOss")} {/* ---- File mode ---- */}

{t("documents.upload.dropFileHere")}

{files.length > 0 && (
{t("documents.upload.selectedFilesPrefix")} {files.length} {t("documents.upload.selectedFilesSuffix")}
{files.map((file, index) => (
{file.name}
))}
)}
{/* ---- Folder mode ---- */}

{t("documents.upload.dropFolderHere")}

{/* `webkitdirectory` / `directory` are non-standard attributes and not modeled by React's InputHTMLAttributes; forward them via a spread with a loose type so TSX does not reject them. */} )} />
{t("documents.upload.filterOptions")}
setIncludeInput(e.target.value)} className="h-8 bg-background" />
setExcludeInput(e.target.value)} className="h-8 bg-background" />
{folderRaw.length > 0 && (
{t("documents.upload.folderSelectedPrefix")} {folderRaw.length} {t("documents.upload.folderSelectedMiddle")} {folderFiltered.length} {t("documents.upload.folderSelectedSuffix")}
{folderFiltered.length > 0 && (
{folderFiltered.slice(0, 50).map((file, index) => (
{file.webkitRelativePath || file.name}
))} {folderFiltered.length > 50 && (
{t("documents.upload.folderMoreHidden", { count: folderFiltered.length - 50, })}
)}
)}
)}
{/* ---- OSS mode ---- */}
{/* 子空间 */}
setSubspace(event.target.value)} /> {defaultSubspace && subspace !== defaultSubspace && (

{t("documents.upload.subspaceDiffHintPrefix")} {subspace || t("documents.upload.subspaceEmpty")} {t("documents.upload.subspaceDiffHintMiddle")} {defaultSubspace} {t("documents.upload.subspaceDiffHintSuffix")}

)}
{/* Metadata */} {hasSchemaFields && (
{metadataFields!.map((field) => (
updateMetadataField(field.name, next)} />
))}
{/* JSON 预览 */}
{jsonPreviewOpen && (
                    {metadataPreview
                      ? JSON.stringify(metadataPreview, null, 2)
                      : "{}"}
                  
)}
)}
{/* Upload progress bar */} {uploadDocuments.isPending && uploadDocuments.progress && (
{t("documents.upload.batchProgress", { completed: uploadDocuments.progress.completedBatches, total: uploadDocuments.progress.totalBatches, })} {t("documents.upload.filesProgress", { completed: uploadDocuments.progress.completedFiles, total: uploadDocuments.progress.totalFiles, })}
{uploadDocuments.progress.errors.length > 0 && (

{t("documents.upload.batchErrorCount", { count: uploadDocuments.progress.errors.length, })}

{uploadDocuments.progress.errors.map((err, i) => (

{err}

))}
)}
)}
); }