17 lines
455 B
TypeScript
17 lines
455 B
TypeScript
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]);
|
|
}
|