'use client'; import { useState } from "react"; import { Box, Button, Typography, Paper, LinearProgress, Stack, TextField, MenuItem, IconButton, Tooltip, Divider } from "@mui/material"; import CloudUploadIcon from "@mui/icons-material/CloudUpload"; import CreateNewFolderIcon from "@mui/icons-material/CreateNewFolder"; import { uploadFileAction, createFolderAction } from "./_actions"; /** * Format bytes to human readable string (MiB/KiB) */ const formatFileSize = (bytes: number) => { if (bytes === 0) return '0 Bytes'; const k = 1024; const sizes = ['Bytes', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; }; export default function UploadView({ user, folders = [] }: { user: any, folders?: any[] }) { const [file, setFile] = useState(null); const [description, setDescription] = useState(""); const [parentId, setParentId] = useState(""); const [status, setStatus] = useState<'idle' | 'uploading' | 'success'>('idle'); // Folder Creation State const [showFolderInput, setShowFolderInput] = useState(false); const [newFolderName, setNewFolderName] = useState(""); // --- RESTORED FILE SIZE LIMITS --- const MAX_FILE_SIZE = 150 * 1024 * 1024; // 150MiB Limit const handleCreateFolder = async () => { if (!newFolderName.trim()) return; try { await createFolderAction(newFolderName); setNewFolderName(""); setShowFolderInput(false); } catch (err) { alert("Error creating folder"); } }; const handleUpload = async () => { if (!file) return; // RESTORED: Client-side size validation if (file.size > MAX_FILE_SIZE) { alert(`File is too large! Maximum size allowed is ${formatFileSize(MAX_FILE_SIZE)}.`); return; } setStatus('uploading'); const formData = new FormData(); formData.append("file", file); formData.append("description", description); formData.append("parentId", parentId); try { await uploadFileAction(formData); setStatus('success'); setFile(null); setDescription(""); } catch (err) { console.error(err); alert("Upload failed. Ensure file size is within limits and check server logs."); setStatus('idle'); } }; return ( Add to Library {/* Section 1: Folder Selection */} 1. Select Target Project / Folder setParentId(e.target.value)} disabled={status === 'uploading'} > None (Root) {folders.map((f) => ( {f.name} ))} setShowFolderInput(!showFolderInput)} sx={{ border: '1px solid', borderColor: 'divider', borderRadius: 1 }} > {showFolderInput && ( setNewFolderName(e.target.value)} autoFocus /> )} {/* Section 2: File Selection */} 2. Upload File { setFile(e.target.files?.[0] || null); setStatus('idle'); }} /> {file && ( Selected: {file.name} MAX_FILE_SIZE ? 'error.main' : 'text.disabled' }}> Size: {formatFileSize(file.size)} {file.size > MAX_FILE_SIZE && "(Too Large)"} )} {/* Section 3: Description */} setDescription(e.target.value)} disabled={status === 'uploading'} /> {/* Status & Action */} {status === 'uploading' && ( Streaming to OneDrive storage... )} {status === 'success' && ( ✅ Successfully Added! )} ); }