|
1
|
"use client" |
|
2
|
|
|
3
|
import React, { useEffect, useMemo, useState } from "react" |
|
4
|
import clsx from "clsx" |
|
5
|
import { |
|
6
|
Configure, |
|
7
|
ConfigureProps, |
|
8
|
Index, |
|
9
|
Snippet, |
|
10
|
useHits, |
|
11
|
useInstantSearch, |
|
12
|
} from "react-instantsearch" |
|
13
|
import { SearchNoResult } from "../NoResults" |
|
14
|
import { useSearch } from "@/providers/Search" |
|
15
|
import { Badge } from "@/components/Badge" |
|
16
|
import { Link } from "@/components/Link" |
|
17
|
import { SearchHitGroupName } from "@/components/Search/Hits/GroupName" |
|
18
|
|
|
19
|
export type Hierarchy = "lvl0" | "lvl1" | "lvl2" | "lvl3" | "lvl4" | "lvl5" |
|
20
|
|
|
21
|
export type HitType = { |
|
22
|
hierarchy: { |
|
23
|
lvl0: string | null |
|
24
|
lvl1: string | null |
|
25
|
lvl2: string | null |
|
26
|
lvl3: string | null |
|
27
|
lvl4: string | null |
|
28
|
lvl5: string | null |
|
29
|
} |
|
30
|
_tags: string[] |
|
31
|
url: string |
|
32
|
url_without_anchor: string |
|
33
|
type?: "lvl1" | "lvl2" | "lvl3" | "lvl4" | "lvl5" | "content" |
|
34
|
content?: string |
|
35
|
__position: number |
|
36
|
__queryID?: string |
|
37
|
objectID: string |
|
38
|
description?: string |
|
39
|
integration_vendor?: string |
|
40
|
} |
|
41
|
|
|
42
|
export type GroupedHitType = { |
|
43
|
[k: string]: HitType[] |
|
44
|
} |
|
45
|
|
|
46
|
export type SearchHitWrapperProps = { |
|
47
|
configureProps: ConfigureProps |
|
48
|
} & Omit<SearchHitsProps, "indexName" | "setNoResults"> |
|
49
|
|
|
50
|
export type IndexResults = { |
|
51
|
[k: string]: boolean |
|
52
|
} |
|
53
|
|
|
54
|
export const SearchHitsWrapper = ({ |
|
55
|
configureProps, |
|
56
|
...rest |
|
57
|
}: SearchHitWrapperProps) => { |
|
58
|
const { status } = useInstantSearch() |
|
59
|
const { selectedIndex, indices } = useSearch() |
|
60
|
const [hasNoResults, setHasNoResults] = useState<IndexResults>({ |
|
61
|
...indices.reduce((acc, index) => { |
|
62
|
acc[index.value] = false |
|
63
|
return acc |
|
64
|
}, {} as IndexResults), |
|
65
|
}) |
|
66
|
const setNoResults = (index: string, value: boolean) => { |
|
67
|
setHasNoResults((prev) => ({ |
|
68
|
...prev, |
|
69
|
[index]: value, |
|
70
|
})) |
|
71
|
} |
|
72
|
const showNoResults = useMemo(() => { |
|
73
|
return Object.values(hasNoResults).every((val) => val) |
|
74
|
}, [hasNoResults]) |
|
75
|
|
|
76
|
return ( |
|
77
|
<div className="overflow-auto px-docs_0.5 flex-1"> |
|
78
|
{status !== "loading" && showNoResults && <SearchNoResult />} |
|
79
|
{indices.map((index) => ( |
|
80
|
<div |
|
81
|
className={clsx(index.value !== selectedIndex && "hidden")} |
|
82
|
key={index.value} |
|
83
|
data-index |
|
84
|
> |
|
85
|
{/* @ts-expect-error React v19 doesn't see this type as a React element */} |
|
86
|
<Index indexName={index.value}> |
|
87
|
{!hasNoResults[index.value] && ( |
|
88
|
<SearchHitGroupName name={index.title} /> |
|
89
|
)} |
|
90
|
<SearchHits |
|
91
|
indexName={index.value} |
|
92
|
setNoResults={setNoResults} |
|
93
|
{...rest} |
|
94
|
/> |
|
95
|
<Configure {...configureProps} /> |
|
96
|
</Index> |
|
97
|
</div> |
|
98
|
))} |
|
99
|
</div> |
|
100
|
) |
|
101
|
} |
|
102
|
|
|
103
|
export type SearchHitsProps = { |
|
104
|
indexName: string |
|
105
|
setNoResults: (index: string, value: boolean) => void |
|
106
|
checkInternalPattern?: RegExp |
|
107
|
} |
|
108
|
|
|
109
|
export const SearchHits = ({ |
|
110
|
indexName, |
|
111
|
setNoResults, |
|
112
|
checkInternalPattern, |
|
113
|
}: SearchHitsProps) => { |
|
114
|
const { items: hits, sendEvent } = useHits<HitType>() |
|
115
|
const { status } = useInstantSearch() |
|
116
|
const { setIsOpen } = useSearch() |
|
117
|
|
|
118
|
useEffect(() => { |
|
119
|
if (status !== "loading" && status !== "stalled") { |
|
120
|
setNoResults(indexName, hits.length === 0) |
|
121
|
} |
|
122
|
}, [hits, status]) |
|
123
|
|
|
124
|
const checkIfInternal = (url: string): boolean => { |
|
125
|
if (!checkInternalPattern) { |
|
126
|
return false |
|
127
|
} |
|
128
|
return checkInternalPattern.test(url) |
|
129
|
} |
|
130
|
|
|
131
|
const getHierarchySnippetAttribute = ( |
|
132
|
hit: (typeof hits)[0] |
|
133
|
): keyof HitType => { |
|
134
|
let prefix = "hierarchy." |
|
135
|
if (!hit._snippetResult?.hierarchy) { |
|
136
|
return (prefix + "lvl1") as keyof HitType |
|
137
|
} |
|
138
|
const entries = Object.entries(hit._snippetResult.hierarchy) |
|
139
|
const matchedLevel = |
|
140
|
entries.find(([, value]) => value.matchLevel === "full") || |
|
141
|
entries.find(([, value]) => value.matchLevel === "partial") |
|
142
|
if (matchedLevel) { |
|
143
|
prefix += matchedLevel[0] |
|
144
|
} else { |
|
145
|
prefix += "lvl1" |
|
146
|
} |
|
147
|
return prefix as keyof HitType |
|
148
|
} |
|
149
|
|
|
150
|
return ( |
|
151
|
<div |
|
152
|
className={clsx( |
|
153
|
"overflow-auto", |
|
154
|
"[&_mark]:bg-medusa-bg-highlight", |
|
155
|
"[&_mark]:text-medusa-fg-interactive" |
|
156
|
)} |
|
157
|
data-group |
|
158
|
> |
|
159
|
{hits.map((item, index) => { |
|
160
|
const hierarchies = Object.values(item.hierarchy) |
|
161
|
.filter(Boolean) |
|
162
|
.join(" › ") |
|
163
|
return ( |
|
164
|
<div |
|
165
|
key={index} |
|
166
|
className={clsx( |
|
167
|
"p-docs_0.5", |
|
168
|
"hover:bg-medusa-bg-base-hover", |
|
169
|
"focus:bg-medusa-bg-base-hover", |
|
170
|
"focus:outline-none", |
|
171
|
"flex justify-between items-center" |
|
172
|
)} |
|
173
|
tabIndex={index} |
|
174
|
data-hit |
|
175
|
onClick={(e) => { |
|
176
|
const target = e.target as Element |
|
177
|
if (target.tagName.toLowerCase() === "div") { |
|
178
|
target.querySelector("a")?.click() |
|
179
|
} |
|
180
|
}} |
|
181
|
> |
|
182
|
<div |
|
183
|
className={clsx( |
|
184
|
"gap-docs_0.25 relative flex flex-1 flex-col", |
|
185
|
"overflow-x-hidden text-ellipsis whitespace-nowrap break-words" |
|
186
|
)} |
|
187
|
> |
|
188
|
<span |
|
189
|
className={clsx( |
|
190
|
"text-compact-small-plus text-medusa-fg-base", |
|
191
|
"max-w-full" |
|
192
|
)} |
|
193
|
> |
|
194
|
{/* @ts-expect-error React v19 doesn't see this type as a React element */} |
|
195
|
<Snippet |
|
196
|
attribute={getHierarchySnippetAttribute(item)} |
|
197
|
hit={item} |
|
198
|
/> |
|
199
|
</span> |
|
200
|
<span className="text-compact-small text-medusa-fg-subtle text-ellipsis overflow-hidden"> |
|
201
|
{item.type === "content" && ( |
|
202
|
<> |
|
203
|
{/* @ts-expect-error React v19 doesn't see this type as a React element */} |
|
204
|
<Snippet attribute={"content"} hit={item} /> |
|
205
|
</> |
|
206
|
)} |
|
207
|
{item.type !== "content" && item.description} |
|
208
|
</span> |
|
209
|
|
|
210
|
<span |
|
211
|
className={clsx( |
|
212
|
"text-ellipsis overflow-hidden", |
|
213
|
"text-medusa-fg-muted items-center text-compact-x-small" |
|
214
|
)} |
|
215
|
> |
|
216
|
{hierarchies} |
|
217
|
</span> |
|
218
|
<Link |
|
219
|
href={item.url} |
|
220
|
className="absolute top-0 left-0 h-full w-full" |
|
221
|
target="_self" |
|
222
|
onClick={(e) => { |
|
223
|
sendEvent("click", item, "Search Result Clicked") |
|
224
|
if (checkIfInternal(item.url)) { |
|
225
|
e.preventDefault() |
|
226
|
window.location.href = item.url |
|
227
|
setIsOpen(false) |
|
228
|
} |
|
229
|
}} |
|
230
|
/> |
|
231
|
</div> |
|
232
|
{!!item.integration_vendor && |
|
233
|
item.integration_vendor !== "Medusa" && ( |
|
234
|
<Badge variant="blue" badgeType="shaded"> |
|
235
|
Community |
|
236
|
</Badge> |
|
237
|
)} |
|
238
|
</div> |
|
239
|
) |
|
240
|
})} |
|
241
|
</div> |
|
242
|
) |
|
243
|
} |