1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
let hit = ({hit, children}: DocSearch.hitComponent) => {
let toTitle = str =>
str->Js.String2.charAt(0)->Js.String2.toUpperCase ++ Js.String2.sliceToEnd(str, ~from=1)
let description = switch hit.url
->Js.String2.split("/")
->Js.Array2.sliceFrom(1)
->Belt.List.fromArray {
| list{"blog" as r | "community" as r, ..._} => r->toTitle
| list{"docs", doc, version, ...rest} =>
let path = rest->Belt.List.toArray
let info =
path
->Js.Array2.slice(~start=0, ~end_=Js.Array2.length(path) - 1)
->Js.Array2.map(path =>
switch path {
| "api" => "API"
| other => toTitle(other)
}
)
[doc->toTitle, version->toTitle]->Js.Array2.concat(info)->Js.Array2.joinWith(" / ")
| _ => ""
}
<Next.Link href={hit.url} className="flex flex-col w-full">
<span className="text-gray-60 captions px-4 pt-3 pb-1 block">
{description->React.string}
</span>
children
</Next.Link>
}
let transformItems = (items: DocSearch.transformItems) => {
items->Belt.Array.keepMap(item => {
let url = try Webapi.URL.make(item.url)->Some catch {
| Js.Exn.Error(obj) =>
Js.Console.error2(`Failed to parse URL ${item.url}`, obj)
None
}
switch url {
| Some({pathname, hash}) => {...item, url: pathname ++ hash}->Some
| None => None
}
})
}
@react.component
let make = () => {
let (state, setState) = React.useState(_ => Inactive)
let router = Next.Router.useRouter()
let version = switch Url.parse(router.route).version {
| Version(v) => v
| _ => "latest"
}
let handleCloseModal = () => {
let () = switch ReactDOM.querySelector(".DocSearch-Modal") {
| Some(modal) =>
switch ReactDOM.querySelector("body") {
| Some(body) =>
open Webapi
body->Element.classList->ClassList.remove("DocSearch--active")
modal->Element.addEventListener("transitionend", () => {
setState(_ => Inactive)
})
| None => setState(_ => Inactive)
}
| None => ()
}
}
React.useEffect(() => {
let isEditableTag = el =>
switch el->tagName {
| "TEXTAREA" | "SELECT" | "INPUT" => true
| _ => false
}
let focusSearch = e => {
switch activeElement {
| Some(el) if el->isEditableTag || el->isContentEditable => ()
| _ =>
setState(_ => Active)
e->keyboardEventPreventDefault
}
}
let handleGlobalKeyDown = e => {
switch e.key {
| "/" => focusSearch(e)
| "k" if e.ctrlKey || e.metaKey => focusSearch(e)
| "Escape" => handleCloseModal()
| _ => ()
}
}
addKeyboardEventListener("keydown", handleGlobalKeyDown)
Some(() => removeKeyboardEventListener("keydown", handleGlobalKeyDown))
}, [setState])
let onClick = _ => {
setState(_ => Active)
}
let onClose = React.useCallback(() => {
handleCloseModal()
}, [setState])
<>
<button onClick type_="button" className="text-gray-60 hover:text-fire-50 p-2">
<Icon.MagnifierGlass className="fill-current" />
</button>
{switch state {
| Active =>
switch ReactDOM.querySelector("body") {
| Some(element) =>
ReactDOM.createPortal(
<DocSearch
apiKey
appId
indexName
onClose
searchParameters={facetFilters: ["version:" ++ version]}
initialScrollY={window->scrollY}
transformItems={transformItems}
hitComponent=hit
/>
element,
)
| None => React.null
}
| Inactive => React.null
}}
</>
}
let comparable = (type key, ~cmp) => {
module N = MakeComparable({
type t = key
let cmp = cmp
})
module(N: Comparable with type t = key)
}
<Next.Link href={hit.url} className="flex flex-col w-full">
<span className="text-gray-60 captions px-4 pt-3 pb-1 block">
{description->React.string}
children
</Next.Link>
|