Files
micronomicon/frontend/src/hooks/useUnsavedGuard.ts
2026-04-01 00:53:55 +02:00

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]);
}