diff --git a/apps/studio/components/interfaces/Explorer/ExplorerNotebookTabCoordinator.tsx b/apps/studio/components/interfaces/Explorer/ExplorerNotebookTabCoordinator.tsx index 63a15562aa8..ceccee89c71 100644 --- a/apps/studio/components/interfaces/Explorer/ExplorerNotebookTabCoordinator.tsx +++ b/apps/studio/components/interfaces/Explorer/ExplorerNotebookTabCoordinator.tsx @@ -35,6 +35,19 @@ export const ExplorerNotebookTabCoordinator = () => { const queryClient = useQueryClient() const tabs = useContext(TabsStateContext) + useEffect(() => { + const handleBeforeUnload = (event: BeforeUnloadEvent) => { + const hasUnsaved = Object.values(notebooksState.notebooks).some(hasDiscardableChanges) + if (hasUnsaved) { + event.preventDefault() + event.returnValue = true + } + } + + window.addEventListener('beforeunload', handleBeforeUnload) + return () => window.removeEventListener('beforeunload', handleBeforeUnload) + }, []) + useEffect(() => { return tabs.registerTabTypeHandler('notebook', { onClose: (tab) => {