|
| 1 | +// src/components/common/attachment/AttachmentList.jsx |
| 2 | +import React from "react"; |
| 3 | +import { |
| 4 | + Box, |
| 5 | + Stack, |
| 6 | + Typography, |
| 7 | + IconButton, |
| 8 | + Tooltip, |
| 9 | + Alert, |
| 10 | + Divider, |
| 11 | +} from "@mui/material"; |
| 12 | +import ZoomInIcon from "@mui/icons-material/ZoomIn"; |
| 13 | +import DownloadIcon from "@mui/icons-material/Download"; |
| 14 | +import ImageIcon from "@mui/icons-material/Image"; |
| 15 | +import InsertDriveFileIcon from "@mui/icons-material/InsertDriveFile"; |
| 16 | +import { |
| 17 | + PictureAsPdf, |
| 18 | + Description, |
| 19 | + TableChart, |
| 20 | + Archive, |
| 21 | + Code, |
| 22 | + Movie, |
| 23 | + MusicNote, |
| 24 | +} from "@mui/icons-material"; |
| 25 | + |
| 26 | +function formatFileSize(bytes) { |
| 27 | + if (bytes === 0) return "0 Bytes"; |
| 28 | + const k = 1024; |
| 29 | + const sizes = ["Bytes", "KB", "MB", "GB"]; |
| 30 | + const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| 31 | + return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i]; |
| 32 | +} |
| 33 | + |
| 34 | +function getFileIcon(fileName, fileType) { |
| 35 | + const ext = fileName.toLowerCase().split(".").pop(); |
| 36 | + if (fileType.startsWith("image/")) return <ImageIcon color="primary" />; |
| 37 | + if (["pdf"].includes(ext)) return <PictureAsPdf color="error" />; |
| 38 | + if (["doc", "docx", "txt", "rtf"].includes(ext)) return <Description />; |
| 39 | + if (["xls", "xlsx", "csv"].includes(ext)) |
| 40 | + return <TableChart color="success" />; |
| 41 | + if (["zip", "rar", "7z", "tar", "gz"].includes(ext)) |
| 42 | + return <Archive color="warning" />; |
| 43 | + if (["js", "ts", "jsx", "tsx", "html", "css", "json", "xml"].includes(ext)) |
| 44 | + return <Code color="info" />; |
| 45 | + if (["mp4", "avi", "mov", "wmv", "flv", "mkv"].includes(ext)) |
| 46 | + return <Movie color="secondary" />; |
| 47 | + if (["mp3", "wav", "flac", "aac", "ogg"].includes(ext)) |
| 48 | + return <MusicNote color="secondary" />; |
| 49 | + return <InsertDriveFileIcon color="action" />; |
| 50 | +} |
| 51 | + |
| 52 | +export default function FileAttachmentViewer({ |
| 53 | + attachments = [], |
| 54 | + loading = false, |
| 55 | + error = null, |
| 56 | + onPreview, |
| 57 | + onDownload, |
| 58 | +}) { |
| 59 | + if (!attachments.length && !loading) return null; |
| 60 | + |
| 61 | + return ( |
| 62 | + <Box> |
| 63 | + <Stack direction="row" alignItems="center" spacing={1} mb={2}> |
| 64 | + <Typography variant="h6" fontWeight={600}> |
| 65 | + 첨부파일 ({attachments.length}개) |
| 66 | + {loading && " (로딩 중...)"} |
| 67 | + </Typography> |
| 68 | + </Stack> |
| 69 | + <Divider sx={{ mb: 3 }} /> |
| 70 | + {error && ( |
| 71 | + <Alert severity="error" sx={{ mb: 2 }}> |
| 72 | + 파일 로드 실패: {error} |
| 73 | + </Alert> |
| 74 | + )} |
| 75 | + <Stack spacing={1}> |
| 76 | + {attachments.map((file) => ( |
| 77 | + <Box |
| 78 | + key={file.id} |
| 79 | + sx={{ |
| 80 | + p: 2, |
| 81 | + border: "1px solid", |
| 82 | + borderColor: "grey.300", |
| 83 | + borderRadius: 1, |
| 84 | + bgcolor: "background.paper", |
| 85 | + "&:hover": { |
| 86 | + borderColor: "primary.main", |
| 87 | + bgcolor: "grey.50", |
| 88 | + }, |
| 89 | + }} |
| 90 | + > |
| 91 | + <Stack direction="row" alignItems="center" spacing={2}> |
| 92 | + <Box |
| 93 | + sx={{ |
| 94 | + width: 40, |
| 95 | + height: 40, |
| 96 | + display: "flex", |
| 97 | + alignItems: "center", |
| 98 | + justifyContent: "center", |
| 99 | + bgcolor: "grey.100", |
| 100 | + borderRadius: 1, |
| 101 | + }} |
| 102 | + > |
| 103 | + {getFileIcon(file.fileName, file.fileType)} |
| 104 | + </Box> |
| 105 | + <Box sx={{ flex: 1, minWidth: 0 }}> |
| 106 | + <Typography variant="body2" fontWeight={600} noWrap> |
| 107 | + {file.fileName} |
| 108 | + </Typography> |
| 109 | + <Typography variant="caption" color="text.secondary"> |
| 110 | + {formatFileSize(file.fileSize)} |
| 111 | + {file.fileType && ` • ${file.fileType}`} |
| 112 | + </Typography> |
| 113 | + </Box> |
| 114 | + <Stack direction="row" spacing={1}> |
| 115 | + {file.isImage && file.imageUrl && onPreview && ( |
| 116 | + <Tooltip title="미리보기"> |
| 117 | + <IconButton size="small" onClick={() => onPreview(file)}> |
| 118 | + <ZoomInIcon /> |
| 119 | + </IconButton> |
| 120 | + </Tooltip> |
| 121 | + )} |
| 122 | + {onDownload && ( |
| 123 | + <Tooltip title="다운로드"> |
| 124 | + <IconButton size="small" onClick={() => onDownload(file)}> |
| 125 | + <DownloadIcon /> |
| 126 | + </IconButton> |
| 127 | + </Tooltip> |
| 128 | + )} |
| 129 | + </Stack> |
| 130 | + </Stack> |
| 131 | + {file.error && ( |
| 132 | + <Alert severity="error" sx={{ mt: 1 }}> |
| 133 | + 파일 로드 실패: {file.error} |
| 134 | + </Alert> |
| 135 | + )} |
| 136 | + </Box> |
| 137 | + ))} |
| 138 | + </Stack> |
| 139 | + </Box> |
| 140 | + ); |
| 141 | +} |
0 commit comments