|
| 1 | +/* |
| 2 | + This is the FTP component for the config page. |
| 3 | +
|
| 4 | + It handles all FTP related operations. |
| 5 | +*/ |
| 6 | +// Base imports |
| 7 | + |
| 8 | +// 3rd party imports |
| 9 | + |
| 10 | +// Redux |
| 11 | +import { Button, Group, LoadingOverlay, Tree } from "@mantine/core" |
| 12 | +import { IconFile, IconFolder, IconFolderOpen } from "@tabler/icons-react" |
| 13 | +import { useEffect, useMemo } from "react" |
| 14 | +import { useDispatch, useSelector } from "react-redux" |
| 15 | +import { |
| 16 | + emitListFiles, |
| 17 | + resetFiles, |
| 18 | + selectFiles, |
| 19 | + selectLoadingListFiles, |
| 20 | +} from "../../redux/slices/ftpSlice" |
| 21 | + |
| 22 | +export default function Ftp() { |
| 23 | + const dispatch = useDispatch() |
| 24 | + const files = useSelector(selectFiles) |
| 25 | + const loadingListFiles = useSelector(selectLoadingListFiles) |
| 26 | + |
| 27 | + const convertedFiles = useMemo(() => { |
| 28 | + if (!files || files.length === 0) return [] |
| 29 | + return files.map((file) => { |
| 30 | + // Need to convert all children recursively |
| 31 | + |
| 32 | + const convertChildren = (children) => { |
| 33 | + if (!children) return undefined |
| 34 | + return children.map((child) => { |
| 35 | + return { |
| 36 | + value: child.path, |
| 37 | + label: child.name, |
| 38 | + children: convertChildren(child.children), |
| 39 | + path: child.path, |
| 40 | + is_dir: child.is_dir, |
| 41 | + } |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + return { |
| 46 | + value: file.path, |
| 47 | + label: file.name, |
| 48 | + children: convertChildren(file.children), |
| 49 | + path: file.path, |
| 50 | + is_dir: file.is_dir, |
| 51 | + } |
| 52 | + }) |
| 53 | + }, [files]) |
| 54 | + |
| 55 | + useEffect(() => { |
| 56 | + if (files.length === 0) { |
| 57 | + dispatch(emitListFiles({ path: "/" })) |
| 58 | + } |
| 59 | + }, [files.length, dispatch]) |
| 60 | + |
| 61 | + function handleFileClick(node) { |
| 62 | + if (node.is_dir) { |
| 63 | + if (node.children === undefined) { |
| 64 | + dispatch(emitListFiles({ path: node.path })) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return ( |
| 70 | + <div className="flex flex-col gap-4 mx-4 relative w-fit"> |
| 71 | + <LoadingOverlay |
| 72 | + visible={loadingListFiles} |
| 73 | + zIndex={1000} |
| 74 | + overlayProps={{ blur: 2 }} |
| 75 | + /> |
| 76 | + |
| 77 | + {loadingListFiles && <p>Loading files...</p>} |
| 78 | + |
| 79 | + <Button |
| 80 | + onClick={() => { |
| 81 | + dispatch(resetFiles()) |
| 82 | + }} |
| 83 | + w={"fit-content"} |
| 84 | + > |
| 85 | + Refresh files |
| 86 | + </Button> |
| 87 | + <Tree |
| 88 | + data={convertedFiles} |
| 89 | + renderNode={({ node, expanded, elementProps }) => ( |
| 90 | + <Group gap={5} {...elementProps} key={node.path}> |
| 91 | + {node.is_dir ? ( |
| 92 | + <> |
| 93 | + {expanded ? ( |
| 94 | + <IconFolderOpen size={20} /> |
| 95 | + ) : ( |
| 96 | + <IconFolder size={20} /> |
| 97 | + )} |
| 98 | + </> |
| 99 | + ) : ( |
| 100 | + <IconFile size={20} /> |
| 101 | + )} |
| 102 | + |
| 103 | + <span |
| 104 | + onClick={() => { |
| 105 | + handleFileClick(node) |
| 106 | + }} |
| 107 | + > |
| 108 | + {node.label} |
| 109 | + </span> |
| 110 | + </Group> |
| 111 | + )} |
| 112 | + /> |
| 113 | + </div> |
| 114 | + ) |
| 115 | +} |
0 commit comments