1 "use client"
2
3 import React, {
4 createContext,
5 useContext,
6 useEffect,
7 useState,
8 useMemo,
9 useRef,
10 } from "react"
11 import { BadgeProps } from "../../components/Badge"
12 import { Modal } from "../../components/Modal"
13 import { Search, SearchProps } from "../../components/Search"
14 import {
15 liteClient as algoliasearch,
16 LiteClient as SearchClient,
17 } from "algoliasearch/lite"
18 import clsx from "clsx"
19 // @ts-expect-error can't install the types package because it doesn't support React v19
20 import { CSSTransition, SwitchTransition } from "react-transition-group"
21
22 export type SearchCommand = {
23 name: string
24 component?: React.ReactNode
25 action?: () => void
26 icon?: React.ReactNode
27 title: string
28 badge?: BadgeProps
29 }
30
31 export type SearchContextType = {
32 isOpen: boolean
33 setIsOpen: React.Dispatch<React.SetStateAction<boolean>>
34 searchClient: SearchClient
35 commands: SearchCommand[]
36 command: SearchCommand | null
37 setCommand: React.Dispatch<React.SetStateAction<SearchCommand | null>>
38 setCommands: React.Dispatch<React.SetStateAction<SearchCommand[]>>
39 modalRef: React.RefObject<HTMLDialogElement | null>
40 indices: AlgoliaIndex[]
41 selectedIndex: string
42 setSelectedIndex: (value: string) => void
43 }
44
45 const SearchContext = createContext<SearchContextType | null>(null)
46
47 export type AlgoliaIndex = {
48 value: string
49 title: string
50 }
51
52 export type AlgoliaProps = {
53 appId: string
54 apiKey: string
55 mainIndexName: string
56 }
57
58 export type SearchProviderProps = {
59 children: React.ReactNode
60 indices: AlgoliaIndex[]
61 defaultIndex: string
62 algolia: AlgoliaProps
63 searchProps: Omit<SearchProps, "algolia">
64 commands?: SearchCommand[]
65 modalClassName?: string
66 }
67
68 export const SearchProvider = ({
69 children,
70 defaultIndex: initialDefaultIndex,
71 searchProps,
72 algolia,
73 commands: initialCommands = [],
74 modalClassName,
75 indices,
76 }: SearchProviderProps) => {
77 const [isOpen, setIsOpen] = useState(false)
78 const [selectedIndex, setSelectedIndex] =
79 useState<string>(initialDefaultIndex)
80 const [commands, setCommands] = useState<SearchCommand[]>(initialCommands)
81 const [command, setCommand] = useState<SearchCommand | null>(null)
82
83 const modalRef = useRef<HTMLDialogElement | null>(null)
84
85 const searchClient: SearchClient = useMemo(
86 () => algoliasearch(algolia.appId, algolia.apiKey),
87 [algolia.appId, algolia.apiKey]
88 )
89
90 useEffect(() => {
91 if (initialDefaultIndex !== selectedIndex) {
92 setSelectedIndex(initialDefaultIndex)
93 }
94 }, [initialDefaultIndex])
95
96 const componentWrapperRef = useRef(null)
97
98 useEffect(() => {
99 command?.action?.()
100 }, [command])
101
102 return (
103 <SearchContext.Provider
104 value={{
105 isOpen,
106 setIsOpen,
107 searchClient,
108 commands,
109 command,
110 setCommand,
111 modalRef,
112 setCommands,
113 indices,
114 selectedIndex,
115 setSelectedIndex,
116 }}
117 >
118 {children}
119 <Modal
120 contentClassName={clsx(
121 "!p-0 overflow-hidden relative h-full",
122 "flex flex-col justify-between"
123 )}
124 modalContainerClassName="!h-[95%] max-h-[95%] md:!h-[480px] md:max-h-[480px]"
125 open={isOpen}
126 onClose={() => setIsOpen(false)}
127 passedRef={modalRef}
128 className={modalClassName}
129 >
130 <SwitchTransition>
131 <CSSTransition
132 classNames={{
133 enter:
134 command === null || !command.component
135 ? "animate-fadeInLeft animate-fast"
136 : "animate-fadeInRight animate-fast",
137 exit:
138 command === null || !command.component
139 ? "animate-fadeOutLeft animate-fast"
140 : "animate-fadeOutRight animate-fast",
141 }}
142 timeout={250}
143 key={command?.component ? command.name : "search"}
144 nodeRef={componentWrapperRef}
145 >
146 <div ref={componentWrapperRef} className="h-full">
147 {!command?.component && (
148 <Search {...searchProps} algolia={algolia} />
149 )}
150 {command?.component}
151 </div>
152 </CSSTransition>
153 </SwitchTransition>
154 </Modal>
155 </SearchContext.Provider>
156 )
157 }
158
159 export const useSearch = (): SearchContextType => {
160 const context = useContext(SearchContext)
161
162 if (!context) {
163 throw new Error("useSearch must be used inside a SearchProvider")
164 }
165
166 return context
167 }