Skip to content

Commit 857fe15

Browse files
authored
docs: use DocSearch instead of own component (#12553)
1 parent 6a72f3d commit 857fe15

5 files changed

Lines changed: 266 additions & 441 deletions

File tree

docs/components/DocSearch/hit.tsx

Lines changed: 0 additions & 62 deletions
This file was deleted.

docs/components/DocSearch/searchInput.tsx

Lines changed: 0 additions & 105 deletions
This file was deleted.
Lines changed: 9 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,15 @@
1-
import { useEffect, useRef, useState } from "react"
2-
import algoliasearch from "algoliasearch/lite"
3-
import { InstantSearch, Hits, useInstantSearch } from "react-instantsearch"
4-
//import { InstantSearchNext } from "react-instantsearch-nextjs"
5-
import { CustomSearchBox } from "./searchInput"
6-
import Hit from "./hit"
1+
import { DocSearch } from "@docsearch/react"
72

8-
const algoliaClient = algoliasearch(
9-
process.env.NEXT_PUBLIC_ALGOLIA_APP_ID!,
10-
process.env.NEXT_PUBLIC_ALGOLIA_KEY!
11-
)
12-
13-
const searchClient = {
14-
...algoliaClient,
15-
search(requests: any) {
16-
if (requests.every(({ params }) => !params.query)) {
17-
return Promise.resolve({
18-
results: requests.map(() => ({
19-
hits: [],
20-
nbHits: 0,
21-
nbPages: 0,
22-
page: 0,
23-
processingTimeMS: 0,
24-
hitsPerPage: 0,
25-
exhaustiveNbHits: false,
26-
query: "",
27-
params: "",
28-
})),
29-
})
30-
}
31-
return algoliaClient.search(requests)
32-
},
33-
}
34-
35-
export default function Wrapper() {
36-
const docSearchRef = useRef<HTMLDivElement>(null)
37-
const [isSearchHitsVisible, setIsSearchHitsVisible] = useState(false)
38-
39-
useEffect(() => {
40-
function ctrlKHandler(e: KeyboardEvent) {
41-
if (e.repeat || e.target instanceof HTMLInputElement) return
42-
if (e.ctrlKey && e.key === "k") {
43-
e.preventDefault()
44-
document
45-
.querySelector<HTMLInputElement>('input[type="search"]')
46-
?.focus()
47-
}
48-
}
49-
50-
function clickSearchBoxOutsideHandler(event: MouseEvent) {
51-
setIsSearchHitsVisible(
52-
Boolean(
53-
event.target instanceof Node &&
54-
docSearchRef.current?.contains(event.target)
55-
)
56-
)
57-
}
58-
59-
window.addEventListener("keydown", ctrlKHandler)
60-
window.addEventListener("mousedown", clickSearchBoxOutsideHandler)
61-
62-
return () => {
63-
window.removeEventListener("keydown", ctrlKHandler)
64-
window.removeEventListener("mousedown", clickSearchBoxOutsideHandler)
65-
}
66-
}, [])
3+
import "@docsearch/css"
674

5+
function App() {
686
return (
69-
<div
70-
ref={docSearchRef}
71-
className="relative [aside_&]:w-full max-md:[nav_&]:hidden"
72-
>
73-
<InstantSearch
74-
indexName="next-auth"
75-
// @ts-expect-error
76-
searchClient={searchClient}
77-
>
78-
<CustomSearchBox />
79-
{isSearchHitsVisible && (
80-
<NoResultsBoundary>
81-
<Hits
82-
hitComponent={Hit}
83-
className="fixed left-2 top-28 z-50 mt-[50px] max-h-[calc(100dvh_-_120px)] w-[calc(100vw_-_16px)] overflow-y-auto rounded-md bg-neutral-100 shadow-lg md:absolute md:left-auto md:right-0 md:top-12 md:mt-auto md:w-96 dark:bg-neutral-800 [&>ol]:flex [&>ol]:flex-col [&>ol]:divide-y [&>ol]:divide-neutral-400/30 [&>ol]:dark:divide-neutral-900/50"
84-
/>
85-
</NoResultsBoundary>
86-
)}
87-
</InstantSearch>
88-
</div>
7+
<DocSearch
8+
appId={process.env.NEXT_PUBLIC_ALGOLIA_APP_ID!}
9+
indexName="next-auth"
10+
apiKey={process.env.NEXT_PUBLIC_ALGOLIA_KEY!}
11+
/>
8912
)
9013
}
9114

92-
function NoResultsBoundary({ children }) {
93-
const { indexUiState, results } = useInstantSearch()
94-
95-
if (
96-
indexUiState.query !== undefined &&
97-
!results.__isArtificial &&
98-
results.nbHits === 0
99-
) {
100-
return (
101-
<div className="fixed left-2 top-28 z-50 mt-[50px] max-h-[calc(100dvh_-_120px)] w-[calc(100vw_-_16px)] overflow-y-auto rounded-md bg-neutral-100 p-2 text-center shadow-md md:absolute md:left-auto md:right-0 md:top-12 md:mt-auto md:w-96 dark:bg-neutral-800 [&>ol]:flex [&>ol]:flex-col [&>ol]:divide-y [&>ol]:divide-neutral-400/30 [&>ol]:dark:divide-neutral-900/50">
102-
No Results
103-
</div>
104-
)
105-
}
106-
107-
if (indexUiState.query === undefined) {
108-
return null
109-
}
110-
111-
return children
112-
}
15+
export default App

docs/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"homepage": "https://authjs.dev",
2626
"dependencies": {
2727
"@ariakit/react": "^0.4.13",
28+
"@docsearch/react": "3",
2829
"@inkeep/widgets": "^0.2.289",
2930
"@next/third-parties": "^14.2.15",
3031
"@radix-ui/react-accordion": "^1.2.1",
@@ -39,9 +40,7 @@
3940
"nextra": "3.0.15",
4041
"nextra-theme-docs": "3.0.15",
4142
"react": "18.3.1",
42-
"react-dom": "18.3.1",
43-
"react-instantsearch": "^7.13.3",
44-
"react-instantsearch-nextjs": "^0.2.5"
43+
"react-dom": "18.3.1"
4544
},
4645
"devDependencies": {
4746
"@types/node": "20.12.7",

0 commit comments

Comments
 (0)