fix(web): retain selection during root reveal

Co-authored-by: Exotic <[email protected]>
This commit is contained in:
t3-code[bot]andExotic committed 2026-08-16 01:34:28 +00:00
1 parent 1b0f5978b3
commit a6cd708825
2 files changed
+44 -10

No files matched your search

@@ -59,7 +59,7 @@ The goal is not to minimize CSS or class counts at any cost. The goal is to put
- Preserve runtime top and bottom overflow state. Do not replace dynamic fades with an always-on static mask.
- Preserve fade geometry and keep the native scrollbar lane opaque so the track and thumb stay visible and usable. A visually similar mask that fades the scrollbar is a regression.
- For path-driven tree actions with flattened directory rows, do not assume that every registered item path is currently visible. Resolve the target through the tree's visible projection before selecting, focusing, or scrolling, and verify intermediate directory actions against a real flattened chain.
- Treat external tree-reveal requests as one-shot events: resolve and validate the visible target without mutating search, focus, or selection, then acknowledge or clear the request after success so component remounts cannot replay stale navigation. A project-root reveal should scroll the tree viewport itself and preserve the current selected item; raw entry order is not visible row order. If a reveal opens and mounts the tree, suppress the normal selected-file reveal while the request is pending and wait for the real virtualized viewport before scrolling.
- Treat external tree-reveal requests as one-shot events: resolve and validate the visible target without mutating search, focus, or selection, then acknowledge or clear the request after success, or reject it when the path is terminally invalid, so component remounts cannot replay stale navigation. Retain requests that are retryable while the tree or viewport is not ready. A project-root reveal should scroll the tree viewport itself and preserve the current selected item; raw entry order is not visible row order. If a reveal opens and mounts the tree, select the current file without focusing or scrolling it, then wait for the real virtualized viewport before scrolling to the root.
- Verify actual scroll behavior when changing virtualizers, masks, overflow ownership, or scrollbar selectors. Source-level class comparison is not enough.
## Visual and layout preservation
@@ -144,8 +144,15 @@ export default function FileBrowserPanel({
const treeSelectionPathRef = useRef<string | null>(null);
const handledRevealRef = useRef<{ path: string; revealId: number } | null>(null);
const handledBreadcrumbRevealRef = useRef<{ path: string; revealId: number } | null>(null);
const pendingBreadcrumbRevealPathRef = useRef(breadcrumbRevealPath);
pendingBreadcrumbRevealPathRef.current = breadcrumbRevealPath;
const suppressSelectedPathScrollRef = useRef(breadcrumbRevealPath === "");
if (
breadcrumbRevealPath === "" &&
selectedPath &&
(handledRevealRef.current?.path !== selectedPath ||
handledRevealRef.current.revealId !== selectedPathRevealId)
) {
suppressSelectedPathScrollRef.current = true;
}
// The tree renders rows in shadow DOM and its anchor rect is unreliable, so
// capture the right-click position ourselves; contextmenu is a composed
@@ -291,12 +298,9 @@ export default function FileBrowserPanel({
useEffect(() => {
if (!selectedPath) {
handledRevealRef.current = null;
suppressSelectedPathScrollRef.current = false;
return;
}
// Opening the explorer from a breadcrumb mounts this effect before the
// breadcrumb reveal below. Let the explicit breadcrumb request own the
// initial focus and scroll instead of centering the open file first.
if (pendingBreadcrumbRevealPathRef.current !== null) return;
const revealRequest = { path: selectedPath, revealId: selectedPathRevealId };
const handledReveal = handledRevealRef.current;
// Entry refreshes rebuild treePaths while the same preview stays open.
@@ -323,6 +327,25 @@ export default function FileBrowserPanel({
handledRevealRef.current = revealRequest;
return;
}
if (suppressSelectedPathScrollRef.current) {
treeSelectionPathRef.current = null;
handledRevealRef.current = revealRequest;
suppressSelectedPathScrollRef.current = false;
syncingSelectionRef.current = true;
const segments = selectedPath.split("/");
let ancestorPath = "";
for (const segment of segments.slice(0, -1)) {
ancestorPath = ancestorPath ? `${ancestorPath}/${segment}` : segment;
const item = model.getItem(`${ancestorPath}/`) ?? model.getItem(ancestorPath);
if (item && "expand" in item) item.expand();
}
selectedItem.select();
queueMicrotask(() => {
syncingSelectionRef.current = false;
});
return;
}
treeSelectionPathRef.current = null;
handledRevealRef.current = revealRequest;
@@ -370,6 +393,7 @@ export default function FileBrowserPanel({
if (!scrollContainer) {
attemptsRemaining -= 1;
if (attemptsRemaining > 0) frameId = requestAnimationFrame(revealProjectRoot);
else onBreadcrumbRevealHandled(breadcrumbRevealId);
return;
}
handledBreadcrumbRevealRef.current = revealRequest;
@@ -384,20 +408,29 @@ export default function FileBrowserPanel({
}
const entryKind = entryKinds.get(breadcrumbRevealPath);
if (!entryKind) return;
if (!entryKind) {
if (!entriesQuery.isPending) onBreadcrumbRevealHandled(breadcrumbRevealId);
return;
}
const itemPath =
entryKind === "directory" && breadcrumbRevealPath
? `${breadcrumbRevealPath}/`
: breadcrumbRevealPath;
const item = itemPath ? (model.getItem(itemPath) ?? model.getItem(breadcrumbRevealPath)) : null;
if (!item) return;
if (!item) {
if (!entriesQuery.isPending) onBreadcrumbRevealHandled(breadcrumbRevealId);
return;
}
const visibleItemPath = itemPath
? entryKind === "directory"
? visibleDirectoryTreePath(entries, breadcrumbRevealPath)
: itemPath
: null;
if (visibleItemPath && !model.getItem(visibleItemPath)) return;
if (visibleItemPath && !model.getItem(visibleItemPath)) {
if (!entriesQuery.isPending) onBreadcrumbRevealHandled(breadcrumbRevealId);
return;
}
handledBreadcrumbRevealRef.current = revealRequest;
syncingSelectionRef.current = true;
@@ -428,6 +461,7 @@ export default function FileBrowserPanel({
breadcrumbRevealId,
breadcrumbRevealPath,
entries,
entriesQuery.isPending,
entryKinds,
model,
onBreadcrumbRevealHandled,