diff --git a/src/_node/file/handlers/handlers.ts b/src/_node/file/handlers/handlers.ts index 6f72d8b0..10df85a0 100644 --- a/src/_node/file/handlers/handlers.ts +++ b/src/_node/file/handlers/handlers.ts @@ -3,7 +3,12 @@ import * as parse5 from "parse5"; import { RainbowAppName } from "@_constants/global"; import { RootNodeUid } from "@_constants/main"; -import { TFileParserResponse, TNodeUid } from "../../"; +import { + TFileNodeData, + TFileNodeTreeData, + TFileParserResponse, + TNodeUid, +} from "../../"; import { THtmlDomNode, THtmlNode, @@ -162,3 +167,27 @@ export const fileHandlers: { } = { html: parseHtml, }; + +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?`; + if (!window.confirm(message)) { + return; + } +}; + +export const confirmFileChanges = (fileTree: TFileNodeTreeData) => { + if (fileTree) { + // confirm files' changes + let hasChangedFile = false; + for (let x in fileTree) { + const _file = fileTree[x]; + const _fileData = _file.data as TFileNodeData; + if (_file && _fileData.changed) { + hasChangedFile = true; + } + } + if (hasChangedFile) { + triggerFileChangeAlert(); + } + } +}; diff --git a/src/components/main/actionsPanel/workspaceTreeView/hooks/useNodeActionsHandler.ts b/src/components/main/actionsPanel/workspaceTreeView/hooks/useNodeActionsHandler.ts index 3cedd762..71dc66b7 100644 --- a/src/components/main/actionsPanel/workspaceTreeView/hooks/useNodeActionsHandler.ts +++ b/src/components/main/actionsPanel/workspaceTreeView/hooks/useNodeActionsHandler.ts @@ -4,7 +4,12 @@ import { TreeItem } from "react-complex-tree"; import { useDispatch } from "react-redux"; import { RednerableFileTypes, RootNodeUid, TmpNodeUid } from "@_constants/main"; -import { createDirectory, TFileNodeData, writeFile } from "@_node/file"; +import { + createDirectory, + TFileNodeData, + triggerFileChangeAlert, + writeFile, +} from "@_node/file"; import { getValidNodeUids } from "@_node/helpers"; import { TNode, TNodeTreeData, TNodeUid } from "@_node/types"; import { MainContext } from "@_redux/main"; @@ -352,11 +357,7 @@ export const useNodeActionsHandler = ( }); if (hasChangedFile) { - const message = `Your changes will be lost if you don't save them. Are you sure you want to continue without saving?`; - - if (!window.confirm(message)) { - return; - } + triggerFileChangeAlert(); } addRunningActions(["fileTreeView-move"]); diff --git a/src/pages/main/MainPage.tsx b/src/pages/main/MainPage.tsx index 10472009..545d3cbc 100644 --- a/src/pages/main/MainPage.tsx +++ b/src/pages/main/MainPage.tsx @@ -24,6 +24,7 @@ import { } from "@_constants/main"; import { buildNohostIDB, + confirmFileChanges, downloadProject, initIDBProject, loadIDBProject, @@ -990,23 +991,7 @@ export default function MainPage() { [importProject], ); const onNew = useCallback(async () => { - if (fileTree) { - // confirm if ffTree is changed - let hasChangedFile = false; - for (let x in fileTree) { - const _file = fileTree[x]; - const _fileData = _file.data as TFileNodeData; - if (_file && _fileData.changed) { - hasChangedFile = true; - } - } - if (hasChangedFile) { - const message = `Your changes will be lost if you don't save them. Are you sure you want to continue without saving?`; - if (!window.confirm(message)) { - return; - } - } - } + confirmFileChanges(fileTree); dispatch(setDoingFileAction(true)); try { @@ -1018,23 +1003,7 @@ export default function MainPage() { dispatch(setDoingFileAction(false)); }, [onImportProject, fileTree]); const onOpen = useCallback(async () => { - if (fileTree) { - // confirm files' changes - let hasChangedFile = false; - for (let x in fileTree) { - const _file = fileTree[x]; - const _fileData = _file.data as TFileNodeData; - if (_file && _fileData.changed) { - hasChangedFile = true; - } - } - if (hasChangedFile) { - const message = `Your changes will be lost if you don't save them. Are you sure you want to continue without saving?`; - if (!window.confirm(message)) { - return; - } - } - } + confirmFileChanges(fileTree); dispatch(setDoingFileAction(true)); try { diff --git a/src/pages/main/processor/helpers.ts b/src/pages/main/processor/helpers.ts index 22e15eab..f6aea0d8 100644 --- a/src/pages/main/processor/helpers.ts +++ b/src/pages/main/processor/helpers.ts @@ -68,6 +68,7 @@ export const getNeedToExpandNodeUids = ( const _expandedItems: TNodeUid[] = []; selectedNodeUids.map((uid) => { let node = validNodeTree[uid]; + if (!node) return; while (node.uid !== RootNodeUid) { _expandedItems.push(node.uid); node = validNodeTree[node.parentUid as TNodeUid];