Skip to content

Commit

Permalink
feat: add popup on unsaved changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Arukuen committed Nov 28, 2024
1 parent 4fd12af commit 4a2964e
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/welcome/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,24 @@ const Settings = () => {
}, [ settings ] )

const hasUnsavedChanges = useMemo( () => Object.keys( unsavedChanges ).length > 0, [ unsavedChanges ] )

useEffect( () => {
const handleBeforeUnload = event => {
if ( hasUnsavedChanges ) {
event.preventDefault()
// Most browsers ignore the custom message, but returning a value triggers the dialog
// https://developer.mozilla.org/en-US/docs/Web/API/BeforeUnloadEvent/returnValue
event.returnValue = true
}
}

window.addEventListener( 'beforeunload', handleBeforeUnload )

return () => {
window.removeEventListener( 'beforeunload', handleBeforeUnload )
}
}, [ hasUnsavedChanges ] )

const filteredSearchTree = useMemo( () => {
if ( ! currentSearch ) {
return SEARCH_TREE
Expand Down

0 comments on commit 4a2964e

Please sign in to comment.