|
1
|
"use client" |
|
2
|
|
|
3
|
import React, { useEffect, useRef } from "react" |
|
4
|
import { InstantSearch, SearchBox } from "react-instantsearch" |
|
5
|
import clsx from "clsx" |
|
6
|
import { SearchEmptyQueryBoundary } from "./EmptyQueryBoundary" |
|
7
|
import { SearchSuggestions, type SearchSuggestionType } from "./Suggestions" |
|
8
|
import { AlgoliaProps, useSearch } from "@/providers/Search" |
|
9
|
import { SearchHitsWrapper } from "./Hits" |
|
10
|
import { SpinnerLoading } from "@/components/Loading/Spinner" |
|
11
|
import { useSearchNavigation } from "@/hooks/use-search-navigation" |
|
12
|
import { OptionType } from "@/hooks/use-select" |
|
13
|
import { SearchFooter } from "./Footer" |
|
14
|
import { SearchFilters } from "./Filters" |
|
15
|
import { SearchCallout } from "./Callout" |
|
16
|
|
|
17
|
export type SearchProps = { |
|
18
|
algolia: AlgoliaProps |
|
19
|
isLoading?: boolean |
|
20
|
suggestions: SearchSuggestionType[] |
|
21
|
checkInternalPattern?: RegExp |
|
22
|
filterOptions?: OptionType[] |
|
23
|
} |
|
24
|
|
|
25
|
export const Search = ({ |
|
26
|
algolia, |
|
27
|
suggestions, |
|
28
|
isLoading = false, |
|
29
|
checkInternalPattern, |
|
30
|
}: SearchProps) => { |
|
31
|
const { isOpen, searchClient, modalRef } = useSearch() |
|
32
|
const searchBoxRef = useRef<HTMLFormElement>(null) |
|
33
|
|
|
34
|
const focusSearchInput = () => |
|
35
|
searchBoxRef.current?.querySelector("input")?.focus() |
|
36
|
|
|
37
|
useEffect(() => { |
|
38
|
if (isOpen && searchBoxRef.current) { |
|
39
|
focusSearchInput() |
|
40
|
} else if (!isOpen) { |
|
41
|
const focusedItem = modalRef.current?.querySelector( |
|
42
|
":focus" |
|
43
|
) as HTMLElement |
|
44
|
if ( |
|
45
|
focusedItem && |
|
46
|
focusedItem === searchBoxRef.current?.querySelector("input") |
|
47
|
) { |
|
48
|
// remove focus |
|
49
|
focusedItem.blur() |
|
50
|
} |
|
51
|
} |
|
52
|
}, [isOpen]) |
|
53
|
|
|
54
|
useSearchNavigation({ |
|
55
|
getInputElm: () => |
|
56
|
searchBoxRef.current?.querySelector("input") as HTMLInputElement, |
|
57
|
focusInput: focusSearchInput, |
|
58
|
keyboardProps: { |
|
59
|
isLoading, |
|
60
|
}, |
|
61
|
}) |
|
62
|
|
|
63
|
return ( |
|
64
|
<div className="h-full flex flex-col"> |
|
65
|
{/* @ts-expect-error React v19 doesn't see this type as a React element */} |
|
66
|
<InstantSearch |
|
67
|
indexName={algolia.mainIndexName} |
|
68
|
searchClient={searchClient} |
|
69
|
future={{ |
|
70
|
preserveSharedStateOnUnmount: true, |
|
71
|
}} |
|
72
|
insights={true} |
|
73
|
> |
|
74
|
<div className={clsx("bg-medusa-bg-base flex z-[1]")}> |
|
75
|
{/* @ts-expect-error React v19 doesn't see this type as a React element */} |
|
76
|
<SearchBox |
|
77
|
classNames={{ |
|
78
|
root: clsx( |
|
79
|
"h-[57px] w-full md:rounded-t-docs_xl relative border-0 border-solid", |
|
80
|
"border-b border-medusa-border-base", |
|
81
|
"bg-transparent" |
|
82
|
), |
|
83
|
form: clsx("h-full md:rounded-t-docs_xl bg-transparent"), |
|
84
|
input: clsx( |
|
85
|
"w-[calc(100%-40px)] h-full px-docs_1 py-docs_0.75 text-medusa-fg-base", |
|
86
|
"placeholder:text-medusa-fg-muted bg-medusa-bg-base", |
|
87
|
"md:rounded-t-docs_xl text-compact-large sm:text-compact-medium", |
|
88
|
"appearance-none search-cancel:hidden border-0 active:outline-none focus:outline-none" |
|
89
|
), |
|
90
|
submit: clsx("absolute top-[18px] left-docs_1 btn-clear p-0"), |
|
91
|
reset: clsx("absolute top-[18px] right-docs_1", "btn-clear"), |
|
92
|
loadingIndicator: clsx("absolute top-[18px] right-docs_1"), |
|
93
|
}} |
|
94
|
submitIconComponent={() => <></>} |
|
95
|
resetIconComponent={() => ( |
|
96
|
<span className="text-medusa-fg-muted text-compact-small-plus hover:text-medusa-fg-subtle"> |
|
97
|
Clear |
|
98
|
</span> |
|
99
|
)} |
|
100
|
placeholder="Find something..." |
|
101
|
autoFocus |
|
102
|
formRef={searchBoxRef as React.RefObject<HTMLFormElement>} |
|
103
|
loadingIconComponent={() => <SpinnerLoading />} |
|
104
|
/> |
|
105
|
</div> |
|
106
|
<div |
|
107
|
className={clsx( |
|
108
|
"md:flex-initial flex flex-col", |
|
109
|
"h-[calc(100%-75px)] lg:max-h-[calc(100%-100px)] lg:min-h-[calc(100%-100px)]" |
|
110
|
)} |
|
111
|
> |
|
112
|
<SearchFilters /> |
|
113
|
<SearchEmptyQueryBoundary |
|
114
|
fallback={<SearchSuggestions suggestions={suggestions} />} |
|
115
|
> |
|
116
|
<SearchCallout /> |
|
117
|
<SearchHitsWrapper |
|
118
|
configureProps={{}} |
|
119
|
checkInternalPattern={checkInternalPattern} |
|
120
|
/> |
|
121
|
</SearchEmptyQueryBoundary> |
|
122
|
</div> |
|
123
|
</InstantSearch> |
|
124
|
<SearchFooter /> |
|
125
|
</div> |
|
126
|
) |
|
127
|
} |