feat(playground): enhance file tree structure by adding path property to FileTree

This commit is contained in:
ngc2207 2024-12-15 00:02:31 +08:00
parent 2cdd9adf3e
commit ac7e34dfc5

View File

@ -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<FileTree>({
name: "",
path: "",
type: "tree",
children: {},
});