2023-01-03 16:55:26 +00:00
|
|
|
/*
|
2024-09-06 10:22:13 +02:00
|
|
|
Copyright 2022-2024 New Vector Ltd.
|
2023-01-03 16:55:26 +00:00
|
|
|
|
2024-09-06 10:22:13 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
Please see LICENSE in the repository root for full details.
|
2023-01-03 16:55:26 +00:00
|
|
|
*/
|
|
|
|
|
|
2022-02-03 16:56:13 -08:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
import { useHistory } from "react-router-dom";
|
|
|
|
|
|
2022-07-30 10:00:34 +02:00
|
|
|
export function useLocationNavigation(enabled = false): void {
|
2022-02-03 16:56:13 -08:00
|
|
|
const history = useHistory();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-06-30 16:43:28 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
let unblock = undefined;
|
2022-02-03 16:56:13 -08:00
|
|
|
|
|
|
|
|
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 10:00:34 +02:00
|
|
|
window.location.href = url.href;
|
2022-02-03 16:56:13 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-04 11:20:25 -04:00
|
|
|
return (): void => {
|
2023-06-30 16:43:28 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
|
// @ts-ignore
|
2022-02-03 16:56:13 -08:00
|
|
|
if (unblock) {
|
|
|
|
|
unblock();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}, [history, enabled]);
|
|
|
|
|
}
|