"use client"; import { useAtom } from "jotai"; import { CheckCircle2, FileType, Info, Upload, X } from "lucide-react"; import { AnimatePresence, motion } from "motion/react"; import { useTranslations } from "next-intl"; import { useCallback, useMemo, useRef, useState } from "react"; import { useDropzone } from "react-dropzone"; import { toast } from "sonner"; import { uploadDocumentMutationAtom } from "@/atoms/documents/document-mutation.atoms"; import { Accordion, AccordionContent, AccordionItem, AccordionTrigger, } from "@/components/ui/accordion"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Progress } from "@/components/ui/progress"; import { Separator } from "@/components/ui/separator"; import { Spinner } from "@/components/ui/spinner"; import { trackDocumentUploadFailure, trackDocumentUploadStarted, trackDocumentUploadSuccess, } from "@/lib/posthog/events"; import { GridPattern } from "./GridPattern"; interface DocumentUploadTabProps { searchSpaceId: string; onSuccess?: () => void; onAccordionStateChange?: (isExpanded: boolean) => void; } const audioFileTypes = { "audio/mpeg": [".mp3", ".mpeg", ".mpga"], "audio/mp4": [".mp4", ".m4a"], "audio/wav": [".wav"], "audio/webm": [".webm"], "text/markdown": [".md", ".markdown"], "text/plain": [".txt"], }; const commonTypes = { "application/pdf": [".pdf"], "application/vnd.openxmlformats-officedocument.wordprocessingml.document": [".docx"], "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": [".xlsx"], "application/vnd.openxmlformats-officedocument.presentationml.presentation": [".pptx"], "text/html": [".html", ".htm"], "text/csv": [".csv"], "image/jpeg": [".jpg", ".jpeg"], "image/png": [".png"], "image/bmp": [".bmp"], "image/webp": [".webp"], "image/tiff": [".tiff"], }; const FILE_TYPE_CONFIG: Record> = { LLAMACLOUD: { ...commonTypes, "application/msword": [".doc"], "application/vnd.ms-word.document.macroEnabled.12": [".docm"], "application/msword-template": [".dot"], "application/vnd.ms-word.template.macroEnabled.12": [".dotm"], "application/vnd.ms-powerpoint": [".ppt"], "application/vnd.ms-powerpoint.template.macroEnabled.12": [".pptm"], "application/vnd.ms-powerpoint.template": [".pot"], "application/vnd.openxmlformats-officedocument.presentationml.template": [".potx"], "application/vnd.ms-excel": [".xls"], "application/vnd.ms-excel.sheet.macroEnabled.12": [".xlsm"], "application/vnd.ms-excel.sheet.binary.macroEnabled.12": [".xlsb"], "application/vnd.ms-excel.workspace": [".xlw"], "application/rtf": [".rtf"], "application/xml": [".xml"], "application/epub+zip": [".epub"], "text/tab-separated-values": [".tsv"], "text/html": [".html", ".htm", ".web"], "image/gif": [".gif"], "image/svg+xml": [".svg"], ...audioFileTypes, }, DOCLING: { ...commonTypes, "text/asciidoc": [".adoc", ".asciidoc"], "text/html": [".html", ".htm", ".xhtml"], "image/tiff": [".tiff", ".tif"], ...audioFileTypes, }, default: { ...commonTypes, "application/msword": [".doc"], "message/rfc822": [".eml"], "application/epub+zip": [".epub"], "image/heic": [".heic"], "application/vnd.ms-outlook": [".msg"], "application/vnd.oasis.opendocument.text": [".odt"], "text/x-org": [".org"], "application/pkcs7-signature": [".p7s"], "application/vnd.ms-powerpoint": [".ppt"], "text/x-rst": [".rst"], "application/rtf": [".rtf"], "text/tab-separated-values": [".tsv"], "application/vnd.ms-excel": [".xls"], "application/xml": [".xml"], ...audioFileTypes, }, }; const cardClass = "border border-border bg-slate-400/5 dark:bg-white/5"; // Upload limits const MAX_FILES = 10; const MAX_TOTAL_SIZE_MB = 200; const MAX_TOTAL_SIZE_BYTES = MAX_TOTAL_SIZE_MB * 1024 * 1024; export function DocumentUploadTab({ searchSpaceId, onSuccess, onAccordionStateChange, }: DocumentUploadTabProps) { const t = useTranslations("upload_documents"); const [files, setFiles] = useState([]); const [uploadProgress, setUploadProgress] = useState(0); const [accordionValue, setAccordionValue] = useState(""); const [uploadDocumentMutation] = useAtom(uploadDocumentMutationAtom); const { mutate: uploadDocuments, isPending: isUploading } = uploadDocumentMutation; const fileInputRef = useRef(null); const acceptedFileTypes = useMemo(() => { const etlService = process.env.NEXT_PUBLIC_ETL_SERVICE; return FILE_TYPE_CONFIG[etlService || "default"] || FILE_TYPE_CONFIG.default; }, []); const supportedExtensions = useMemo( () => Array.from(new Set(Object.values(acceptedFileTypes).flat())).sort(), [acceptedFileTypes] ); const onDrop = useCallback( (acceptedFiles: File[]) => { setFiles((prev) => { const newFiles = [...prev, ...acceptedFiles]; // Check file count limit if (newFiles.length > MAX_FILES) { toast.error(t("max_files_exceeded"), { description: t("max_files_exceeded_desc", { max: MAX_FILES }), }); return prev; } // Check total size limit const newTotalSize = newFiles.reduce((sum, file) => sum + file.size, 0); if (newTotalSize > MAX_TOTAL_SIZE_BYTES) { toast.error(t("max_size_exceeded"), { description: t("max_size_exceeded_desc", { max: MAX_TOTAL_SIZE_MB }), }); return prev; } return newFiles; }); }, [t] ); const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop, accept: acceptedFileTypes, maxSize: 50 * 1024 * 1024, // 50MB per file noClick: false, disabled: files.length >= MAX_FILES, }); // Handle file input click to prevent event bubbling that might reopen dialog const handleFileInputClick = useCallback((e: React.MouseEvent) => { e.stopPropagation(); }, []); const formatFileSize = (bytes: number) => { if (bytes === 0) return "0 Bytes"; const k = 1024; const sizes = ["Bytes", "KB", "MB", "GB", "TB"]; const i = Math.floor(Math.log(bytes) / Math.log(k)); return `${parseFloat((bytes / k ** i).toFixed(2))} ${sizes[i]}`; }; const totalFileSize = files.reduce((total, file) => total + file.size, 0); // Check if limits are reached const isFileCountLimitReached = files.length >= MAX_FILES; const isSizeLimitReached = totalFileSize >= MAX_TOTAL_SIZE_BYTES; const remainingFiles = MAX_FILES - files.length; const remainingSizeMB = Math.max( 0, (MAX_TOTAL_SIZE_BYTES - totalFileSize) / (1024 * 1024) ).toFixed(1); // Track accordion state changes const handleAccordionChange = useCallback( (value: string) => { setAccordionValue(value); onAccordionStateChange?.(value === "supported-file-types"); }, [onAccordionStateChange] ); const handleUpload = async () => { setUploadProgress(0); trackDocumentUploadStarted(Number(searchSpaceId), files.length, totalFileSize); const progressInterval = setInterval(() => { setUploadProgress((prev) => (prev >= 90 ? prev : prev + Math.random() * 10)); }, 200); uploadDocuments( { files, search_space_id: Number(searchSpaceId) }, { onSuccess: () => { clearInterval(progressInterval); setUploadProgress(100); trackDocumentUploadSuccess(Number(searchSpaceId), files.length); toast(t("upload_initiated"), { description: t("upload_initiated_desc") }); onSuccess?.(); }, onError: (error: unknown) => { clearInterval(progressInterval); setUploadProgress(0); const message = error instanceof Error ? error.message : "Upload failed"; trackDocumentUploadFailure(Number(searchSpaceId), message); toast(t("upload_error"), { description: `${t("upload_error_desc")}: ${message}`, }); }, } ); }; return ( {t("file_size_limit")}{" "} {t("upload_limits", { maxFiles: MAX_FILES, maxSizeMB: MAX_TOTAL_SIZE_MB })}
{isFileCountLimitReached ? (

{t("file_limit_reached")}

{t("file_limit_reached_desc", { max: MAX_FILES })}

) : isDragActive ? (

{t("drop_files")}

) : (

{t("drag_drop")}

{t("or_browse")}

{files.length > 0 && (

{t("remaining_capacity", { files: remainingFiles, sizeMB: remainingSizeMB })}

)}
)} {!isFileCountLimitReached && (
)}
{files.length > 0 && (
{t("selected_files", { count: files.length })} {t("total_size")}: {formatFileSize(totalFileSize)}
{files.map((file, index) => (

{file.name}

{formatFileSize(file.size)} {file.type || "Unknown type"}
))}
{isUploading && (
{t("uploading_files")} {Math.round(uploadProgress)}%
)}
)}
{t("supported_file_types")}
{t("file_types_desc")}
{supportedExtensions.map((ext) => ( {ext} ))}
); }