From 48ad4f65026f62543c90215f5fae0cab8ccc51f5 Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Tue, 30 Jul 2024 19:29:23 +0000 Subject: [PATCH] Confirm before unload if unsaved changes --- gui/src/app/Project/ProjectContextProvider.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/gui/src/app/Project/ProjectContextProvider.tsx b/gui/src/app/Project/ProjectContextProvider.tsx index 27ffb4eb..3a930406 100644 --- a/gui/src/app/Project/ProjectContextProvider.tsx +++ b/gui/src/app/Project/ProjectContextProvider.tsx @@ -1,4 +1,8 @@ -import { initialDataModel, ProjectDataModel } from "@SpCore/ProjectDataModel"; +import { + initialDataModel, + modelHasUnsavedChanges, + ProjectDataModel, +} from "@SpCore/ProjectDataModel"; import { fetchRemoteProject, fromQueryParams, @@ -47,9 +51,13 @@ const ProjectContextProvider: FunctionComponent< useEffect(() => { // as user reloads the page or closes the tab, save state to local storage - const handleBeforeUnload = () => { + const handleBeforeUnload = (e: BeforeUnloadEvent) => { const state = serializeProjectToLocalStorage(data); localStorage.setItem("stan-playground-saved-state", state); + if (modelHasUnsavedChanges(data)) { + e.preventDefault(); + e.returnValue = true; // legacy + } }; window.addEventListener("beforeunload", handleBeforeUnload);