-
-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathSearch.res
160 lines (138 loc) · 4.31 KB
/
Search.res
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
152
153
154
155
156
157
158
159
160
let apiKey = "a2485ef172b8cd82a2dfa498d551399b"
let indexName = "rescript-lang"
let appId = "S32LNEY41T"
@val @scope("document")
external activeElement: option<Dom.element> = "activeElement"
type keyboardEventLike = {key: string, ctrlKey: bool, metaKey: bool}
@val @scope("window")
external addKeyboardEventListener: (string, keyboardEventLike => unit) => unit = "addEventListener"
@val @scope("window")
external removeKeyboardEventListener: (string, keyboardEventLike => unit) => unit =
"addEventListener"
type window
@val external window: window = "window"
@get external scrollY: window => int = "scrollY"
@send
external keyboardEventPreventDefault: keyboardEventLike => unit = "preventDefault"
@get external tagName: Dom.element => string = "tagName"
@get external isContentEditable: Dom.element => bool = "isContentEditable"
type state = Active | Inactive
let hit = ({hit, children}: DocSearch.hitComponent) => {
let toTitle = str => str->String.charAt(0)->String.toUpperCase ++ String.sliceToEnd(str, ~start=1)
let description = switch hit.url
->String.split("/")
->Array.sliceToEnd(~start=1)
->List.fromArray {
| list{"blog" as r | "community" as r, ..._} => r->toTitle
| list{"docs", doc, version, ...rest} =>
let path = rest->List.toArray
let info =
path
->Array.slice(~start=0, ~end=Array.length(path) - 1)
->Array.map(path =>
switch path {
| "api" => "API"
| other => toTitle(other)
}
)
[doc->toTitle, version->toTitle]->Array.concat(info)->Array.join(" / ")
| _ => ""
}
<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->Array.filterMap(item => {
let url = try Webapi.URL.make(item.url)->Some catch {
| Exn.Error(obj) =>
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 = Url.parse(router.route)->Url.getVersionString
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
}}
</>
}