saleor-dashboard/src/hooks/useNavigator.ts
Michał Droń d5c9a3dae8
Add trailing commas (#2062)
* Require trailing commas

* Add trailing commas

* Add trailing commas in testUtils dir

* Add trailing commas
2022-06-21 11:36:55 +02:00

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;