GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(web): Files tree opens a chain of single folders in one click (#4… · patroza/t3code@000ea81

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/web/src/components/files
    • FileBrowserPanel.browser.test.tsx
    • FileBrowserPanel.tsx
    • fileTreeExpansion.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/web/src/components/files/FileBrowserPanel.browser.test.tsx‎

Copy file name to clipboardExpand all lines: apps/web/src/components/files/FileBrowserPanel.browser.test.tsx
+57Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,63 @@ describe("FileBrowserPanel", () => {
378378
await userEvent.click(page.getByRole("button", { name: "Refresh workspace files" }));
379379
expect(new Set(revalidate.mock.calls.flat())).toEqual(new Set(["src", ""]));
380380
});
381+
382+
it("opens a chain of single folders from one click and keeps it closed once closed", async () => {
383+
const chain: readonly ProjectEntry[] = [
384+
{ path: "tests", kind: "directory" },
385+
{ path: "tests/edit", kind: "directory" },
386+
{ path: "tests/edit/unit", kind: "directory" },
387+
{ path: "tests/edit/unit/a.ts", kind: "file" },
388+
];
389+
const panel = () => (
390+
<FileBrowserPanel
391+
environmentId={EnvironmentId.make("env-files")}
392+
cwd="/repo-single-folder-chain"
393+
projectName="repo"
394+
selectedPath={null}
395+
selectedPathRevealId={0}
396+
onOpenFile={() => {}}
397+
workspaceMutationId={null}
398+
/>
399+
);
400+
using _listing = withDirectoryListing(chain.slice(0, 1));
401+
{
402+
await using screen = await render(panel());
403+
await vi.waitFor(() => expect(findTreeRow("tests/")).not.toBeNull());
404+
findTreeRow("tests/")!.click();
405+
await vi.waitFor(() =>
406+
expect(findTreeRow("tests/")!.getAttribute("aria-expanded")).toBe("true"),
407+
);
408+
for (const listed of [2, 3, 4]) {
409+
directory.entries = chain.slice(0, listed);
410+
await screen.rerender(panel());
411+
}
412+
await vi.waitFor(() => expect(findTreeRow("tests/edit/unit/a.ts")).not.toBeNull());
413+
414+
findTreeRow("tests/edit/unit/")!.click();
415+
await vi.waitFor(() => expect(findTreeRow("tests/edit/unit/a.ts")).toBeNull());
416+
directory.entries = [...chain, { path: "tests/edit/unit/b.ts", kind: "file" }];
417+
await screen.rerender(panel());
418+
expect(findTreeRow("tests/edit/unit/")!.getAttribute("aria-expanded")).toBe("false");
419+
}
420+
421+
directory.entries = [...chain, { path: "pkg", kind: "directory" }];
422+
await using remounted = await render(panel());
423+
await vi.waitFor(() => expect(findTreeRow("tests/edit/unit/")).not.toBeNull());
424+
expect(findTreeRow("tests/edit/unit/")!.getAttribute("aria-expanded")).toBe("false");
425+
426+
// An ignored folder can hold tens of thousands of entries, so it still opens by hand.
427+
findTreeRow("pkg/")!.click();
428+
await vi.waitFor(() => expect(findTreeRow("pkg/")!.getAttribute("aria-expanded")).toBe("true"));
429+
directory.entries = [
430+
...directory.entries,
431+
{ path: "pkg/node_modules", kind: "directory", ignored: true },
432+
];
433+
await remounted.rerender(panel());
434+
await vi.waitFor(() => expect(findTreeRow("pkg/node_modules/")).not.toBeNull());
435+
expect(findTreeRow("pkg/node_modules/")!.getAttribute("aria-expanded")).toBe("false");
436+
});
437+
381438
describe("revealing a folder while folders above it are still loading", () => {
382439
const roots = Array.from({ length: 12 }, (_, index): ProjectEntry => {
383440
return { path: `d${String(index).padStart(2, "0")}`, kind: "directory" };
Collapse file

‎apps/web/src/components/files/FileBrowserPanel.tsx‎

Copy file name to clipboardExpand all lines: apps/web/src/components/files/FileBrowserPanel.tsx
+42-4Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { createFileTreeDragMentionController } from "./fileTreeDragMention";
3939
import {
4040
areAllDirectoriesExpanded,
4141
coalesceReentrantCalls,
42+
onlyChildren,
4243
readExpandedDirectories,
4344
setAllDirectoriesExpanded,
4445
writeExpandedDirectories,
@@ -165,6 +166,9 @@ function buildTreeEntries(
165166
return { entries, entryKinds, treePaths, directoryPaths, expandableDirectoryPaths };
166167
}
167168

169+
// A symlink to its own parent lists itself as an only child without end.
170+
const MAX_FOLDERS_OPENED_WITH_PARENT = 16;
171+
168172
export default function FileBrowserPanel({
169173
environmentId,
170174
cwd,
@@ -390,29 +394,63 @@ export default function FileBrowserPanel({
390394
const expansionRoot = `${environmentId}:${cwd}`;
391395
// Folders expanded when the panel last unmounted; each reopens once lazy loading lists it.
392396
const [pendingExpandedPaths] = useState(() => new Set(readExpandedDirectories(expansionRoot)));
397+
// Folders just opened and not listed yet, with how many only-child folders may open below each.
398+
// Pierre merges such a chain into one row that takes its open state from the last folder.
399+
const openingPathsRef = useRef(new Map<string, number>());
393400
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to the Pierre tree model to load directories as they are expanded
394401
useEffect(() => {
395402
const currentPaths = new Set(directoryPaths);
396403
for (const path of expandedPathsRef.current) {
397404
if (!currentPaths.has(path)) expandedPathsRef.current.delete(path);
398405
}
406+
for (const path of openingPathsRef.current.keys()) {
407+
if (!currentPaths.has(path)) openingPathsRef.current.delete(path);
408+
}
409+
let childrenByParent: ReadonlyMap<string, ProjectEntry | null> | null = null;
410+
const openOnlyChild = (path: string) => {
411+
const remaining = openingPathsRef.current.get(path);
412+
if (remaining === undefined) return;
413+
if (childrenByParent === null) childrenByParent = onlyChildren(directoryEntries);
414+
const child = childrenByParent.get(path.replace(/\/$/, ""));
415+
if (child === undefined) return;
416+
// Like expand all, leave an ignored folder such as `node_modules` to open by hand.
417+
const opensChild =
418+
child?.kind === "directory" &&
419+
(!child.ignored ||
420+
directoryEntries.some((entry) => entry.ignored && treePath(entry) === path));
421+
if (!opensChild || remaining === 0) {
422+
openingPathsRef.current.delete(path);
423+
return;
424+
}
425+
const childItem = model.getItem(treePath(child));
426+
// The listing reaches the tree after this effect, which notifies again.
427+
if (!childItem || !("expand" in childItem)) return;
428+
openingPathsRef.current.delete(path);
429+
if (childItem.isExpanded()) return;
430+
openingPathsRef.current.set(treePath(child), remaining - 1);
431+
childItem.expand();
432+
};
399433
// Restoring calls item.expand(), which notifies subscribers synchronously.
400434
const loadExpanded = coalesceReentrantCalls(() => {
401435
if (model.isSearchOpen()) return;
402436
let changed = false;
403437
for (const path of directoryPaths) {
404438
const item = model.getItem(path);
405-
if (item?.isDirectory() && pendingExpandedPaths.delete(path) && "expand" in item) {
406-
item.expand();
407-
}
439+
const restored = item?.isDirectory() && pendingExpandedPaths.delete(path);
440+
if (restored && item && "expand" in item) item.expand();
408441
if (item?.isDirectory() && "isExpanded" in item && item.isExpanded()) {
409442
if (!expandedPathsRef.current.has(path)) {
410443
expandedPathsRef.current.add(path);
411444
changed = true;
445+
if (!restored && !openingPathsRef.current.has(path)) {
446+
openingPathsRef.current.set(path, MAX_FOLDERS_OPENED_WITH_PARENT);
447+
}
412448
void load(path.replace(/\/$/, ""));
413449
}
450+
openOnlyChild(path);
414451
} else {
415452
if (item?.isDirectory() && expandedPathsRef.current.has(path)) setExpandAll(false);
453+
openingPathsRef.current.delete(path);
416454
if (expandedPathsRef.current.delete(path)) changed = true;
417455
}
418456
}
@@ -425,7 +463,7 @@ export default function FileBrowserPanel({
425463
});
426464
loadExpanded();
427465
return model.subscribe(loadExpanded);
428-
}, [directoryPaths, expansionRoot, load, model, pendingExpandedPaths]);
466+
}, [directoryEntries, directoryPaths, expansionRoot, load, model, pendingExpandedPaths]);
429467
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- pushes ignored-file status into the imperative Pierre tree model
430468
useEffect(() => {
431469
model.setGitStatus(
Collapse file

‎apps/web/src/components/files/fileTreeExpansion.ts‎

Copy file name to clipboardExpand all lines: apps/web/src/components/files/fileTreeExpansion.ts
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ProjectEntry } from "@t3tools/contracts";
12
import * as Schema from "effect/Schema";
23

34
import { getLocalStorageItem, setLocalStorageItem } from "~/hooks/useLocalStorage";
@@ -82,6 +83,18 @@ export function setAllDirectoriesExpanded(
8283
}
8384
}
8485

86+
/** Maps each listed parent path to its only child, or to null when it holds several entries. */
87+
export function onlyChildren(
88+
entries: readonly ProjectEntry[],
89+
): ReadonlyMap<string, ProjectEntry | null> {
90+
const result = new Map<string, ProjectEntry | null>();
91+
for (const entry of entries) {
92+
const parent = entry.path.slice(0, Math.max(0, entry.path.lastIndexOf("/")));
93+
result.set(parent, result.has(parent) ? null : entry);
94+
}
95+
return result;
96+
}
97+
8598
/** Calls made while `run` is running rerun it once it returns, instead of nesting. */
8699
export function coalesceReentrantCalls(run: () => void): () => void {
87100
let running = false;

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.