Fix file tree folders flickering on file select

Since the browsed file lives in the route, every click reloads the route
and open folders re-rendered their contents, briefly collapsing them.
Folders now load their entries once and the tree is rebuilt only when
the commit changes.
This commit is contained in:
Rūdolfs Ošiņš
2026-10-09 10:29:32 +02:00
parent 1b07495946
commit b3477e0365
3 changed files with 78 additions and 36 deletions
+33 -31
View File
@@ -1,6 +1,8 @@
<script lang="ts">
import type { Tree } from "@bindings/source/Tree";
import { untrack } from "svelte";
import FileTreeFile from "@app/components/FileTreeFile.svelte";
import FileTreeFolder from "@app/components/FileTreeFolder.svelte";
import Icon from "@app/components/Icon.svelte";
@@ -30,9 +32,14 @@
}: Props = $props();
const expanded = $derived(isExpanded(prefix));
const treePromise = $derived(
expanded ? fetchTree(prefix) : Promise.resolve(undefined),
);
let tree: Tree | undefined = $state.raw();
let requested = false;
$effect(() => {
if (!expanded || requested) return;
requested = true;
void untrack(() => fetchTree(prefix)).then(loaded => (tree = loaded));
});
</script>
<style>
@@ -61,32 +68,27 @@
</div>
</div>
{#if expanded}
{#await treePromise then tree}
{#if tree}
<div
style:display="flex"
style:flex-direction="column"
style:gap="0.25rem">
{#each tree.entries as entry (entry.path)}
{#if entry.kind === "tree"}
<FileTreeFolder
{fetchTree}
{onSelect}
name={entry.name}
{currentPath}
{isExpanded}
{toggleExpanded}
prefix={`${entry.path}/`}
indent={indent + 1.5} />
{:else if entry.kind === "blob"}
<FileTreeFile
name={entry.name}
onSelect={() => onSelect(entry.path)}
active={entry.path === currentPath}
indent={indent + 1.5} />
{/if}
{/each}
</div>
{/if}
{/await}
{#if tree}
<div style:display="flex" style:flex-direction="column" style:gap="0.25rem">
{#each tree.entries as entry (entry.path)}
{#if entry.kind === "tree"}
<FileTreeFolder
{fetchTree}
{onSelect}
name={entry.name}
{currentPath}
{isExpanded}
{toggleExpanded}
prefix={`${entry.path}/`}
indent={indent + 1.5} />
{:else if entry.kind === "blob"}
<FileTreeFile
name={entry.name}
onSelect={() => onSelect(entry.path)}
active={entry.path === currentPath}
indent={indent + 1.5} />
{/if}
{/each}
</div>
{/if}
{/if}
+7 -5
View File
@@ -222,11 +222,13 @@
{#if tree.entries.length > 0}
<ScrollArea
style="border-right: 1px solid var(--color-border-subtle); flex: 1; min-height: 0; width: 100%; padding: 0.5rem;">
<TreeComponent
{tree}
{currentPath}
{fetchTree}
onSelect={showPath} />
{#key oid}
<TreeComponent
{tree}
{currentPath}
{fetchTree}
onSelect={showPath} />
{/key}
</ScrollArea>
{/if}
</div>
+38
View File
@@ -37,6 +37,44 @@ test("the repo home shows the README and browses the tree", async ({
await expect(page.getByText("Install it first.")).toBeVisible();
});
async function createNestedDocsRepo(peer: RadiclePeer) {
const { rid, repoFolder } = await createRepo(peer, { name: "docs" });
const files = {
"README.md": "# Docs\n\nWelcome to the docs.\n",
"guides/setup.md": "Install it first.\n",
"guides/usage.md": "Then use it.\n",
"api/index.md": "The API reference.\n",
};
for (const [file, content] of Object.entries(files)) {
await Fs.mkdir(Path.dirname(Path.join(repoFolder, file)), {
recursive: true,
});
await Fs.writeFile(Path.join(repoFolder, file), content);
}
await peer.git(["add", "."], { cwd: repoFolder });
await peer.git(["commit", "-m", "Write the docs"], { cwd: repoFolder });
await peer.git(["push", "rad", "main"], { cwd: repoFolder });
return { rid, repoFolder };
}
test("selecting a file keeps the open folder's entries rendered", async ({
page,
peer,
}) => {
const { rid } = await createNestedDocsRepo(peer);
await page.goto(`/repos/${rid}/home`);
await expect(page.getByText("Welcome to the docs.")).toBeVisible();
await page.getByText("guides", { exact: true }).click();
const sibling = await page
.getByText("usage.md", { exact: true })
.elementHandle();
await page.getByText("setup.md", { exact: true }).click();
await expect(page.getByText("Install it first.")).toBeVisible();
expect(await sibling?.evaluate(el => el.isConnected)).toBe(true);
});
test("commits are listed and open with their changes", async ({
page,
peer,