fix: last known index so you can swap file/folder when creating in the same location

This commit is contained in:
Kevin
2025-06-26 13:12:57 -05:00
parent 2fd87fab31
commit d867d825c9

View File

@ -104,6 +104,7 @@ export const ProjectExplorer = ({
const [contextMenuRow, setContextMenuRow] = const [contextMenuRow, setContextMenuRow] =
useState<FileExplorerEntry | null>(null) useState<FileExplorerEntry | null>(null)
const [isRenaming, setIsRenaming] = useState<boolean>(false) const [isRenaming, setIsRenaming] = useState<boolean>(false)
const lastIndexBeforeNothing = useRef<number>(-2)
const fileExplorerContainer = useRef<HTMLDivElement | null>(null) const fileExplorerContainer = useRef<HTMLDivElement | null>(null)
const projectExplorerRef = useRef<HTMLDivElement | null>(null) const projectExplorerRef = useRef<HTMLDivElement | null>(null)
@ -129,7 +130,7 @@ export const ProjectExplorer = ({
return return
} }
const row = rowsToRenderRef.current[activeIndexRef.current] || null const row = rowsToRenderRef.current[activeIndexRef.current] || rowsToRenderRef.current[lastIndexBeforeNothing.current] || null
setFakeRow({ entry: row, isFile: true }) setFakeRow({ entry: row, isFile: true })
if (row?.key) { if (row?.key) {
// If the file tree had the folder opened make the new one open. // If the file tree had the folder opened make the new one open.
@ -143,7 +144,7 @@ export const ProjectExplorer = ({
if (createFolderPressed <= 0 || readOnly) { if (createFolderPressed <= 0 || readOnly) {
return return
} }
const row = rowsToRenderRef.current[activeIndexRef.current] || null const row = rowsToRenderRef.current[activeIndexRef.current] || rowsToRenderRef.current[lastIndexBeforeNothing.current] || null
setFakeRow({ entry: row, isFile: false }) setFakeRow({ entry: row, isFile: false })
if (row?.key) { if (row?.key) {
// If the file tree had the folder opened make the new one open. // If the file tree had the folder opened make the new one open.
@ -203,6 +204,7 @@ export const ProjectExplorer = ({
setIsRenaming(false) setIsRenaming(false)
} }
// gotcha: sync state // gotcha: sync state
openedRowsRef.current = openedRows openedRowsRef.current = openedRows
activeIndexRef.current = activeIndex activeIndexRef.current = activeIndex
@ -545,6 +547,9 @@ export const ProjectExplorer = ({
projectExplorerRef.current && projectExplorerRef.current &&
!path.includes(projectExplorerRef.current) !path.includes(projectExplorerRef.current)
) { ) {
if (activeIndexRef.current > 0) {
lastIndexBeforeNothing.current = activeIndexRef.current
}
setActiveIndex(NOTHING_IS_SELECTED) setActiveIndex(NOTHING_IS_SELECTED)
} }
} }