Skip to content

Commit

Permalink
Fixing directory/file selection logic to deselect folders properly an…
Browse files Browse the repository at this point in the history
…d 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 <[email protected]>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 6, 2024
1 parent 4a4400e commit 3580591
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/components/FileTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ const FileTreeItem = ({
onCreateFolder,
newTreeEntry,
level = 0,
treeSelection,
setTreeSelection,
}: {
parentDir: FileEntry | undefined
project?: IndexLoaderData['project']
Expand All @@ -170,12 +172,15 @@ const FileTreeItem = ({
onCreateFolder: (name: string) => void
newTreeEntry: TreeEntry
level?: number
treeSelection: FileEntry | undefined
setTreeSelection: Dispatch<React.SetStateAction<FileEntry | undefined>>
}) => {
const { send: fileSend, context: fileContext } = useFileContext()
const { onFileOpen, onFileClose } = useLspContext()
const navigate = useNavigate()
const [isConfirmingDelete, setIsConfirmingDelete] = useState(false)
const isCurrentFile = fileOrDir.path === currentFile?.path
const isFileOrDirHighlighted = treeSelection?.path === fileOrDir?.path
const itemRef = useRef(null)

// Since every file or directory gets its own FileTreeItem, we can do this.
Expand Down Expand Up @@ -242,6 +247,8 @@ const FileTreeItem = ({
}

async function handleClick() {
setTreeSelection(fileOrDir)

if (fileOrDir.children !== null) return // Don't open directories

if (fileOrDir.name?.endsWith(FILE_EXT) === false && project?.path) {
Expand All @@ -263,6 +270,7 @@ const FileTreeItem = ({
// Open kcl files
navigate(`${PATHS.FILE}/${encodeURIComponent(fileOrDir.path)}`)
}

onNavigateToFile?.()
}

Expand All @@ -274,7 +282,7 @@ const FileTreeItem = ({
<li
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 ' +
(isCurrentFile
(isFileOrDirHighlighted || isCurrentFile
? '!bg-primary/10 !text-primary dark:!bg-primary/20 dark:!text-inherit'
: '')
}
Expand Down Expand Up @@ -311,9 +319,7 @@ const FileTreeItem = ({
<Disclosure.Button
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' +
(lastDirectoryClicked?.path === fileOrDir.path
? ' ui-open:bg-primary/10'
: '')
(isFileOrDirHighlighted ? ' ui-open:bg-primary/10' : '')
}
style={{ paddingInlineStart: getIndentationCSS(level) }}
onClick={(e) => {
Expand Down Expand Up @@ -402,6 +408,8 @@ const FileTreeItem = ({
onNavigateToFile={onNavigateToFile}
level={level + 1}
key={level + '-' + child.path}
treeSelection={treeSelection}
setTreeSelection={setTreeSelection}
/>
)
)}
Expand Down Expand Up @@ -626,6 +634,10 @@ export const FileTreeInner = ({
FileEntry | undefined
>(undefined)

const [treeSelection, setTreeSelection] = useState<FileEntry | undefined>(
loaderData.file
)

const onNavigateToFile_ = () => {
// Reset modeling state when navigating to a new file
onNavigateToFile?.()
Expand Down Expand Up @@ -678,6 +690,7 @@ export const FileTreeInner = ({
// We're at the root, can't select anything further
if (!target) return

setTreeSelection(target)
setLastDirectoryClicked(target)
fileSend({
type: 'Set selected directory',
Expand Down Expand Up @@ -722,6 +735,8 @@ export const FileTreeInner = ({
onClickDirectory={onClickDirectory}
onNavigateToFile={onNavigateToFile_}
key={fileOrDir.path}
treeSelection={treeSelection}
setTreeSelection={setTreeSelection}
/>
)
)}
Expand Down

0 comments on commit 3580591

Please sign in to comment.