2026-01-08 05:41:31 +00:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { useState } from "react";
|
2026-01-09 06:58:44 +00:00
|
|
|
import {
|
|
|
|
|
Box, Button, Typography, Paper, LinearProgress, Stack,
|
|
|
|
|
TextField, MenuItem, IconButton, Tooltip, Divider
|
|
|
|
|
} from "@mui/material";
|
2026-01-08 05:41:31 +00:00
|
|
|
import CloudUploadIcon from "@mui/icons-material/CloudUpload";
|
2026-01-09 06:58:44 +00:00
|
|
|
import CreateNewFolderIcon from "@mui/icons-material/CreateNewFolder";
|
|
|
|
|
import { uploadFileAction, createFolderAction } from "./_actions";
|
2026-01-08 05:41:31 +00:00
|
|
|
|
|
|
|
|
/**
|
2026-01-09 06:58:44 +00:00
|
|
|
* Format bytes to human readable string (MiB/KiB)
|
2026-01-08 05:41:31 +00:00
|
|
|
*/
|
|
|
|
|
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];
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-09 06:58:44 +00:00
|
|
|
export default function UploadView({ user, folders = [] }: { user: any, folders?: any[] }) {
|
2026-01-08 05:41:31 +00:00
|
|
|
const [file, setFile] = useState<File | null>(null);
|
2026-01-09 06:58:44 +00:00
|
|
|
const [description, setDescription] = useState("");
|
|
|
|
|
const [parentId, setParentId] = useState("");
|
2026-01-08 05:41:31 +00:00
|
|
|
const [status, setStatus] = useState<'idle' | 'uploading' | 'success'>('idle');
|
|
|
|
|
|
2026-01-09 06:58:44 +00:00
|
|
|
// 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");
|
|
|
|
|
}
|
|
|
|
|
};
|
2026-01-08 05:41:31 +00:00
|
|
|
|
|
|
|
|
const handleUpload = async () => {
|
|
|
|
|
if (!file) return;
|
2026-01-09 06:58:44 +00:00
|
|
|
|
|
|
|
|
// RESTORED: Client-side size validation
|
2026-01-08 05:41:31 +00:00
|
|
|
if (file.size > MAX_FILE_SIZE) {
|
2026-01-09 06:58:44 +00:00
|
|
|
alert(`File is too large! Maximum size allowed is ${formatFileSize(MAX_FILE_SIZE)}.`);
|
|
|
|
|
return;
|
2026-01-08 05:41:31 +00:00
|
|
|
}
|
2026-01-09 06:58:44 +00:00
|
|
|
|
2026-01-08 05:41:31 +00:00
|
|
|
setStatus('uploading');
|
|
|
|
|
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append("file", file);
|
2026-01-09 06:58:44 +00:00
|
|
|
formData.append("description", description);
|
|
|
|
|
formData.append("parentId", parentId);
|
2026-01-08 05:41:31 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await uploadFileAction(formData);
|
|
|
|
|
setStatus('success');
|
|
|
|
|
setFile(null);
|
2026-01-09 06:58:44 +00:00
|
|
|
setDescription("");
|
2026-01-08 05:41:31 +00:00
|
|
|
} catch (err) {
|
2026-01-09 06:58:44 +00:00
|
|
|
console.error(err);
|
|
|
|
|
alert("Upload failed. Ensure file size is within limits and check server logs.");
|
2026-01-08 05:41:31 +00:00
|
|
|
setStatus('idle');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2026-01-09 06:58:44 +00:00
|
|
|
<Paper sx={{ p: { xs: 3, md: 6 }, borderRadius: 4 }} elevation={3}>
|
|
|
|
|
<Typography variant="h4" fontWeight={900} color="primary" gutterBottom align="center">
|
|
|
|
|
Add to Library
|
2026-01-08 05:41:31 +00:00
|
|
|
</Typography>
|
|
|
|
|
|
2026-01-09 06:58:44 +00:00
|
|
|
<Stack spacing={4} sx={{ mt: 4 }}>
|
|
|
|
|
|
|
|
|
|
{/* Section 1: Folder Selection */}
|
|
|
|
|
<Box>
|
|
|
|
|
<Typography variant="subtitle2" gutterBottom fontWeight="bold">
|
|
|
|
|
1. Select Target Project / Folder
|
|
|
|
|
</Typography>
|
|
|
|
|
<Stack direction="row" spacing={1}>
|
|
|
|
|
<TextField
|
|
|
|
|
select
|
|
|
|
|
fullWidth
|
|
|
|
|
label="Choose Folder"
|
|
|
|
|
value={parentId}
|
|
|
|
|
onChange={(e) => setParentId(e.target.value)}
|
|
|
|
|
disabled={status === 'uploading'}
|
|
|
|
|
>
|
|
|
|
|
<MenuItem value=""><em>None (Root)</em></MenuItem>
|
|
|
|
|
{folders.map((f) => (
|
|
|
|
|
<MenuItem key={f.id} value={f.id}>{f.name}</MenuItem>
|
|
|
|
|
))}
|
|
|
|
|
</TextField>
|
|
|
|
|
<Tooltip title="Create New Folder">
|
|
|
|
|
<IconButton
|
|
|
|
|
color="primary"
|
|
|
|
|
onClick={() => setShowFolderInput(!showFolderInput)}
|
|
|
|
|
sx={{ border: '1px solid', borderColor: 'divider', borderRadius: 1 }}
|
|
|
|
|
>
|
|
|
|
|
<CreateNewFolderIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
</Stack>
|
2026-01-08 05:41:31 +00:00
|
|
|
|
2026-01-09 06:58:44 +00:00
|
|
|
{showFolderInput && (
|
|
|
|
|
<Stack direction="row" spacing={1} sx={{ mt: 2, p: 2, bgcolor: 'grey.50', borderRadius: 2 }}>
|
|
|
|
|
<TextField
|
|
|
|
|
size="small"
|
|
|
|
|
fullWidth
|
|
|
|
|
placeholder="New folder name..."
|
|
|
|
|
value={newFolderName}
|
|
|
|
|
onChange={(e) => setNewFolderName(e.target.value)}
|
|
|
|
|
autoFocus
|
|
|
|
|
/>
|
|
|
|
|
<Button variant="contained" onClick={handleCreateFolder}>Create</Button>
|
|
|
|
|
</Stack>
|
2026-01-08 05:41:31 +00:00
|
|
|
)}
|
|
|
|
|
</Box>
|
|
|
|
|
|
2026-01-09 06:58:44 +00:00
|
|
|
<Divider />
|
|
|
|
|
|
|
|
|
|
{/* Section 2: File Selection */}
|
|
|
|
|
<Box>
|
|
|
|
|
<Typography variant="subtitle2" gutterBottom fontWeight="bold">
|
|
|
|
|
2. Upload File
|
|
|
|
|
</Typography>
|
|
|
|
|
<Box sx={{ p: 4, border: '2px dashed #ccc', borderRadius: 2, textAlign: 'center', bgcolor: '#fcfcfc' }}>
|
|
|
|
|
<input
|
|
|
|
|
type="file" id="file-input" hidden
|
|
|
|
|
accept=".pdf,.epub,.mobi,.azw3,.txt,.png,.jpeg"
|
|
|
|
|
onChange={(e) => {
|
|
|
|
|
setFile(e.target.files?.[0] || null);
|
|
|
|
|
setStatus('idle');
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<label htmlFor="file-input">
|
|
|
|
|
<Button variant="outlined" component="span" startIcon={<CloudUploadIcon />}>
|
|
|
|
|
{file ? "Change File" : "Choose Book/Image"}
|
|
|
|
|
</Button>
|
|
|
|
|
</label>
|
|
|
|
|
{file && (
|
|
|
|
|
<Box mt={2}>
|
|
|
|
|
<Typography variant="body2" sx={{ color: 'text.secondary' }}>
|
|
|
|
|
Selected: <strong>{file.name}</strong>
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography variant="caption" sx={{ color: file.size > MAX_FILE_SIZE ? 'error.main' : 'text.disabled' }}>
|
|
|
|
|
Size: {formatFileSize(file.size)} {file.size > MAX_FILE_SIZE && "(Too Large)"}
|
|
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
|
|
|
|
|
{/* Section 3: Description */}
|
|
|
|
|
<TextField
|
|
|
|
|
label="Notes / Description"
|
|
|
|
|
multiline rows={2} fullWidth
|
|
|
|
|
value={description}
|
|
|
|
|
onChange={(e) => setDescription(e.target.value)}
|
|
|
|
|
disabled={status === 'uploading'}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{/* Status & Action */}
|
2026-01-08 05:41:31 +00:00
|
|
|
{status === 'uploading' && (
|
2026-01-09 06:58:44 +00:00
|
|
|
<Box>
|
|
|
|
|
<LinearProgress sx={{ borderRadius: 5, height: 10 }} />
|
|
|
|
|
<Typography variant="caption" color="primary" sx={{ mt: 1, display: 'block', textAlign: 'center' }}>
|
|
|
|
|
Streaming to OneDrive storage...
|
2026-01-08 05:41:31 +00:00
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<Button
|
2026-01-09 06:58:44 +00:00
|
|
|
variant="contained" size="large" fullWidth
|
|
|
|
|
disabled={!file || status === 'uploading' || file.size > MAX_FILE_SIZE}
|
2026-01-08 05:41:31 +00:00
|
|
|
onClick={handleUpload}
|
2026-01-09 06:58:44 +00:00
|
|
|
sx={{ py: 2, fontWeight: 'bold' }}
|
2026-01-08 05:41:31 +00:00
|
|
|
>
|
2026-01-09 06:58:44 +00:00
|
|
|
{status === 'uploading' ? 'Uploading...' : 'Confirm Upload'}
|
2026-01-08 05:41:31 +00:00
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
{status === 'success' && (
|
2026-01-09 06:58:44 +00:00
|
|
|
<Typography align="center" color="success.main" fontWeight="bold">
|
|
|
|
|
✅ Successfully Added!
|
2026-01-08 05:41:31 +00:00
|
|
|
</Typography>
|
|
|
|
|
)}
|
|
|
|
|
</Stack>
|
|
|
|
|
</Paper>
|
|
|
|
|
);
|
|
|
|
|
}
|