From ccba3d42d27d1d41d2589b02cf6faa144510dbfd Mon Sep 17 00:00:00 2001 From: mawkone Date: Sun, 14 Jun 2026 13:45:03 -0700 Subject: [PATCH] feat(codebase): smart auto-expand logic for Next.js and React project structures --- .../components/project/gitea-file-tree.tsx | 80 +++++++++++++++---- 1 file changed, 66 insertions(+), 14 deletions(-) diff --git a/vibn-frontend/components/project/gitea-file-tree.tsx b/vibn-frontend/components/project/gitea-file-tree.tsx index ceaa0d6..89be813 100644 --- a/vibn-frontend/components/project/gitea-file-tree.tsx +++ b/vibn-frontend/components/project/gitea-file-tree.tsx @@ -111,26 +111,78 @@ export function GiteaFileTree({ .then(async (items) => { if (cancelled) return; - // Auto-expand top-level directories so the tree doesn't look empty - const dirs = items.filter((i) => i.type === "dir").map((i) => i.path); const newChildrenByPath: Record = {}; const newExpanded = new Set(); - // Cap at 10 to avoid API spam on huge repos - const dirsToExpand = dirs.slice(0, 10); - await Promise.all( - dirsToExpand.map(async (dirPath) => { + const rootDirs = items.filter((i) => i.type === "dir"); + const rootDirNames = rootDirs.map((i) => i.name); + + const hasSrc = rootDirNames.includes("src"); + const hasAppOrComponents = + rootDirNames.includes("app") || + rootDirNames.includes("components") || + rootDirNames.includes("pages"); + + if (hasSrc || hasAppOrComponents) { + // Smart default: expand app/components/pages, and src -> app/components/pages + if (hasSrc) { + const srcItem = rootDirs.find((i) => i.name === "src")!; try { - const children = await fetchPath(dirPath); - newChildrenByPath[dirPath] = children; - newExpanded.add(dirPath); + const srcItems = await fetchPath(srcItem.path); + newChildrenByPath[srcItem.path] = srcItems; + newExpanded.add(srcItem.path); + + const srcDirs = srcItems.filter((i) => i.type === "dir"); + const subPromises = []; + for (const target of ["app", "components", "pages", "lib"]) { + const subItem = srcDirs.find((i) => i.name === target); + if (subItem) { + subPromises.push( + fetchPath(subItem.path).then((children) => { + newChildrenByPath[subItem.path] = children; + newExpanded.add(subItem.path); + }), + ); + } + } + await Promise.all(subPromises); } catch (e) { - console.warn( - `[gitea-file-tree] failed to auto-expand ${dirPath}`, - ); + console.warn(`[gitea-file-tree] failed to auto-expand src`); } - }), - ); + } + + if (hasAppOrComponents) { + const subPromises = []; + for (const target of ["app", "components", "pages", "lib"]) { + const rootItem = rootDirs.find((i) => i.name === target); + if (rootItem) { + subPromises.push( + fetchPath(rootItem.path).then((children) => { + newChildrenByPath[rootItem.path] = children; + newExpanded.add(rootItem.path); + }), + ); + } + } + await Promise.all(subPromises); + } + } else { + // Fallback: auto-expand up to 10 top-level directories so it doesn't look empty + const dirsToExpand = rootDirs.map((i) => i.path).slice(0, 10); + await Promise.all( + dirsToExpand.map(async (dirPath) => { + try { + const children = await fetchPath(dirPath); + newChildrenByPath[dirPath] = children; + newExpanded.add(dirPath); + } catch (e) { + console.warn( + `[gitea-file-tree] failed to auto-expand ${dirPath}`, + ); + } + }), + ); + } if (cancelled) return;