Skip to content

Commit

Permalink
Confirm before unload if unsaved changes
Browse files Browse the repository at this point in the history
  • Loading branch information
WardBrian committed Jul 30, 2024
1 parent c78aa2c commit 48ad4f6
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions gui/src/app/Project/ProjectContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { initialDataModel, ProjectDataModel } from "@SpCore/ProjectDataModel";
import {
initialDataModel,
modelHasUnsavedChanges,
ProjectDataModel,
} from "@SpCore/ProjectDataModel";
import {
fetchRemoteProject,
fromQueryParams,
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 48ad4f6

Please sign in to comment.