|
| 1 | +import { useRef, useState } from 'react'; |
| 2 | +import { |
| 3 | + Box, Typography, IconButton, List, ListItem, ListItemText, ListItemSecondaryAction, |
| 4 | + useTheme, |
| 5 | +} from '@mui/material'; |
| 6 | +import UploadFileIcon from '@mui/icons-material/UploadFile'; |
| 7 | +import DeleteIcon from '@mui/icons-material/Delete'; |
| 8 | + |
| 9 | +interface StagedAttachmentsProps { |
| 10 | + files: File[]; |
| 11 | + onAdd: (files: File[]) => void; |
| 12 | + onRemove: (index: number) => void; |
| 13 | +} |
| 14 | + |
| 15 | +function formatSize(bytes: number): string { |
| 16 | + if (bytes < 1024) return `${bytes} B`; |
| 17 | + if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; |
| 18 | + return `${(bytes / 1024 / 1024).toFixed(1)} MB`; |
| 19 | +} |
| 20 | + |
| 21 | +export function StagedAttachments({ files, onAdd, onRemove }: StagedAttachmentsProps) { |
| 22 | + const { palette } = useTheme(); |
| 23 | + const inputRef = useRef<HTMLInputElement>(null); |
| 24 | + const [dragOver, setDragOver] = useState(false); |
| 25 | + |
| 26 | + const handleDrop = (e: React.DragEvent) => { |
| 27 | + e.preventDefault(); |
| 28 | + setDragOver(false); |
| 29 | + if (e.dataTransfer.files.length > 0) { |
| 30 | + onAdd(Array.from(e.dataTransfer.files)); |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + const handleFiles = (fileList: FileList | null) => { |
| 35 | + if (fileList && fileList.length > 0) { |
| 36 | + onAdd(Array.from(fileList)); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + return ( |
| 41 | + <Box> |
| 42 | + {files.length > 0 && ( |
| 43 | + <List dense disablePadding sx={{ mb: 1 }}> |
| 44 | + {files.map((file, i) => ( |
| 45 | + <ListItem key={i} disableGutters sx={{ py: 0.25 }}> |
| 46 | + <ListItemText |
| 47 | + primary={file.name} |
| 48 | + secondary={formatSize(file.size)} |
| 49 | + primaryTypographyProps={{ variant: 'body2', noWrap: true }} |
| 50 | + secondaryTypographyProps={{ variant: 'caption' }} |
| 51 | + /> |
| 52 | + <ListItemSecondaryAction> |
| 53 | + <IconButton size="small" onClick={() => onRemove(i)}> |
| 54 | + <DeleteIcon fontSize="small" /> |
| 55 | + </IconButton> |
| 56 | + </ListItemSecondaryAction> |
| 57 | + </ListItem> |
| 58 | + ))} |
| 59 | + </List> |
| 60 | + )} |
| 61 | + |
| 62 | + <Box |
| 63 | + onDragOver={e => { e.preventDefault(); setDragOver(true); }} |
| 64 | + onDragLeave={() => setDragOver(false)} |
| 65 | + onDrop={handleDrop} |
| 66 | + onClick={() => inputRef.current?.click()} |
| 67 | + sx={{ |
| 68 | + border: `2px dashed ${dragOver ? palette.primary.main : palette.divider}`, |
| 69 | + borderRadius: 1, |
| 70 | + p: 2, |
| 71 | + display: 'flex', |
| 72 | + alignItems: 'center', |
| 73 | + justifyContent: 'center', |
| 74 | + gap: 1, |
| 75 | + bgcolor: dragOver ? (palette.mode === 'dark' ? 'rgba(144,202,249,0.08)' : 'rgba(25,118,210,0.04)') : 'transparent', |
| 76 | + transition: 'all 0.2s', |
| 77 | + cursor: 'pointer', |
| 78 | + }} |
| 79 | + > |
| 80 | + <UploadFileIcon color="action" fontSize="small" /> |
| 81 | + <Typography variant="body2" color="text.secondary"> |
| 82 | + Drop files here or click to add |
| 83 | + </Typography> |
| 84 | + <input |
| 85 | + ref={inputRef} |
| 86 | + type="file" |
| 87 | + multiple |
| 88 | + hidden |
| 89 | + onChange={e => { handleFiles(e.target.files); e.target.value = ''; }} |
| 90 | + /> |
| 91 | + </Box> |
| 92 | + </Box> |
| 93 | + ); |
| 94 | +} |
0 commit comments