'use client'; import { useState, useRef } from "react"; import { Box, Button, Typography, Paper, Stack, TextField, MenuItem, IconButton, Divider, Grid, CircularProgress } from "@mui/material"; import CloudUploadIcon from "@mui/icons-material/CloudUpload"; import FolderIcon from "@mui/icons-material/Folder"; import CreateNewFolderIcon from "@mui/icons-material/CreateNewFolder"; import AssignmentIcon from '@mui/icons-material/Assignment'; import AddCircleOutlineIcon from '@mui/icons-material/AddCircleOutline'; import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline'; import { useRouter } from "next/navigation"; import { uploadFileAction, createFolderAction } from "./_actions"; interface MetadataPair { key: string; value: string; } export default function UploadView({ user, folders = [] }: any) { const router = useRouter(); const fileInputRef = useRef(null); const [file, setFile] = useState(null); const [description, setDescription] = useState(""); const [parentId, setParentId] = useState(""); const [status, setStatus] = useState<'idle' | 'uploading' | 'success'>('idle'); const [customMetadata, setCustomMetadata] = useState([]); // Folder Creation State const [showFolderInput, setShowFolderInput] = useState(false); const [newFolderName, setNewFolderName] = useState(""); const [isCreatingFolder, setIsCreatingFolder] = useState(false); const handleCreateFolder = async () => { if (!newFolderName.trim()) return; setIsCreatingFolder(true); try { // FIX: Pass the current parentId to the action so it nests correctly const result = await createFolderAction(newFolderName, parentId); if (result.success) { setNewFolderName(""); setShowFolderInput(false); router.refresh(); } } catch (err: any) { alert(err.message || "Failed to create folder"); } finally { setIsCreatingFolder(false); } }; const addMetadataRow = () => setCustomMetadata([...customMetadata, { key: "", value: "" }]); const removeMetadataRow = (index: number) => { setCustomMetadata(customMetadata.filter((_, i) => i !== index)); }; const updateMetadataRow = (index: number, field: 'key' | 'value', val: string) => { const updated = [...customMetadata]; updated[index][field] = val; setCustomMetadata(updated); }; const handleUpload = async () => { if (!file) return; setStatus('uploading'); const formData = new FormData(); formData.append("file", file); formData.append("description", description); formData.append("parentId", parentId); const metadataObj = customMetadata.reduce((acc, curr) => { if (curr.key.trim()) acc[curr.key.trim()] = curr.value; return acc; }, {} as Record); formData.append("customMetadata", JSON.stringify(metadataObj)); try { const result = await uploadFileAction(formData); if (result.success) { setStatus('success'); setFile(null); setDescription(""); setCustomMetadata([]); router.push("/dashboard"); router.refresh(); } } catch (err) { alert("Upload failed."); setStatus('idle'); } }; return ( Add to Library {/* 1. Destination */} 1. Destination setParentId(e.target.value)} size="small" slotProps={{ select: { displayEmpty: true }, inputLabel: { shrink: true }, }} > -- Root (Main Folder) -- {folders.map((f: any) => ( {f.name} ))} setShowFolderInput(!showFolderInput)} sx={{ border: '1px solid #ccc', borderRadius: 1 }} > {showFolderInput && ( {parentId ? `Create inside current selection` : `Create at Root`} setNewFolderName(e.target.value)} onKeyPress={(e) => e.key === 'Enter' && handleCreateFolder()} /> )} {/* 2. Upload Area */} 2. Upload File setFile(e.target.files?.[0] || null)} /> {/* 3. Custom Attributes */} 3. Custom Attributes {customMetadata.map((row, index) => ( updateMetadataRow(index, 'key', e.target.value)} /> updateMetadataRow(index, 'value', e.target.value)} /> removeMetadataRow(index)}> ))} setDescription(e.target.value)} /> ); }