feat: auto-reveal active file in explorer on tab switch (#380)

* feat: auto-reveal active file in explorer on tab switch

Adds useFileExplorerAutoReveal hook that scrolls the file explorer to
show the active editor file when switching tabs, mirroring VS Code's
explorer.autoReveal behavior. Files already visible in the tree get
scrolled to directly; files with collapsed ancestors trigger the reveal
machinery with flash disabled to avoid visual noise.

* refactor: extract file explorer handlers to fix max-lines lint rule

Move handleClick, handleDoubleClick, and handleWheelCapture into a
dedicated useFileExplorerHandlers hook to bring FileExplorer.tsx under
the 400-line limit.
This commit is contained in:
Jinjing
2026-04-07 22:09:50 -07:00
committed by GitHub
parent f61ef05866
commit 5d8e69a235
5 changed files with 224 additions and 56 deletions
@@ -2,17 +2,17 @@ import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useVirtualizer } from '@tanstack/react-virtual'
import { Loader2 } from 'lucide-react'
import { useAppStore } from '@/store'
import { detectLanguage } from '@/lib/language-detect'
import { dirname, normalizeRelativePath } from '@/lib/path'
import { ScrollArea } from '@/components/ui/scroll-area'
import { cn } from '@/lib/utils'
import { FileDeleteDialog } from './FileDeleteDialog'
import { FileExplorerBackgroundMenu } from './FileExplorerBackgroundMenu'
import { FileExplorerRow, InlineInputRow } from './FileExplorerRow'
import type { TreeNode } from './file-explorer-types'
import { splitPathSegments } from './path-tree'
import { buildFolderStatusMap, buildStatusMap, STATUS_COLORS } from './status-display'
import { useFileDeletion } from './useFileDeletion'
import { useFileExplorerAutoReveal } from './useFileExplorerAutoReveal'
import { useFileExplorerHandlers } from './useFileExplorerHandlers'
import { useFileExplorerReveal } from './useFileExplorerReveal'
import { useFileExplorerInlineInput } from './useFileExplorerInlineInput'
import { useFileExplorerKeys } from './useFileExplorerKeys'
@@ -190,6 +190,18 @@ export default function FileExplorer(): React.JSX.Element {
virtualizer
})
useFileExplorerAutoReveal({
activeFileId,
activeWorktreeId,
worktreePath,
pendingExplorerReveal,
openFiles,
rowsByPath,
flatRows,
setSelectedPath,
virtualizer
})
useEffect(() => {
if (inlineInputIndex >= 0) {
virtualizer.scrollToIndex(inlineInputIndex, { align: 'auto' })
@@ -206,55 +218,17 @@ export default function FileExplorer(): React.JSX.Element {
requestDelete
})
const handleClick = useCallback(
(node: TreeNode) => {
if (!activeWorktreeId) {
return
}
setSelectedPath(node.path)
if (node.isDirectory) {
toggleDir(activeWorktreeId, node.path)
return
}
openFile({
filePath: node.path,
relativePath: node.relativePath,
worktreeId: activeWorktreeId,
language: detectLanguage(node.name),
mode: 'edit'
})
},
[activeWorktreeId, openFile, toggleDir]
)
const handleDoubleClick = useCallback(
(node: TreeNode) => {
if (!activeWorktreeId || node.isDirectory) {
return
}
pinFile(node.path)
},
[activeWorktreeId, pinFile]
)
const { handleClick, handleDoubleClick, handleWheelCapture } = useFileExplorerHandlers({
activeWorktreeId,
openFile,
pinFile,
toggleDir,
setSelectedPath,
scrollRef
})
const handleDuplicate = useFileDuplicate({ worktreePath, refreshDir })
const handleWheelCapture = useCallback((e: React.WheelEvent<HTMLDivElement>) => {
const container = scrollRef.current
if (!container || Math.abs(e.deltaY) <= Math.abs(e.deltaX)) {
return
}
const target = e.target
if (!(target instanceof Element) || !target.closest('[data-explorer-draggable="true"]')) {
return
}
if (container.scrollHeight <= container.clientHeight) {
return
}
e.preventDefault()
container.scrollTop += e.deltaY
}, [])
if (!worktreePath) {
return (
<div className="flex h-full items-center justify-center text-[11px] text-muted-foreground px-4 text-center">
@@ -0,0 +1,96 @@
import { useEffect, useRef } from 'react'
import type { Dispatch, SetStateAction } from 'react'
import type { Virtualizer } from '@tanstack/react-virtual'
import { useAppStore } from '@/store'
import type { OpenFile } from '@/store/slices/editor'
import type { TreeNode } from './file-explorer-types'
type UseFileExplorerAutoRevealParams = {
activeFileId: string | null
activeWorktreeId: string | null
worktreePath: string | null
pendingExplorerReveal: { worktreeId: string; filePath: string; requestId: number } | null
openFiles: OpenFile[]
rowsByPath: Map<string, TreeNode>
flatRows: TreeNode[]
setSelectedPath: Dispatch<SetStateAction<string | null>>
virtualizer: Virtualizer<HTMLDivElement, Element>
}
/**
* Auto-reveal: when the active editor file changes, scroll the explorer to show it.
* This mirrors VS Code's explorer.autoReveal behavior.
*
* For files already visible in the tree, scrolls directly (no flash).
* For files whose ancestors are collapsed, triggers the reveal machinery
* to expand ancestors and scroll, but skips the flash animation.
*/
export function useFileExplorerAutoReveal({
activeFileId,
activeWorktreeId,
worktreePath,
pendingExplorerReveal,
openFiles,
rowsByPath,
flatRows,
setSelectedPath,
virtualizer
}: UseFileExplorerAutoRevealParams): void {
const prevActiveFileIdRef = useRef<string | null>(null)
useEffect(() => {
if (activeFileId === prevActiveFileIdRef.current) {
return
}
prevActiveFileIdRef.current = activeFileId
if (!activeFileId || !activeWorktreeId || !worktreePath) {
return
}
// Don't override a pending manual reveal (e.g. from Source Control "Reveal in Explorer")
if (pendingExplorerReveal) {
return
}
// Only auto-reveal regular edit-mode files, not diffs or conflict reviews
const activeFile = openFiles.find((f) => f.id === activeFileId)
if (!activeFile || activeFile.worktreeId !== activeWorktreeId || activeFile.mode !== 'edit') {
return
}
const filePath = activeFile.filePath
if (rowsByPath.has(filePath)) {
// File is already visible in the tree — just scroll to it and select
setSelectedPath(filePath)
const targetIndex = flatRows.findIndex((row) => row.path === filePath)
if (targetIndex !== -1) {
requestAnimationFrame(() => {
virtualizer.scrollToIndex(targetIndex, { align: 'auto' })
})
}
} else {
// File's ancestor folders aren't expanded — use the reveal machinery
// to expand them and scroll, but skip the flash animation.
useAppStore.setState({
pendingExplorerReveal: {
worktreeId: activeWorktreeId,
filePath,
requestId: Date.now(),
flash: false
}
})
}
}, [
activeFileId,
activeWorktreeId,
worktreePath,
pendingExplorerReveal,
openFiles,
rowsByPath,
flatRows,
setSelectedPath,
virtualizer
])
}
@@ -0,0 +1,87 @@
import { useCallback } from 'react'
import type React from 'react'
import type { RefObject } from 'react'
import { detectLanguage } from '@/lib/language-detect'
import type { TreeNode } from './file-explorer-types'
type UseFileExplorerHandlersParams = {
activeWorktreeId: string | null
openFile: (params: {
filePath: string
relativePath: string
worktreeId: string
language: string
mode: 'edit'
}) => void
pinFile: (filePath: string) => void
toggleDir: (worktreeId: string, dirPath: string) => void
setSelectedPath: (path: string) => void
scrollRef: RefObject<HTMLDivElement | null>
}
type UseFileExplorerHandlersReturn = {
handleClick: (node: TreeNode) => void
handleDoubleClick: (node: TreeNode) => void
handleWheelCapture: (e: React.WheelEvent<HTMLDivElement>) => void
}
export function useFileExplorerHandlers({
activeWorktreeId,
openFile,
pinFile,
toggleDir,
setSelectedPath,
scrollRef
}: UseFileExplorerHandlersParams): UseFileExplorerHandlersReturn {
const handleClick = useCallback(
(node: TreeNode) => {
if (!activeWorktreeId) {
return
}
setSelectedPath(node.path)
if (node.isDirectory) {
toggleDir(activeWorktreeId, node.path)
return
}
openFile({
filePath: node.path,
relativePath: node.relativePath,
worktreeId: activeWorktreeId,
language: detectLanguage(node.name),
mode: 'edit'
})
},
[activeWorktreeId, openFile, toggleDir, setSelectedPath]
)
const handleDoubleClick = useCallback(
(node: TreeNode) => {
if (!activeWorktreeId || node.isDirectory) {
return
}
pinFile(node.path)
},
[activeWorktreeId, pinFile]
)
const handleWheelCapture = useCallback(
(e: React.WheelEvent<HTMLDivElement>) => {
const container = scrollRef.current
if (!container || Math.abs(e.deltaY) <= Math.abs(e.deltaX)) {
return
}
const target = e.target
if (!(target instanceof Element) || !target.closest('[data-explorer-draggable="true"]')) {
return
}
if (container.scrollHeight <= container.clientHeight) {
return
}
e.preventDefault()
container.scrollTop += e.deltaY
},
[scrollRef]
)
return { handleClick, handleDoubleClick, handleWheelCapture }
}
@@ -8,7 +8,12 @@ import type { DirCache, TreeNode } from './file-explorer-types'
type UseFileExplorerRevealParams = {
activeWorktreeId: string | null
worktreePath: string | null
pendingExplorerReveal: { worktreeId: string; filePath: string; requestId: number } | null
pendingExplorerReveal: {
worktreeId: string
filePath: string
requestId: number
flash?: boolean
} | null
clearPendingExplorerReveal: () => void
expanded: Set<string>
dirCache: Record<string, DirCache>
@@ -145,14 +150,19 @@ export function useFileExplorerReveal({
clearPendingExplorerReveal()
setSelectedPath(revealPath)
setFlashingPath(revealPath)
if (flashTimeoutRef.current !== null) {
window.clearTimeout(flashTimeoutRef.current)
// Only flash when explicitly requested (e.g. "Reveal in Explorer" from Source Control).
// Auto-reveals on tab switch skip the flash to avoid visual noise.
if (pendingExplorerReveal.flash !== false) {
setFlashingPath(revealPath)
if (flashTimeoutRef.current !== null) {
window.clearTimeout(flashTimeoutRef.current)
}
flashTimeoutRef.current = window.setTimeout(() => {
setFlashingPath((current) => (current === revealPath ? null : current))
flashTimeoutRef.current = null
}, 2000)
}
flashTimeoutRef.current = window.setTimeout(() => {
setFlashingPath((current) => (current === revealPath ? null : current))
flashTimeoutRef.current = null
}, 2000)
requestAnimationFrame(() => {
window.setTimeout(() => {
+1
View File
@@ -132,6 +132,7 @@ export type EditorSlice = {
worktreeId: string
filePath: string
requestId: number
flash?: boolean
} | null
revealInExplorer: (worktreeId: string, filePath: string) => void
clearPendingExplorerReveal: () => void