Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

impr: Quit guard

Rene Wang61fe7849d7e5
diff --git a/src/features/PageEditor/PageEditor.test.tsx b/src/features/PageEditor/PageEditor.test.tsx
new file mode 100644
index 00000000000..1bb31e86bd2
--- /dev/null
+++ b/src/features/PageEditor/PageEditor.test.tsx
@@ -0,0 +1,81 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { useEffect } from 'react';
+import { renderHook } from '@testing-library/react';
+
+describe('PageEditor beforeunload handler', () => {
+ let addEventListenerSpy: ReturnType<typeof vi.spyOn>;
+ let removeEventListenerSpy: ReturnType<typeof vi.spyOn>;
+
+ beforeEach(() => {
+ addEventListenerSpy = vi.spyOn(window, 'addEventListener');
+ removeEventListenerSpy = vi.spyOn(window, 'removeEventListener');
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it('should call preventDefault when isDirty is true', () => {
+ const preventDefaultMock = vi.fn();
+ const handleBeforeUnload = vi.fn((e: BeforeUnloadEvent) => {
+ const isDirty = true;
+ if (isDirty) {
+ e.preventDefault();
+ preventDefaultMock();
+ }
+ });
+
+ window.addEventListener('beforeunload', handleBeforeUnload);
+
+ const event = new Event('beforeunload') as BeforeUnloadEvent;
+ window.dispatchEvent(event);
+
+ expect(handleBeforeUnload).toHaveBeenCalled();
+ expect(preventDefaultMock).toHaveBeenCalled();
+
+ window.removeEventListener('beforeunload', handleBeforeUnload);
+ });
+
+ it('should not call preventDefault when isDirty is false', () => {
+ const preventDefaultMock = vi.fn();
+ const handleBeforeUnload = vi.fn((e: BeforeUnloadEvent) => {
+ const isDirty = false;
+ if (isDirty) {
+ e.preventDefault();
+ preventDefaultMock();
+ }
+ });
+
+ window.addEventListener('beforeunload', handleBeforeUnload);
+
+ const event = new Event('beforeunload') as BeforeUnloadEvent;
+ window.dispatchEvent(event);
+
+ expect(handleBeforeUnload).toHaveBeenCalled();
+ expect(preventDefaultMock).not.toHaveBeenCalled();
+
+ window.removeEventListener('beforeunload', handleBeforeUnload);
+ });
+
+ it('should add and remove event listener through useEffect', () => {
+ const { unmount } = renderHook(() =>
+ useEffect(() => {
+ const handleBeforeUnload = (e: BeforeUnloadEvent) => {
+ e.preventDefault();
+ };
+
+ window.addEventListener('beforeunload', handleBeforeUnload);
+
+ return () => {
+ window.removeEventListener('beforeunload', handleBeforeUnload);
+ };
+ }, []),
+ );
+
+ expect(addEventListenerSpy).toHaveBeenCalledWith('beforeunload', expect.any(Function));
+
+ unmount();
+
+ expect(removeEventListenerSpy).toHaveBeenCalledWith('beforeunload', expect.any(Function));
+ });
+});
diff --git a/src/features/PageEditor/PageEditor.tsx b/src/features/PageEditor/PageEditor.tsx
index eaf5106d96c..96927c0a59b 100644
--- a/src/features/PageEditor/PageEditor.tsx
+++ b/src/features/PageEditor/PageEditor.tsx
@@ -3,7 +3,7 @@
import { BUILTIN_AGENT_SLUGS } from '@lobechat/builtin-agents';
import { Flexbox } from '@lobehub/ui';
import { cssVar } from 'antd-style';
-import { type FC, memo } from 'react';
+import { type FC, memo, useEffect } from 'react';
import Loading from '@/components/Loading/BrandTextLoading';
import WideScreenContainer from '@/features/WideScreenContainer';
@@ -33,11 +33,30 @@ interface PageEditorProps {
const PageEditorCanvas = memo(() => {
const editor = usePageEditorStore((s) => s.editor);
const flushSave = usePageEditorStore((s) => s.flushSave);
+ const isDirty = usePageEditorStore((s) => s.isDirty);
// Register Files scope and save document hotkey
useRegisterFilesHotkeys();
useSaveDocumentHotkey(flushSave);
+ // Warn user before leaving page with unsaved changes
+ useEffect(() => {
+ const handleBeforeUnload = (e: BeforeUnloadEvent) => {
+ if (isDirty) {
+ // Prevent default and show browser confirmation dialog
+ e.preventDefault();
+ // Most modern browsers require returnValue to be set
+ e.returnValue = '';
+ }
+ };
+
+ window.addEventListener('beforeunload', handleBeforeUnload);
+
+ return () => {
+ window.removeEventListener('beforeunload', handleBeforeUnload);
+ };
+ }, [isDirty]);
+
return (
<>
<PageTitle />