
* Require trailing commas * Add trailing commas * Add trailing commas in testUtils dir * Add trailing commas
42 lines
951 B
TypeScript
42 lines
951 B
TypeScript
import { ExitFormDialogContext } from "@saleor/components/Form/ExitFormDialogProvider";
|
|
import { useContext } from "react";
|
|
import useRouter from "use-react-router";
|
|
|
|
export type UseNavigatorResult = (
|
|
url: string,
|
|
opts?: {
|
|
replace?: boolean;
|
|
preserveQs?: boolean;
|
|
resetScroll?: boolean;
|
|
},
|
|
) => void;
|
|
function useNavigator(): UseNavigatorResult {
|
|
const {
|
|
location: { search },
|
|
history,
|
|
} = useRouter();
|
|
|
|
const { shouldBlockNavigation } = useContext(ExitFormDialogContext);
|
|
|
|
return (
|
|
url: string,
|
|
{ replace = false, preserveQs = false, resetScroll = false } = {},
|
|
) => {
|
|
if (shouldBlockNavigation()) {
|
|
return;
|
|
}
|
|
|
|
const targetUrl = preserveQs ? url + search : url;
|
|
|
|
if (replace) {
|
|
history.replace(targetUrl);
|
|
} else {
|
|
history.push(targetUrl);
|
|
}
|
|
if (resetScroll) {
|
|
window.scrollTo({ behavior: "smooth", top: 0 });
|
|
}
|
|
};
|
|
}
|
|
|
|
export default useNavigator;
|