|
| 1 | +import type { TreeNode } from "./types"; |
| 2 | +import { formatLeafValue, isPreviewable } from "./utils"; |
| 3 | +import DownloadIcon from "@mui/icons-material/Download"; |
| 4 | +import ExpandLess from "@mui/icons-material/ExpandLess"; |
| 5 | +import ExpandMore from "@mui/icons-material/ExpandMore"; |
| 6 | +import FolderIcon from "@mui/icons-material/Folder"; |
| 7 | +import InsertDriveFileIcon from "@mui/icons-material/InsertDriveFile"; |
| 8 | +import VisibilityIcon from "@mui/icons-material/Visibility"; |
| 9 | +import { Box, Button, Collapse, Typography } from "@mui/material"; |
| 10 | +import React from "react"; |
| 11 | + |
| 12 | +type Props = { |
| 13 | + node: TreeNode; |
| 14 | + level: number; |
| 15 | + onPreview: (url: string, index: number) => void; |
| 16 | +}; |
| 17 | + |
| 18 | +const FileTreeRow: React.FC<Props> = ({ node, level, onPreview }) => { |
| 19 | + const [open, setOpen] = React.useState(false); |
| 20 | + |
| 21 | + if (node.kind === "folder") { |
| 22 | + return ( |
| 23 | + <> |
| 24 | + <Box |
| 25 | + sx={{ |
| 26 | + display: "flex", |
| 27 | + alignItems: "center", |
| 28 | + gap: 1, |
| 29 | + py: 0.5, |
| 30 | + px: 1, |
| 31 | + cursor: "pointer", |
| 32 | + "&:hover": { backgroundColor: "rgba(0,0,0,0.04)" }, |
| 33 | + }} |
| 34 | + onClick={() => setOpen((o) => !o)} |
| 35 | + > |
| 36 | + <Box sx={{ pl: level * 1.25 }}> |
| 37 | + <FolderIcon fontSize="small" /> |
| 38 | + </Box> |
| 39 | + <Typography sx={{ fontWeight: 600, flex: 1 }}>{node.name}</Typography> |
| 40 | + {open ? <ExpandLess /> : <ExpandMore />} |
| 41 | + </Box> |
| 42 | + |
| 43 | + <Collapse in={open} timeout="auto" unmountOnExit> |
| 44 | + {node.children.map((child) => ( |
| 45 | + <FileTreeRow |
| 46 | + key={child.path} |
| 47 | + node={child} |
| 48 | + level={level + 1} |
| 49 | + onPreview={onPreview} |
| 50 | + /> |
| 51 | + ))} |
| 52 | + </Collapse> |
| 53 | + </> |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + return ( |
| 58 | + <Box |
| 59 | + sx={{ display: "flex", alignItems: "flex-start", gap: 1, py: 0.5, px: 1 }} |
| 60 | + > |
| 61 | + <Box sx={{ pl: level * 1.25, pt: "2px" }}> |
| 62 | + <InsertDriveFileIcon fontSize="small" /> |
| 63 | + </Box> |
| 64 | + |
| 65 | + <Box sx={{ flex: 1, minWidth: 0, overflow: "hidden" }}> |
| 66 | + <Typography |
| 67 | + title={node.name} |
| 68 | + sx={{ |
| 69 | + fontWeight: 500, |
| 70 | + whiteSpace: "nowrap", |
| 71 | + overflow: "hidden", |
| 72 | + textOverflow: "ellipsis", |
| 73 | + }} |
| 74 | + > |
| 75 | + {node.name} |
| 76 | + </Typography> |
| 77 | + |
| 78 | + {!node.link && node.value !== undefined && ( |
| 79 | + <Typography |
| 80 | + title={ |
| 81 | + node.name === "_ArrayZipData_" |
| 82 | + ? "[compressed data]" |
| 83 | + : typeof node.value === "string" |
| 84 | + ? node.value |
| 85 | + : JSON.stringify(node.value) |
| 86 | + } |
| 87 | + sx={{ |
| 88 | + fontFamily: "monospace", |
| 89 | + fontSize: "0.85rem", |
| 90 | + color: "text.secondary", |
| 91 | + whiteSpace: "nowrap", |
| 92 | + overflow: "hidden", |
| 93 | + textOverflow: "ellipsis", |
| 94 | + mt: 0.25, |
| 95 | + }} |
| 96 | + > |
| 97 | + {node.name === "_ArrayZipData_" |
| 98 | + ? "[compressed data]" |
| 99 | + : formatLeafValue(node.value)} |
| 100 | + </Typography> |
| 101 | + )} |
| 102 | + </Box> |
| 103 | + |
| 104 | + {node.link?.url && ( |
| 105 | + <Box sx={{ display: "flex", gap: 1, flexShrink: 0 }}> |
| 106 | + <Button |
| 107 | + size="small" |
| 108 | + variant="text" |
| 109 | + onClick={() => window.open(node.link!.url, "_blank")} |
| 110 | + startIcon={<DownloadIcon fontSize="small" />} |
| 111 | + > |
| 112 | + Download |
| 113 | + </Button> |
| 114 | + {isPreviewable(node.link.url) && ( |
| 115 | + <Button |
| 116 | + size="small" |
| 117 | + variant="text" |
| 118 | + startIcon={<VisibilityIcon fontSize="small" />} |
| 119 | + onClick={() => onPreview(node.link!.url, node.link!.index)} |
| 120 | + > |
| 121 | + Preview |
| 122 | + </Button> |
| 123 | + )} |
| 124 | + </Box> |
| 125 | + )} |
| 126 | + </Box> |
| 127 | + ); |
| 128 | +}; |
| 129 | + |
| 130 | +export default FileTreeRow; |
0 commit comments