From 005993da2146eab83886ca7c44c32698f292171b Mon Sep 17 00:00:00 2001 From: Tuan Cao Date: Thu, 24 Mar 2022 11:15:43 +0700 Subject: [PATCH] Make Tree navigation go to correct page. --- components/FolderTree.js | 10 ++++++++++ lib/post.js | 23 ++++++++++++++++++++++- pages/index.js | 10 ++++++---- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/components/FolderTree.js b/components/FolderTree.js index 4665859..bc228f6 100644 --- a/components/FolderTree.js +++ b/components/FolderTree.js @@ -3,6 +3,7 @@ import TreeView from '@mui/lab/TreeView'; import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; import ChevronRightIcon from '@mui/icons-material/ChevronRight'; import TreeItem from '@mui/lab/TreeItem'; +import { useRouter } from 'next/router' export default function FolderTree(props) { const renderTree = (nodes) => ( @@ -13,12 +14,21 @@ export default function FolderTree(props) { ); + const router = useRouter() return ( } defaultExpanded={['root']} defaultExpandIcon={} + onNodeSelect = {(event, nodIds) => { + const currentNode = props.flattenNodes.find(aNode => {return aNode.id === nodIds}) + // console.log(event) + // console.log(currentNode) + if (currentNode != null && currentNode.routePath != null) { + router.push(currentNode.routePath) + } + }} sx={{ height: 110, flexGrow: 1, maxWidth: 400, overflowY: 'auto' }} > {renderTree(props.tree)} diff --git a/lib/post.js b/lib/post.js index 5d0c94c..b3b9c89 100644 --- a/lib/post.js +++ b/lib/post.js @@ -177,12 +177,18 @@ export function getPostListData() { export function getDirectoryData() { const filteredDirectory = dirTree(postsDirectory,{ extensions: /\.md/ }); const convertedData = convertObject(filteredDirectory) + // console.log() + // const array = getFlattenArray(convertedData) return convertedData } let _counter = 0; export function convertObject(thisObject) { const children = [] - const newObject = {name: thisObject.name, children: children, id: (_counter++).toString()}; + + + let routerPath = getPostListData().find(fileName => fileName === Transformer.normalizeFileName(thisObject.name) ) || null + routerPath = routerPath ? '/note/' +routerPath : null + const newObject = {name: thisObject.name, children: children, id: (_counter++).toString(), routePath: routerPath || null }; if (thisObject.children != null && thisObject.children.length > 0) { thisObject.children.forEach(aChild => { @@ -193,4 +199,19 @@ export function convertObject(thisObject) { } else { return newObject } +} + +function flat(array) { + var result = []; + array.forEach(function (a) { + result.push(a); + if (Array.isArray(a.children)) { + result = result.concat(flat(a.children)); + } + }); + return result; +} + +export function getFlattenArray (thisObject) { + return flat(thisObject.children) } \ No newline at end of file diff --git a/pages/index.js b/pages/index.js index a68ece5..503e747 100644 --- a/pages/index.js +++ b/pages/index.js @@ -1,13 +1,13 @@ import Layout, {siteTitle} from "../components/layout"; -import {getSinglePost, getGraphData, getDirectoryData, convertObject} from "../lib/post"; +import {getSinglePost, getGraphData, getDirectoryData, convertObject, getFlattenArray} from "../lib/post"; import FolderTree from "../components/FolderTree"; -export default function Home({content, graphdata, filenames, tree, ...props}) { +export default function Home({content, graphdata, filenames, tree,flattenNodes, ...props}) { return (
- +
@@ -19,11 +19,13 @@ export function getStaticProps() { const tree = convertObject(getDirectoryData()); const contentData = getSinglePost("index"); const graphdata = getGraphData(); + const flattenNodes = getFlattenArray(tree) return { props: { content: contentData, graphdata: graphdata, - tree: tree + tree: tree, + flattenNodes: flattenNodes }, }; }