2022-02-04 00:56:13 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
import { useHistory } from "react-router-dom";
|
|
|
|
|
2022-07-30 08:00:34 +00:00
|
|
|
export function useLocationNavigation(enabled = false): void {
|
2022-02-04 00:56:13 +00:00
|
|
|
const history = useHistory();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let unblock;
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
unblock = history.block((tx) => {
|
|
|
|
const url = new URL(tx.pathname, window.location.href);
|
|
|
|
url.search = tx.search;
|
|
|
|
url.hash = tx.hash;
|
2022-07-30 08:00:34 +00:00
|
|
|
window.location.href = url.href;
|
2022-02-04 00:56:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (unblock) {
|
|
|
|
unblock();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [history, enabled]);
|
|
|
|
}
|