From 20e3ee2c57d40d6c87b9af1e6d7ece72c2bb5878 Mon Sep 17 00:00:00 2001 From: Mihovil Ilakovac Date: Tue, 20 Jun 2023 12:03:01 +0200 Subject: [PATCH] Updates file tree behaviour Signed-off-by: Mihovil Ilakovac --- wasp-ai/main.wasp | 6 ++- .../src/client/components/CodeHighlight.jsx | 2 +- wasp-ai/src/client/components/FileTree.jsx | 42 +++++++++++++++++ wasp-ai/src/client/pages/MainPage.jsx | 46 +++++++++++++------ 4 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 wasp-ai/src/client/components/FileTree.jsx diff --git a/wasp-ai/main.wasp b/wasp-ai/main.wasp index 33ff8473c..8b4298cae 100644 --- a/wasp-ai/main.wasp +++ b/wasp-ai/main.wasp @@ -3,7 +3,11 @@ app waspAi { version: "^0.10.6" }, title: "wasp-ai", - dependencies: [("uuid", "^9.0.0"), ("prismjs", "^1.29.0")], + dependencies: [ + ("uuid", "^9.0.0"), + ("prismjs", "^1.29.0"), + ("react-accessible-treeview", "2.6.1") + ], client: { rootComponent: import { RootComponent } from "@client/RootComponent.jsx", } diff --git a/wasp-ai/src/client/components/CodeHighlight.jsx b/wasp-ai/src/client/components/CodeHighlight.jsx index 42039d64a..61ee08145 100644 --- a/wasp-ai/src/client/components/CodeHighlight.jsx +++ b/wasp-ai/src/client/components/CodeHighlight.jsx @@ -13,7 +13,7 @@ export function CodeHighlight(props = {}) { ...others } = props; const langCls = language ? `language-${language}` : ""; - async function highlight() { + function highlight() { if (codeRef.current) { Prism.highlightElement(codeRef.current); } diff --git a/wasp-ai/src/client/components/FileTree.jsx b/wasp-ai/src/client/components/FileTree.jsx new file mode 100644 index 000000000..cd7499a03 --- /dev/null +++ b/wasp-ai/src/client/components/FileTree.jsx @@ -0,0 +1,42 @@ +import React, { useMemo } from "react"; +export function FileTree({ paths, activeFilePath, onActivePathSelect }) { + const tree = useMemo(() => { + if (paths && paths.length > 0) { + const tree = {}; + paths.forEach((path) => { + const pathParts = path.split("/"); + let node = tree; + pathParts.forEach((part, i) => { + if (i === pathParts.length - 1) { + node[part] = path; + } else { + if (!node[part]) { + node[part] = {}; + } + node = node[part]; + } + }); + }); + return tree; + } else { + return null; + } + }, [paths]); + + return ( +
+ {paths.map((path) => ( +
onActivePathSelect(path)} + > +
{path}
+
+ ))} +
+ ); +} diff --git a/wasp-ai/src/client/pages/MainPage.jsx b/wasp-ai/src/client/pages/MainPage.jsx index dbdb5ab5e..69f7c429e 100644 --- a/wasp-ai/src/client/pages/MainPage.jsx +++ b/wasp-ai/src/client/pages/MainPage.jsx @@ -4,6 +4,7 @@ import startGeneratingNewApp from "@wasp/actions/startGeneratingNewApp"; import getAppGenerationResult from "@wasp/queries/getAppGenerationResult"; import { useQuery } from "@wasp/queries"; import { CodeHighlight } from "../components/CodeHighlight"; +import { FileTree } from "../components/FileTree"; const MainPage = () => { const [appName, setAppName] = useState(""); @@ -68,6 +69,30 @@ const MainPage = () => { } }, [activeFilePath]); + const interestingFilePaths = useMemo(() => { + if (files) { + return Object.keys(files) + .filter( + (path) => + path !== ".env.server" && + path !== ".env.client" && + path !== "src/client/vite-env.d.ts" && + path !== "src/client/tsconfig.json" && + path !== "src/server/tsconfig.json" && + path !== "src/shared/tsconfig.json" && + path !== ".gitignore" && + path !== "src/.waspignore" && + path !== ".wasproot" + ) + .sort( + (a, b) => + (a.endsWith(".wasp") ? 0 : 1) - (b.endsWith(".wasp") ? 0 : 1) + ); + } else { + return []; + } + }, [files]); + return (
{ Files
-