feat: added a twist
This commit is contained in:
16
frontend/src/hooks/useUnsavedGuard.ts
Normal file
16
frontend/src/hooks/useUnsavedGuard.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { useEffect } from "react";
|
||||
import { useEditorStore } from "@/stores/editorStore";
|
||||
|
||||
export function useUnsavedGuard() {
|
||||
const isDirty = useEditorStore((s) => s.isDirty);
|
||||
|
||||
useEffect(() => {
|
||||
const handler = (e: BeforeUnloadEvent) => {
|
||||
if (isDirty) {
|
||||
e.preventDefault();
|
||||
}
|
||||
};
|
||||
window.addEventListener("beforeunload", handler);
|
||||
return () => window.removeEventListener("beforeunload", handler);
|
||||
}, [isDirty]);
|
||||
}
|
||||
Reference in New Issue
Block a user