-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathuseUpdateNavigatingHref.ts
38 lines (34 loc) · 1.14 KB
/
useUpdateNavigatingHref.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { useRouter } from 'next/router'
import { useCallback } from 'react'
import { useRecoilState } from 'recoil'
import { navigatingToHrefAtom } from '@dao-dao/state/recoil'
// This hook abstracts away the logic of updating the global URL navigation
// loading state which is used by stateful components to indicate when a
// navigation event is occurring.
export const useUpdateNavigatingHref = () => {
const router = useRouter()
const [navigatingToHref, setNavigatingToHref] =
useRecoilState(navigatingToHrefAtom)
const updateNavigatingHref = useCallback(
(href: string | undefined) => {
// If not on destination page, set navigating state. If already there or
// href is invalid, do nothing.
if (
href &&
// Make sure not only the hash is changing.
router.asPath.split('#')[0] !== href.split('#')[0] &&
// Don't set navigating if remote.
!href.startsWith('http')
) {
setNavigatingToHref(href)
} else {
setNavigatingToHref(undefined)
}
},
[router.asPath, setNavigatingToHref]
)
return {
navigatingToHref,
updateNavigatingHref,
}
}