|
| 1 | +import { Button } from "@mui/material"; |
| 2 | +import { useDispatch } from "react-redux"; |
| 3 | +import React, { useEffect } from "react"; |
| 4 | +import { setDirectories } from "../../lib/state/reducer"; |
| 5 | +import { openFolder } from "../../lib/utils/fileSystem"; |
| 6 | +import { connect } from "react-redux"; |
| 7 | +import TreeView from "@mui/lab/TreeView"; |
| 8 | +import ExpandMoreIcon from "@mui/icons-material/ExpandMore"; |
| 9 | +import ChevronRightIcon from "@mui/icons-material/ChevronRight"; |
| 10 | +import TreeItem, { useTreeItem } from "@mui/lab/TreeItem"; |
| 11 | + |
| 12 | +interface PanelProps { |
| 13 | + state: any; |
| 14 | +} |
| 15 | + |
| 16 | +const RecursiveComponent = ({ name, items }) => { |
| 17 | + console.log(items); |
| 18 | + const hasChildren = items && items; |
| 19 | + console.log(hasChildren) |
| 20 | + |
| 21 | + return ( |
| 22 | + <> |
| 23 | + <TreeItem nodeId={name} label={name} className="mt-1" key={name}> |
| 24 | + {hasChildren && |
| 25 | + items.map((item) => <RecursiveComponent key={item.name} {...item} />)} |
| 26 | + </TreeItem> |
| 27 | + </> |
| 28 | + ); |
| 29 | +}; |
| 30 | + |
| 31 | +export const Explorer: React.FC<PanelProps> = ({ state }) => { |
| 32 | + const dispatch = useDispatch(); |
| 33 | + async function onFolderSelect() { |
| 34 | + const folders = await openFolder(); |
| 35 | + dispatch(setDirectories(folders)); |
| 36 | + } |
| 37 | + return ( |
| 38 | + <div className="mt-5 px-4 text-sm"> |
| 39 | + {state && Object.keys(state).length ? ( |
| 40 | + <TreeView |
| 41 | + aria-label="file system navigator" |
| 42 | + defaultCollapseIcon={<ExpandMoreIcon />} |
| 43 | + defaultExpandIcon={<ChevronRightIcon />} |
| 44 | + sx={{ flexGrow: 1, maxWidth: 400, overflowY: "auto" }} |
| 45 | + > |
| 46 | + <RecursiveComponent {...state} /> |
| 47 | + </TreeView> |
| 48 | + ) : ( |
| 49 | + <div> |
| 50 | + <p>You have not yet added a folder to the workspace.</p> |
| 51 | + <Button className="mt-4" onClick={onFolderSelect}> |
| 52 | + Open Folder |
| 53 | + </Button> |
| 54 | + </div> |
| 55 | + )} |
| 56 | + </div> |
| 57 | + ); |
| 58 | +}; |
| 59 | + |
| 60 | +interface State { |
| 61 | + state: Object; |
| 62 | + app: any; |
| 63 | +} |
| 64 | +function mapStateToProps(state: State) { |
| 65 | + return { |
| 66 | + state: state.app.directories, |
| 67 | + }; |
| 68 | +} |
| 69 | +export default connect(mapStateToProps, null)(Explorer); |
0 commit comments