fix: redirect only on rename, need to have a cleaner path library and variable name

This commit is contained in:
Kevin
2025-06-24 08:49:37 -05:00
parent 9e16737ee2
commit 3817d60aa9
4 changed files with 118 additions and 20 deletions

View File

@ -332,17 +332,42 @@ export const ProjectExplorer = ({
},
})
} else {
// rename
systemIOActor.send({
type: SystemIOMachineEvents.renameFolder,
data: {
requestedFolderName: requestedName,
folderName: name,
absolutePathToParentDirectory: getParentAbsolutePath(
const absolutePathToParentDirectory = getParentAbsolutePath(
row.path
),
},
})
)
const oldPath = window.electron.path.join(
absolutePathToParentDirectory,
name
)
const newPath = window.electron.path.join(
absolutePathToParentDirectory,
requestedName
)
const shouldWeNavigate = file?.path?.startsWith(oldPath)
if (shouldWeNavigate && file && file.path) {
const requestedFileNameWithExtension = parentPathRelativeToProject(file?.path?.replace(oldPath, newPath), applicationProjectDirectory)
systemIOActor.send({
type: SystemIOMachineEvents.renameFolderAndNavigateToFile,
data: {
requestedFolderName: requestedName,
folderName: name,
absolutePathToParentDirectory,
requestedProjectName: project.name,
requestedFileNameWithExtension
},
})
} else {
systemIOActor.send({
type: SystemIOMachineEvents.renameFolder,
data: {
requestedFolderName: requestedName,
folderName: name,
absolutePathToParentDirectory,
},
})
}
// TODO: Gotcha... Set new string open even if it fails?
if (openedRowsRef.current[child.key]) {
// If the file tree had the folder opened make the new one open.
@ -387,14 +412,18 @@ export const ProjectExplorer = ({
},
})
} else {
const requestedAbsoluteFilePathWithExtension = joinOSPaths(getParentAbsolutePath(
row.path
), name)
const requestedAbsoluteFilePathWithExtension = joinOSPaths(
getParentAbsolutePath(row.path),
name
)
// If your router loader is within the file you are renaming then reroute to the new path on disk
// If you are renaming a file you are not loaded into, do not reload!
const shouldWeNavigate = requestedAbsoluteFilePathWithExtension === file?.path
const shouldWeNavigate =
requestedAbsoluteFilePathWithExtension === file?.path
systemIOActor.send({
type: shouldWeNavigate ? SystemIOMachineEvents.renameFileAndNavigateToFile : SystemIOMachineEvents.renameFile,
type: shouldWeNavigate
? SystemIOMachineEvents.renameFileAndNavigateToFile
: SystemIOMachineEvents.renameFile,
data: {
requestedFileNameWithExtension:
fileNameForcedWithOriginalExt,

View File

@ -182,7 +182,17 @@ export const systemIOMachine = setup({
data: {
requestedAbsolutePath: string
}
},
}
| {
type: SystemIOMachineEvents.renameFolderAndNavigateToFile
data: {
requestedFolderName: string
folderName: string
absolutePathToParentDirectory: string
requestedProjectName: string
requestedFileNameWithExtension: string
}
}
},
actions: {
[SystemIOMachineActions.setFolders]: assign({
@ -422,9 +432,11 @@ export const systemIOMachine = setup({
requestedFolderName: string
folderName: string
absolutePathToParentDirectory: string
requestedProjectName?: string
requestedFileNameWithExtension?: string
}
}) => {
return { message: '', folderName: '', requestedFolderName: '' }
return { message: '', folderName: '', requestedFolderName: '', requestedProjectName: '', requestedFileNameWithExtension:''}
}
),
[SystemIOMachineActors.renameFile]: fromPromise(
@ -590,6 +602,9 @@ export const systemIOMachine = setup({
[SystemIOMachineEvents.renameFileAndNavigateToFile]: {
target: SystemIOMachineStates.renamingFileAndNavigateToFile,
},
[SystemIOMachineEvents.renameFolderAndNavigateToFile]: {
target: SystemIOMachineStates.renamingFolderAndNavigateToFile,
},
},
},
[SystemIOMachineStates.readingFolders]: {
@ -1057,8 +1072,6 @@ export const systemIOMachine = setup({
? event.output.filePathWithExtensionRelativeToProject
: event.output.filePathWithExtensionRelativeToProject +
'.kcl'
console.log(file, event.output.projectName)
return {
project: event.output.projectName,
file,
@ -1074,5 +1087,53 @@ export const systemIOMachine = setup({
},
},
},
[SystemIOMachineStates.renamingFolderAndNavigateToFile]: {
invoke: {
id: SystemIOMachineActors.renameFolderAndNavigateToFile,
src: SystemIOMachineActors.renameFolder,
input: ({ context, event, self }) => {
assertEvent(event, SystemIOMachineEvents.renameFolderAndNavigateToFile)
return {
context,
requestedFolderName: event.data.requestedFolderName,
folderName: event.data.folderName,
absolutePathToParentDirectory:
event.data.absolutePathToParentDirectory,
rootContext: self.system.get('root').getSnapshot().context,
requestedProjectName: event.data.requestedProjectName,
requestedFileNameWithExtension: event.data.requestedFileNameWithExtension
}
},
onDone: {
target: SystemIOMachineStates.readingFolders,
actions: [
assign({
requestedFileName: ({ event }) => {
assertEvent(
event,
SystemIOMachineEvents.done_renameFolderAndNavigateToFile
)
// Gotcha: file could have an ending of .kcl...
const file =
event.output.requestedFileNameWithExtension.endsWith(
'.kcl'
)
? event.output.requestedFileNameWithExtension
: event.output.requestedFileNameWithExtension +
'.kcl'
return {
project: event.output.requestedProjectName,
file,
}
},
}),
SystemIOMachineActions.toastSuccess],
},
onError: {
target: SystemIOMachineStates.idle,
actions: [SystemIOMachineActions.toastError],
},
},
},
},
})

View File

@ -418,6 +418,8 @@ export const systemIOMachineDesktop = systemIOMachine.provide({
requestedFolderName: string
folderName: string
absolutePathToParentDirectory: string
requestedProjectName?: string
requestedFileNameWithExtension?: string
}
}) => {
const {
@ -454,12 +456,14 @@ export const systemIOMachineDesktop = systemIOMachine.provide({
}
}
window.electron.rename(oldPath, newPath)
window.electron.rename(oldPath, newPath)
return {
message: `Successfully renamed folder "${folderName}" to "${requestedFolderName}"`,
folderName,
requestedFolderName,
requestedProjectName: input.requestedProjectName,
requestedFileNameWithExtension: input.requestedFileNameWithExtension
}
}
),

View File

@ -22,6 +22,7 @@ export enum SystemIOMachineActors {
createBlankFile = 'create blank file',
createBlankFolder = 'create blank folder',
renameFileAndNavigateToFile = 'rename file and navigate to file',
renameFolderAndNavigateToFile = 'rename folder and navigate to file'
}
export enum SystemIOMachineStates {
@ -45,6 +46,7 @@ export enum SystemIOMachineStates {
creatingBlankFile = 'creatingBlankFile',
creatingBlankFolder = 'creatingBlankFolder',
renamingFileAndNavigateToFile = 'renamingFileAndNavigateToFile',
renamingFolderAndNavigateToFile = 'renamingFolderAndNavigateToFile'
}
const donePrefix = 'xstate.done.actor.'
@ -81,6 +83,8 @@ export enum SystemIOMachineEvents {
renameFileAndNavigateToFile = 'rename file and navigate to file',
done_renameFileAndNavigateToFile = donePrefix +
'rename file and navigate to file',
renameFolderAndNavigateToFile = 'rename folder and navigate to file',
done_renameFolderAndNavigateToFile = donePrefix + 'rename folder and navigate to file'
}
export enum SystemIOMachineActions {