diff --git a/src/_node/file/helpers.ts b/src/_node/file/helpers.ts index 13cd623f..1ae4a285 100644 --- a/src/_node/file/helpers.ts +++ b/src/_node/file/helpers.ts @@ -1,4 +1,4 @@ -import { RootNodeUid } from "@_constants/main"; +import { FileChangeAlertMessage, RootNodeUid } from "@_constants/main"; import { TFileHandlerInfoObj, @@ -58,17 +58,16 @@ export const isUnsavedProject = (fileTree: TFileNodeTreeData) => { return false; }; -export const triggerAlert = (msg: string) => { +export const confirmAlert = (msg: string): boolean => { if (!window.confirm(msg)) { - return; + return false; } + return true; }; -export const triggerFileChangeAlert = () => { - const message = `Your changes will be lost if you don't save them. Are you sure you want to continue without saving?`; - triggerAlert(message); -}; -export const confirmFileChanges = (fileTree: TFileNodeTreeData) => { - isUnsavedProject(fileTree) && triggerFileChangeAlert(); +export const confirmFileChanges = (fileTree: TFileNodeTreeData): boolean => { + return isUnsavedProject(fileTree) + ? confirmAlert(FileChangeAlertMessage) + : true; }; export const getFileNameAndExtensionFromFullname = ( diff --git a/src/components/main/actionsPanel/workspaceTreeView/hooks/useNodeActionsHandler.ts b/src/components/main/actionsPanel/workspaceTreeView/hooks/useNodeActionsHandler.ts index 71ce8c19..bc9b932a 100644 --- a/src/components/main/actionsPanel/workspaceTreeView/hooks/useNodeActionsHandler.ts +++ b/src/components/main/actionsPanel/workspaceTreeView/hooks/useNodeActionsHandler.ts @@ -3,13 +3,17 @@ import { useCallback, useContext } from "react"; import { TreeItem } from "react-complex-tree"; import { useDispatch } from "react-redux"; -import { RednerableFileTypes, RootNodeUid, TmpNodeUid } from "@_constants/main"; +import { + FileChangeAlertMessage, + RednerableFileTypes, + RootNodeUid, + TmpNodeUid, +} from "@_constants/main"; import { _createIDBDirectory, TFileNodeData, - triggerAlert, - triggerFileChangeAlert, _writeIDBFile, + confirmAlert, } from "@_node/file"; import { getValidNodeUids } from "@_node/helpers"; import { TNode, TNodeTreeData, TNodeUid } from "@_node/types"; @@ -264,7 +268,7 @@ export const useNodeActionsHandler = ({ const fileData = file.data; if (file && fileData.changed) { const message = `Your changes will be lost if you don't save them. Are you sure you want to continue without saving?`; - triggerAlert(message); + if (!confirmAlert(message)) return; } addTemporaryNodes(file.uid); @@ -360,9 +364,7 @@ export const useNodeActionsHandler = ({ return _file && _fileData.changed; }); - if (hasChangedFile) { - triggerFileChangeAlert(); - } + if (hasChangedFile && !confirmAlert(FileChangeAlertMessage)) return; addRunningActions(["fileTreeView-move"]); diff --git a/src/constants/main.ts b/src/constants/main.ts index d8b3a9dd..b82042b7 100644 --- a/src/constants/main.ts +++ b/src/constants/main.ts @@ -31,5 +31,6 @@ export const ShortDelay = 50; export const NodePathSplitter: string = "?"; +export const FileChangeAlertMessage = `Your changes will be lost if you don't save them. Are you sure you want to continue without saving?`; // ---------------------- export const TmpNodeUid = "tmp:node:uid"; diff --git a/src/pages/main/MainPage.tsx b/src/pages/main/MainPage.tsx index e73e8976..5cdee720 100644 --- a/src/pages/main/MainPage.tsx +++ b/src/pages/main/MainPage.tsx @@ -458,8 +458,12 @@ export default function MainPage() { recentProjectContexts[index]; const projectHandler = recentProjectHandlers[index]; - confirmFileChanges(fileTree); - importProject(projectContext, projectHandler); + + confirmFileChanges(fileTree) && + importProject( + projectContext, + projectHandler, + ); } else if ( (currentCmdkPage === "Add" && command.Group === "Recent") || diff --git a/src/pages/main/hooks/useCmdk.ts b/src/pages/main/hooks/useCmdk.ts index cad87373..1fe15b7c 100644 --- a/src/pages/main/hooks/useCmdk.ts +++ b/src/pages/main/hooks/useCmdk.ts @@ -84,7 +84,7 @@ export const useCmdk = ({ cmdkReferenceData, importProject }: IUseCmdk) => { dispatch(setCmdkOpen(true)); }, [cmdkOpen]); const onNew = useCallback(async () => { - confirmFileChanges(fileTree); + if (!confirmFileChanges(fileTree)) return; dispatch(setDoingFileAction(true)); try { @@ -96,7 +96,7 @@ export const useCmdk = ({ cmdkReferenceData, importProject }: IUseCmdk) => { dispatch(setDoingFileAction(false)); }, [fileTree, importProject]); const onOpen = useCallback(async () => { - confirmFileChanges(fileTree); + if (!confirmFileChanges(fileTree)) return; dispatch(setDoingFileAction(true)); try {