|
1
|
"use client" |
|
2
|
|
|
3
|
import { usePathname } from "next/navigation" |
|
4
|
import { useIsBrowser } from "../../providers/BrowserProvider" |
|
5
|
import { useSiteConfig } from "../../providers/SiteConfig" |
|
6
|
import { useMemo } from "react" |
|
7
|
|
|
8
|
type useHeadingUrlProps = { |
|
9
|
id: string |
|
10
|
} |
|
11
|
|
|
12
|
export const useHeadingUrl = ({ id }: useHeadingUrlProps) => { |
|
13
|
const { isBrowser } = useIsBrowser() |
|
14
|
const { |
|
15
|
config: { basePath }, |
|
16
|
} = useSiteConfig() |
|
17
|
const pathname = usePathname() |
|
18
|
const headingUrl = useMemo(() => { |
|
19
|
const hash = `#${id}` |
|
20
|
if (!isBrowser) { |
|
21
|
return hash |
|
22
|
} |
|
23
|
|
|
24
|
const url = `${window.location.origin}${basePath}${pathname}`.replace( |
|
25
|
/\/$/, |
|
26
|
"" |
|
27
|
) |
|
28
|
|
|
29
|
return `${url}${hash}` |
|
30
|
}, [id, isBrowser, pathname]) |
|
31
|
|
|
32
|
return headingUrl |
|
33
|
} |