2023-03-16 08:32:48 -04:00
|
|
|
import { useCallback, useMemo } from 'react'
|
|
|
|
import useIsMounted from './use-is-mounted'
|
2024-03-27 07:04:45 -04:00
|
|
|
import { location } from '@/shared/components/location'
|
2023-03-16 08:32:48 -04:00
|
|
|
|
|
|
|
export const useLocation = () => {
|
|
|
|
const isMounted = useIsMounted()
|
|
|
|
|
|
|
|
const assign = useCallback(
|
|
|
|
url => {
|
|
|
|
if (isMounted.current) {
|
2024-03-27 07:04:45 -04:00
|
|
|
location.assign(url)
|
2023-03-16 08:32:48 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[isMounted]
|
|
|
|
)
|
|
|
|
|
2024-05-03 03:46:21 -04:00
|
|
|
const replace = useCallback(
|
|
|
|
url => {
|
|
|
|
if (isMounted.current) {
|
|
|
|
location.replace(url)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[isMounted]
|
|
|
|
)
|
|
|
|
|
2023-03-16 08:32:48 -04:00
|
|
|
const reload = useCallback(() => {
|
|
|
|
if (isMounted.current) {
|
2024-03-27 07:04:45 -04:00
|
|
|
location.reload()
|
2023-03-16 08:32:48 -04:00
|
|
|
}
|
|
|
|
}, [isMounted])
|
|
|
|
|
2024-05-03 03:46:21 -04:00
|
|
|
return useMemo(() => ({ assign, replace, reload }), [assign, replace, reload])
|
2023-03-16 08:32:48 -04:00
|
|
|
}
|