feat(codebase): smart auto-expand logic for Next.js and React project structures
This commit is contained in:
@@ -111,13 +111,64 @@ export function GiteaFileTree({
|
|||||||
.then(async (items) => {
|
.then(async (items) => {
|
||||||
if (cancelled) return;
|
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 newChildrenByPath: Record<string, TreeItem[]> = {};
|
||||||
const newExpanded = new Set<string>();
|
const newExpanded = new Set<string>();
|
||||||
|
|
||||||
// Cap at 10 to avoid API spam on huge repos
|
const rootDirs = items.filter((i) => i.type === "dir");
|
||||||
const dirsToExpand = dirs.slice(0, 10);
|
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 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 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(
|
await Promise.all(
|
||||||
dirsToExpand.map(async (dirPath) => {
|
dirsToExpand.map(async (dirPath) => {
|
||||||
try {
|
try {
|
||||||
@@ -131,6 +182,7 @@ export function GiteaFileTree({
|
|||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (cancelled) return;
|
if (cancelled) return;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user