1 "use client"
2
3 import React from "react"
4 import { useInstantSearch } from "react-instantsearch"
5 import { SearchHitGroupName } from "../Hits/GroupName"
6 import { useSearch } from "@/providers/Search"
7 import { SearchSuggestionItem } from "./Item"
8 import { Badge } from "@/components/Badge"
9
10 export type SearchSuggestionType = {
11 title: string
12 items: string[]
13 }
14
15 export type SearchSuggestionsProps = {
16 suggestions: SearchSuggestionType[]
17 }
18
19 export const SearchSuggestions = ({ suggestions }: SearchSuggestionsProps) => {
20 const { setIndexUiState } = useInstantSearch()
21 const { commands, setCommand } = useSearch()
22
23 return (
24 <div className="h-full overflow-auto px-docs_0.5">
25 {commands.length > 0 && (
26 <>
27 <SearchHitGroupName name={"Commands"} />
28 {commands.map((command, index) => (
29 <SearchSuggestionItem
30 onClick={() => setCommand(command)}
31 key={index}
32 tabIndex={index}
33 >
34 {command.icon}
35 <span className="flex-1">{command.title}</span>
36 {command.badge && <Badge {...command.badge} />}
37 </SearchSuggestionItem>
38 ))}
39 </>
40 )}
41 {suggestions.map((suggestion, index) => (
42 <React.Fragment key={index}>
43 <SearchHitGroupName name={suggestion.title} />
44 {suggestion.items.map((item, itemIndex) => (
45 <SearchSuggestionItem
46 onClick={() =>
47 setIndexUiState({
48 query: item,
49 })
50 }
51 key={itemIndex}
52 tabIndex={commands.length + itemIndex}
53 >
54 {item}
55 </SearchSuggestionItem>
56 ))}
57 </React.Fragment>
58 ))}
59 </div>
60 )
61 }