'use client'; import { useState } from "react"; import { Box, Button, Typography, Paper, LinearProgress, Stack } from "@mui/material"; import CloudUploadIcon from "@mui/icons-material/CloudUpload"; import { uploadFileAction } from "./_actions"; /** * Helper function to convert raw bytes into a human-readable string. * This helps the user understand exactly how large their e-book is. */ 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)); // Returns something like "1.45 MB" or "850 KB" return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; }; export default function UploadView({ user }: { user: any }) { const [file, setFile] = useState(null); const [status, setStatus] = useState<'idle' | 'uploading' | 'success'>('idle'); const MAX_FILE_SIZE = 50 * 1024 * 1024; // 50MB Limit const handleUpload = async () => { if (!file) return; 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); try { await uploadFileAction(formData); setStatus('success'); setFile(null); } catch (err) { // If the server action fails, it usually prints details in the terminal alert("Upload failed. Ensure the 'WebCalibre' folder can be created and OneDrive has space."); setStatus('idle'); } }; return ( Upload to WebCalibre Adding books as {user.name} {/* Dropzone/Selection Area */} { setFile(e.target.files?.[0] || null); setStatus('idle'); }} /> {/* New: Enhanced File Info Display */} {file && ( Selected: {file.name} File Size: {formatFileSize(file.size)} )} {/* Progress Indicator */} {status === 'uploading' && ( Connecting to OneDrive & Uploading... )} {/* Action Button */} {/* Success Feedback */} {status === 'success' && ( ✅ Successfully uploaded to your library! )} ); }