feat(codebase): smart auto-expand logic for Next.js and React project structures
This commit is contained in:
@@ -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<string, TreeItem[]> = {};
|
||||
const newExpanded = new Set<string>();
|
||||
|
||||
// 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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user