Fixing directory/file selection logic to deselect folders properly and always hightlight files. (#4408)
* File tree acts as VS Code's file tree * Adjust test for new expectations * Remove screenshot * Actually remove this screenshot * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu-latest-8-cores) * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu-latest-8-cores) * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu-latest-8-cores) * fix: fixed logic for selection * fix: always show the current file --------- Co-authored-by: 49lf <ircsurfer33@gmail.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
@ -154,6 +154,8 @@ const FileTreeItem = ({
|
|||||||
onCreateFolder,
|
onCreateFolder,
|
||||||
newTreeEntry,
|
newTreeEntry,
|
||||||
level = 0,
|
level = 0,
|
||||||
|
treeSelection,
|
||||||
|
setTreeSelection,
|
||||||
}: {
|
}: {
|
||||||
parentDir: FileEntry | undefined
|
parentDir: FileEntry | undefined
|
||||||
project?: IndexLoaderData['project']
|
project?: IndexLoaderData['project']
|
||||||
@ -170,12 +172,15 @@ const FileTreeItem = ({
|
|||||||
onCreateFolder: (name: string) => void
|
onCreateFolder: (name: string) => void
|
||||||
newTreeEntry: TreeEntry
|
newTreeEntry: TreeEntry
|
||||||
level?: number
|
level?: number
|
||||||
|
treeSelection: FileEntry | undefined
|
||||||
|
setTreeSelection: Dispatch<React.SetStateAction<FileEntry | undefined>>
|
||||||
}) => {
|
}) => {
|
||||||
const { send: fileSend, context: fileContext } = useFileContext()
|
const { send: fileSend, context: fileContext } = useFileContext()
|
||||||
const { onFileOpen, onFileClose } = useLspContext()
|
const { onFileOpen, onFileClose } = useLspContext()
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const [isConfirmingDelete, setIsConfirmingDelete] = useState(false)
|
const [isConfirmingDelete, setIsConfirmingDelete] = useState(false)
|
||||||
const isCurrentFile = fileOrDir.path === currentFile?.path
|
const isCurrentFile = fileOrDir.path === currentFile?.path
|
||||||
|
const isFileOrDirHighlighted = treeSelection?.path === fileOrDir?.path
|
||||||
const itemRef = useRef(null)
|
const itemRef = useRef(null)
|
||||||
|
|
||||||
// Since every file or directory gets its own FileTreeItem, we can do this.
|
// Since every file or directory gets its own FileTreeItem, we can do this.
|
||||||
@ -242,6 +247,8 @@ const FileTreeItem = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function handleClick() {
|
async function handleClick() {
|
||||||
|
setTreeSelection(fileOrDir)
|
||||||
|
|
||||||
if (fileOrDir.children !== null) return // Don't open directories
|
if (fileOrDir.children !== null) return // Don't open directories
|
||||||
|
|
||||||
if (fileOrDir.name?.endsWith(FILE_EXT) === false && project?.path) {
|
if (fileOrDir.name?.endsWith(FILE_EXT) === false && project?.path) {
|
||||||
@ -263,6 +270,7 @@ const FileTreeItem = ({
|
|||||||
// Open kcl files
|
// Open kcl files
|
||||||
navigate(`${PATHS.FILE}/${encodeURIComponent(fileOrDir.path)}`)
|
navigate(`${PATHS.FILE}/${encodeURIComponent(fileOrDir.path)}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
onNavigateToFile?.()
|
onNavigateToFile?.()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -274,7 +282,7 @@ const FileTreeItem = ({
|
|||||||
<li
|
<li
|
||||||
className={
|
className={
|
||||||
'group m-0 p-0 border-solid border-0 hover:bg-primary/5 focus-within:bg-primary/5 dark:hover:bg-primary/20 dark:focus-within:bg-primary/20 ' +
|
'group m-0 p-0 border-solid border-0 hover:bg-primary/5 focus-within:bg-primary/5 dark:hover:bg-primary/20 dark:focus-within:bg-primary/20 ' +
|
||||||
(isCurrentFile
|
(isFileOrDirHighlighted || isCurrentFile
|
||||||
? '!bg-primary/10 !text-primary dark:!bg-primary/20 dark:!text-inherit'
|
? '!bg-primary/10 !text-primary dark:!bg-primary/20 dark:!text-inherit'
|
||||||
: '')
|
: '')
|
||||||
}
|
}
|
||||||
@ -311,9 +319,7 @@ const FileTreeItem = ({
|
|||||||
<Disclosure.Button
|
<Disclosure.Button
|
||||||
className={
|
className={
|
||||||
' group border-none text-sm rounded-none p-0 m-0 flex items-center justify-start w-full py-0.5 hover:text-primary hover:bg-primary/5 dark:hover:text-inherit dark:hover:bg-primary/10' +
|
' group border-none text-sm rounded-none p-0 m-0 flex items-center justify-start w-full py-0.5 hover:text-primary hover:bg-primary/5 dark:hover:text-inherit dark:hover:bg-primary/10' +
|
||||||
(lastDirectoryClicked?.path === fileOrDir.path
|
(isFileOrDirHighlighted ? ' ui-open:bg-primary/10' : '')
|
||||||
? ' ui-open:bg-primary/10'
|
|
||||||
: '')
|
|
||||||
}
|
}
|
||||||
style={{ paddingInlineStart: getIndentationCSS(level) }}
|
style={{ paddingInlineStart: getIndentationCSS(level) }}
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
@ -402,6 +408,8 @@ const FileTreeItem = ({
|
|||||||
onNavigateToFile={onNavigateToFile}
|
onNavigateToFile={onNavigateToFile}
|
||||||
level={level + 1}
|
level={level + 1}
|
||||||
key={level + '-' + child.path}
|
key={level + '-' + child.path}
|
||||||
|
treeSelection={treeSelection}
|
||||||
|
setTreeSelection={setTreeSelection}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
)}
|
)}
|
||||||
@ -626,6 +634,10 @@ export const FileTreeInner = ({
|
|||||||
FileEntry | undefined
|
FileEntry | undefined
|
||||||
>(undefined)
|
>(undefined)
|
||||||
|
|
||||||
|
const [treeSelection, setTreeSelection] = useState<FileEntry | undefined>(
|
||||||
|
loaderData.file
|
||||||
|
)
|
||||||
|
|
||||||
const onNavigateToFile_ = () => {
|
const onNavigateToFile_ = () => {
|
||||||
// Reset modeling state when navigating to a new file
|
// Reset modeling state when navigating to a new file
|
||||||
onNavigateToFile?.()
|
onNavigateToFile?.()
|
||||||
@ -678,6 +690,7 @@ export const FileTreeInner = ({
|
|||||||
// We're at the root, can't select anything further
|
// We're at the root, can't select anything further
|
||||||
if (!target) return
|
if (!target) return
|
||||||
|
|
||||||
|
setTreeSelection(target)
|
||||||
setLastDirectoryClicked(target)
|
setLastDirectoryClicked(target)
|
||||||
fileSend({
|
fileSend({
|
||||||
type: 'Set selected directory',
|
type: 'Set selected directory',
|
||||||
@ -722,6 +735,8 @@ export const FileTreeInner = ({
|
|||||||
onClickDirectory={onClickDirectory}
|
onClickDirectory={onClickDirectory}
|
||||||
onNavigateToFile={onNavigateToFile_}
|
onNavigateToFile={onNavigateToFile_}
|
||||||
key={fileOrDir.path}
|
key={fileOrDir.path}
|
||||||
|
treeSelection={treeSelection}
|
||||||
|
setTreeSelection={setTreeSelection}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
)}
|
)}
|
||||||
|
Reference in New Issue
Block a user