mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
29 lines
781 B
TypeScript
29 lines
781 B
TypeScript
|
import { useCallback, useEffect, useState } from "react";
|
||
|
|
||
|
const useReloadConfirmations = (message?: string) => {
|
||
|
const [showAlert, setShowAlert] = useState(false);
|
||
|
|
||
|
const handleBeforeUnload = useCallback(
|
||
|
(event: BeforeUnloadEvent) => {
|
||
|
event.preventDefault();
|
||
|
event.returnValue = "";
|
||
|
return message ?? "Are you sure you want to leave?";
|
||
|
},
|
||
|
[message]
|
||
|
);
|
||
|
|
||
|
useEffect(() => {
|
||
|
if (!showAlert) {
|
||
|
window.removeEventListener("beforeunload", handleBeforeUnload);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
window.addEventListener("beforeunload", handleBeforeUnload);
|
||
|
return () => window.removeEventListener("beforeunload", handleBeforeUnload);
|
||
|
}, [handleBeforeUnload, showAlert]);
|
||
|
|
||
|
return { setShowAlert };
|
||
|
};
|
||
|
|
||
|
export default useReloadConfirmations;
|