Files
element-call/src/useLocationNavigation.ts
T

37 lines
893 B
TypeScript
Raw Normal View History

2023-01-03 16:55:26 +00:00
/*
Copyright 2022-2024 New Vector Ltd.
2023-01-03 16:55:26 +00: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
*/
import { useEffect } from "react";
import { useHistory } from "react-router-dom";
2022-07-30 10:00:34 +02:00
export function useLocationNavigation(enabled = false): void {
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;
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;
});
}
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
if (unblock) {
unblock();
}
};
}, [history, enabled]);
}