From ac7e34dfc5806aa33b08a70d26bc31a2860ae1a7 Mon Sep 17 00:00:00 2001 From: ngc2207 Date: Sun, 15 Dec 2024 00:02:31 +0800 Subject: [PATCH] feat(playground): enhance file tree structure by adding path property to FileTree --- src/app/playground/components/playground-sidebar.tsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/app/playground/components/playground-sidebar.tsx b/src/app/playground/components/playground-sidebar.tsx index 886345c..82dbc32 100644 --- a/src/app/playground/components/playground-sidebar.tsx +++ b/src/app/playground/components/playground-sidebar.tsx @@ -26,12 +26,13 @@ import { ChevronRight, File, Folder, FolderOpen } from "lucide-react"; interface FileTree { name: string; + path: string; type: "blob" | "tree"; children?: { [key: string]: FileTree }; } export function buildFileTree(tree: GitEntry[]): FileTree { - const root: FileTree = { name: "", type: "tree", children: {} }; + const root: FileTree = { name: "", type: "tree", path: "", children: {} }; tree.forEach((entry: GitEntry) => { if (!entry.path) return; @@ -39,14 +40,17 @@ export function buildFileTree(tree: GitEntry[]): FileTree { const pathParts = entry.path.split("/"); let currentLevel = root; - pathParts.forEach((part: string) => { + pathParts.forEach((part: string, index: number) => { if (!currentLevel.children) { currentLevel.children = {}; } + const fullPath = pathParts.slice(0, index + 1).join("/"); + if (!currentLevel.children[part]) { currentLevel.children[part] = { name: part, + path: fullPath, type: entry.type as "blob" | "tree", children: {}, }; @@ -78,6 +82,7 @@ export function PlaygroundSidebar({ const { data: session } = useSession(); const [fileTree, setFileTree] = React.useState({ name: "", + path: "", type: "tree", children: {}, });