Repository navigation
Expand file tree
/
Copy pathuseSearch.js
More file actions
105 lines (92 loc) · 2.48 KB
/
Copy pathuseSearch.js
File metadata and controls
105 lines (92 loc) · 2.48 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import React from 'react'
import { createPortal } from 'react-dom'
import Router from 'next/router'
import Head from 'next/head'
import Link from 'next/link'
import { useDocSearchKeyboardEvents } from '@docsearch/react'
import { siteConfig } from 'siteConfig'
const SearchContext = React.createContext()
let DocSearchModal = null
export const useSearch = () => React.useContext(SearchContext)
export function SearchProvider({
children,
searchParameters = {
hitsPerPage: 5,
},
}) {
const [isShowing, setIsShowing] = React.useState(false)
const onOpen = React.useCallback(function onOpen() {
function importDocSearchModalIfNeeded() {
if (DocSearchModal) {
return Promise.resolve()
}
return import('@docsearch/react/modal').then(
({ DocSearchModal: Modal }) => (DocSearchModal = Modal)
)
}
importDocSearchModalIfNeeded().then(() => {
setIsShowing(true)
})
}, [])
const onClose = React.useCallback(() => setIsShowing(false), [])
useDocSearchKeyboardEvents({
isOpen: isShowing,
onOpen,
onClose,
})
const options = {
appId: siteConfig.algolia.appId,
apiKey: siteConfig.algolia.apiKey,
indexName: siteConfig.algolia.indexName,
renderModal: true,
}
return (
<>
<Head>
<link
key="algolia"
rel="preconnect"
href={`https://${options.appId}-dsn.algolia.net`}
crossOrigin="true"
/>
</Head>
<SearchContext.Provider value={{ DocSearchModal, onOpen }}>
{children}
</SearchContext.Provider>
{isShowing &&
createPortal(
<DocSearchModal
{...options}
searchParameters={searchParameters}
onClose={onClose}
navigator={{
navigate({ suggestionUrl }) {
Router.push(suggestionUrl)
},
}}
transformItems={items => {
return items.map(item => {
const url = new URL(item.url)
return {
...item,
url: item.url
.replace(url.origin, '')
.replace('#__next', '')
.replace('/docs/#', '/docs/overview#'),
}
})
}}
hitComponent={Hit}
/>,
document.body
)}
</>
)
}
function Hit({ hit, children }) {
return (
<Link href={hit.url.replace()}>
<a>{children}</a>
</Link>
)
}