2024-02-04 05:09:41 +00:00
|
|
|
import * as React from "react";
|
|
|
|
import { TreeView, TreeItem } from "@mui/x-tree-view";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { styled } from "@mui/material/styles";
|
2022-04-19 04:39:51 +00:00
|
|
|
|
2023-12-24 02:06:57 +00:00
|
|
|
const TCTreeItem = styled(TreeItem)(({ theme }) => ({
|
2024-02-04 05:09:41 +00:00
|
|
|
"& .MuiTreeItem-content": {
|
|
|
|
borderRadius: "10px",
|
|
|
|
"&:hover": {
|
|
|
|
backgroundColor: "rgba(25, 118, 210, 0.59)",
|
2024-01-07 03:04:57 +00:00
|
|
|
},
|
2024-02-04 05:09:41 +00:00
|
|
|
"& .MuiTreeItem-label": {
|
|
|
|
fontSize: "1rem",
|
|
|
|
paddingLeft: "6px",
|
|
|
|
fontFamily:
|
|
|
|
'-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif,',
|
2023-12-24 02:06:57 +00:00
|
|
|
lineHeight: 2.0,
|
2022-04-19 04:39:51 +00:00
|
|
|
},
|
2023-12-24 02:06:57 +00:00
|
|
|
},
|
2024-02-04 05:09:41 +00:00
|
|
|
}));
|
2022-03-23 15:25:28 +00:00
|
|
|
|
|
|
|
export default function FolderTree(props) {
|
2024-02-04 05:09:41 +00:00
|
|
|
const router = useRouter();
|
2024-01-07 03:04:57 +00:00
|
|
|
|
2024-02-04 05:09:41 +00:00
|
|
|
const memoTree = React.useMemo(() => renderTree(props.tree), [props.tree]);
|
|
|
|
const expandedNodes = [props.tree.id];
|
2024-01-07 03:04:57 +00:00
|
|
|
|
2023-12-24 02:06:57 +00:00
|
|
|
return (
|
2023-12-26 01:27:55 +00:00
|
|
|
<>
|
2024-02-04 05:09:41 +00:00
|
|
|
<a href="http://localhost:3000">{"<-"} Back To Portfolio</a>
|
2023-12-31 21:16:04 +00:00
|
|
|
|
2023-12-26 01:27:55 +00:00
|
|
|
<TreeView
|
|
|
|
aria-label="rich object"
|
2023-12-31 16:57:12 +00:00
|
|
|
defaultCollapseIcon={<ChevronDownIcon />}
|
2023-12-26 01:27:55 +00:00
|
|
|
defaultExpanded={expandedNodes}
|
|
|
|
defaultExpandIcon={<ChevronRightIcon />}
|
|
|
|
onNodeSelect={(event, nodIds) => {
|
2024-02-04 05:09:41 +00:00
|
|
|
// TODO: already sorted, impliment binary search
|
|
|
|
const currentNode = props.flattenNodes.find((aNode) => {
|
|
|
|
return aNode.id === nodIds;
|
|
|
|
});
|
|
|
|
console.log({ currentNode });
|
2023-12-26 01:27:55 +00:00
|
|
|
if (currentNode != null && currentNode.routePath != null) {
|
2024-02-04 05:09:41 +00:00
|
|
|
router.push(currentNode.routePath);
|
2023-12-26 01:27:55 +00:00
|
|
|
}
|
|
|
|
}}
|
2024-02-04 05:09:41 +00:00
|
|
|
sx={{ overflowY: "scroll" }}
|
2023-12-26 01:27:55 +00:00
|
|
|
>
|
2024-01-07 03:04:57 +00:00
|
|
|
{memoTree}
|
2023-12-26 01:27:55 +00:00
|
|
|
</TreeView>
|
|
|
|
</>
|
2023-12-24 02:06:57 +00:00
|
|
|
);
|
2022-03-23 15:25:28 +00:00
|
|
|
}
|
2023-12-31 16:57:12 +00:00
|
|
|
|
|
|
|
function ChevronRightIcon() {
|
|
|
|
return (
|
2024-02-04 05:09:41 +00:00
|
|
|
<svg
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
fill="none"
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
strokeWidth={1.5}
|
|
|
|
stroke="currentColor"
|
|
|
|
height={10}
|
|
|
|
width={10}
|
|
|
|
>
|
|
|
|
<path
|
|
|
|
strokeLinecap="round"
|
|
|
|
strokeLinejoin="round"
|
|
|
|
d="m8.25 4.5 7.5 7.5-7.5 7.5"
|
|
|
|
/>
|
2023-12-31 16:57:12 +00:00
|
|
|
</svg>
|
2024-02-04 05:09:41 +00:00
|
|
|
);
|
2023-12-31 16:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ChevronDownIcon() {
|
|
|
|
return (
|
2024-02-04 05:09:41 +00:00
|
|
|
<svg
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
fill="none"
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
strokeWidth={1.5}
|
|
|
|
stroke="currentColor"
|
|
|
|
height={10}
|
|
|
|
width={10}
|
|
|
|
>
|
|
|
|
<path
|
|
|
|
strokeLinecap="round"
|
|
|
|
strokeLinejoin="round"
|
|
|
|
d="m19.5 8.25-7.5 7.5-7.5-7.5"
|
|
|
|
/>
|
2023-12-31 16:57:12 +00:00
|
|
|
</svg>
|
2024-02-04 05:09:41 +00:00
|
|
|
);
|
2023-12-31 16:57:12 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 05:09:41 +00:00
|
|
|
function renderTree(nodes, layer = 0) {
|
2024-01-07 03:04:57 +00:00
|
|
|
return (
|
2024-02-04 05:09:41 +00:00
|
|
|
<TCTreeItem
|
|
|
|
key={nodes.id}
|
|
|
|
nodeId={nodes.id}
|
|
|
|
label={nodes.name}
|
|
|
|
sx={{ marginLeft: 1 * layer }}
|
|
|
|
>
|
2024-01-07 03:04:57 +00:00
|
|
|
{Array.isArray(nodes.children)
|
|
|
|
? nodes.children.map((node) => renderTree(node, layer + 1))
|
|
|
|
: null}
|
|
|
|
</TCTreeItem>
|
2024-02-04 05:09:41 +00:00
|
|
|
);
|
2024-01-07 03:04:57 +00:00
|
|
|
}
|