From 590688f1235796b2d740549d11697eda46bcbf9d Mon Sep 17 00:00:00 2001 From: MiniX16 Date: Wed, 12 Nov 2025 17:59:28 +0100 Subject: [PATCH 1/3] Add unsaved-change warning to Home page editor --- client/homebrew/pages/homePage/homePage.jsx | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/client/homebrew/pages/homePage/homePage.jsx b/client/homebrew/pages/homePage/homePage.jsx index 82c5b7084..5f2a3d14b 100644 --- a/client/homebrew/pages/homePage/homePage.jsx +++ b/client/homebrew/pages/homePage/homePage.jsx @@ -53,8 +53,9 @@ const HomePage =(props)=>{ const [isSaving , setIsSaving] = useState(false); const [autoSaveEnabled , setAutoSaveEnable] = useState(false); - const editorRef = useRef(null); - const lastSavedBrew = useRef(_.cloneDeep(props.brew)); + const editorRef = useRef(null); + const lastSavedBrew = useRef(_.cloneDeep(props.brew)); + const unsavedChangesRef = useRef(unsavedChanges); useEffect(()=>{ fetchThemeBundle(setError, setThemeBundle, currentBrew.renderer, currentBrew.theme); @@ -69,13 +70,28 @@ const HomePage =(props)=>{ } }; + const handleBeforeUnload = (e)=>{ + if(unsavedChangesRef.current) { + e.preventDefault(); + e.returnValue = ''; + return ''; + } + }; + + window.addEventListener('beforeunload', handleBeforeUnload); + document.addEventListener('keydown', handleControlKeys); return ()=>{ document.removeEventListener('keydown', handleControlKeys); + window.removeEventListener('beforeunload', handleBeforeUnload); }; }, []); + useEffect(()=>{ + unsavedChangesRef.current = unsavedChanges; + }, [unsavedChanges]); + const save = ()=>{ request.post('/api') .send(currentBrew) From b66625e59d41886c297b0b77469ced9bde419196 Mon Sep 17 00:00:00 2001 From: MiniX16 Date: Thu, 13 Nov 2025 12:16:37 +0100 Subject: [PATCH 2/3] Handle unsaved warning with onbeforeunload --- client/homebrew/pages/homePage/homePage.jsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/client/homebrew/pages/homePage/homePage.jsx b/client/homebrew/pages/homePage/homePage.jsx index 5f2a3d14b..8405f34d3 100644 --- a/client/homebrew/pages/homePage/homePage.jsx +++ b/client/homebrew/pages/homePage/homePage.jsx @@ -78,13 +78,15 @@ const HomePage =(props)=>{ } }; - window.addEventListener('beforeunload', handleBeforeUnload); + const previousBeforeUnload = window.onbeforeunload; + + window.onbeforeunload = handleBeforeUnload; document.addEventListener('keydown', handleControlKeys); return ()=>{ document.removeEventListener('keydown', handleControlKeys); - window.removeEventListener('beforeunload', handleBeforeUnload); + window.onbeforeunload = previousBeforeUnload; }; }, []); From fa9f18075921452a26c0b2e61cc4448e3f5c235f Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Fri, 14 Nov 2025 22:59:34 -0500 Subject: [PATCH 3/3] Modify slightly to follow the existing structure in editPage.jsx for easier merging of these pages later --- client/homebrew/pages/homePage/homePage.jsx | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/client/homebrew/pages/homePage/homePage.jsx b/client/homebrew/pages/homePage/homePage.jsx index 9a779a070..463df333b 100644 --- a/client/homebrew/pages/homePage/homePage.jsx +++ b/client/homebrew/pages/homePage/homePage.jsx @@ -70,23 +70,14 @@ const HomePage =(props)=>{ } }; - const handleBeforeUnload = (e)=>{ - if(unsavedChangesRef.current) { - e.preventDefault(); - e.returnValue = ''; - return ''; - } - }; - - const previousBeforeUnload = window.onbeforeunload; - - window.onbeforeunload = handleBeforeUnload; - document.addEventListener('keydown', handleControlKeys); - + window.onbeforeunload = ()=>{ + if(unsavedChangesRef.current) + return 'You have unsaved changes!'; + }; return ()=>{ document.removeEventListener('keydown', handleControlKeys); - window.onbeforeunload = previousBeforeUnload; + window.onbeforeunload = null; }; }, []);