mirror of
https://github.com/stefanpejcic/openpanel
synced 2025-06-26 18:28:26 +00:00
30 lines
743 B
TypeScript
30 lines
743 B
TypeScript
import { useLocation } from "@tanstack/react-location";
|
|
import { useEffect } from "react";
|
|
|
|
import type { PromptProps } from "@pankod/refine-core";
|
|
|
|
export const Prompt: React.FC<PromptProps> = ({
|
|
message,
|
|
when,
|
|
setWarnWhen,
|
|
}) => {
|
|
const location = useLocation();
|
|
|
|
useEffect(() => {
|
|
if (!when) return;
|
|
|
|
const unblock = location.history.block((transition: any) => {
|
|
if (window.confirm(message)) {
|
|
setWarnWhen?.(false);
|
|
unblock();
|
|
transition.retry();
|
|
} else {
|
|
location.current.pathname = window.location.pathname;
|
|
}
|
|
});
|
|
return unblock;
|
|
}, [when, location, message]);
|
|
|
|
return null;
|
|
};
|