overleaf/services/web/frontend/js/shared/hooks/use-location.ts
Alf Eaton f375362894 Always use mockable location methods (#11929)
* Always use mockable location methods
* Add eslint rules for window.location calls/assignment
* Add useLocation hook
* Update tests

GitOrigin-RevId: eafb846db89f884a7a9a8570cce7745be605152c
2023-03-17 09:05:21 +00:00

25 lines
606 B
TypeScript

import { useCallback, useMemo } from 'react'
import useIsMounted from './use-is-mounted'
export const useLocation = () => {
const isMounted = useIsMounted()
const assign = useCallback(
url => {
if (isMounted.current) {
// eslint-disable-next-line no-restricted-syntax
window.location.assign(url)
}
},
[isMounted]
)
const reload = useCallback(() => {
if (isMounted.current) {
// eslint-disable-next-line no-restricted-syntax
window.location.reload()
}
}, [isMounted])
return useMemo(() => ({ assign, reload }), [assign, reload])
}