|
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" |
7 | 2 |
|
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" |
67 | 4 |
|
| 5 | +function App() { |
68 | 6 | 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 | + /> |
89 | 12 | ) |
90 | 13 | } |
91 | 14 |
|
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 |
0 commit comments