-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathSearch.res
More file actions
188 lines (172 loc) · 5.55 KB
/
Search.res
File metadata and controls
188 lines (172 loc) · 5.55 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
let apiKey = "a2485ef172b8cd82a2dfa498d551399b"
let indexName = "rescript-lang"
let appId = "S32LNEY41T"
type state = Active | Inactive
let hit = ({hit, children}: DocSearch.hitComponent) => {
let toTitle = str => str->String.charAt(0)->String.toUpperCase ++ String.slice(str, ~start=1)
let url = hit.url->Util.Url.normalizeUrl
let description = switch hit.url
->String.split("/")
->Array.slice(~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(" / ")
| _ => ""
}
let isDeprecated = hit.deprecated->Option.isSome
let deprecatedBadge = isDeprecated
? <span
className="inline-flex items-center px-2 py-1 text-xs font-medium text-orange-600 bg-orange-100 rounded-full mr-2"
>
{"Deprecated"->React.string}
</span>
: React.null
<ReactRouter.Link.String to=url className="flex flex-col w-full">
<span className="text-gray-60 captions px-4 pt-3 pb-1 flex items-center">
{deprecatedBadge}
{description->React.string}
</span>
children
</ReactRouter.Link.String>
}
let transformItems = (items: DocSearch.transformItems) => {
items
->Array.filterMap(item => {
let url = try WebAPI.URL.make(~url=item.url->Url.normalizePath)->Some catch {
| Exn.Error(obj) =>
Console.error2(`Failed to parse URL ${item.url}`, obj)
None
}
switch url {
| Some({pathname, hash}) =>
RegExp.test(/v(8|9|10|11)\./, pathname)
? None
: {
// DocSearch internally calls .replace() on hierarchy.lvl1, so we must
// provide a fallback for items where lvl1 is null to prevent crashes
let hierarchy = item.hierarchy
let lvl0 = hierarchy.lvl0->Nullable.toOption->Option.getOr("")
let lvl1 = hierarchy.lvl1->Nullable.toOption->Option.getOr(lvl0)
Some({
...item,
deprecated: pathname->String.includes("api/js") ||
pathname->String.includes("api/core")
? Some("Deprecated")
: None,
url: pathname->String.replace("/v12.0.0/", "/") ++ hash,
hierarchy: {
...hierarchy,
lvl0: Nullable.make(lvl0),
lvl1: Nullable.make(lvl1),
},
})
}
| None => None
}
})
// Sort deprecated items to the end
->Array.toSorted((a, b) => {
switch (a.deprecated, b.deprecated) {
| (Some(_), None) => 1. // a is deprecated, b is not - put a after b
| (None, Some(_)) => -1. // a is not deprecated, b is - put a before b
| _ => 0.
}
})
->Array.toSorted((a, b) => {
switch (a.url->String.includes("api/stdlib"), b.url->String.includes("api/stdlib")) {
| (true, false) => -1. // a is a stdlib doc, b is not - put a before b
| (false, true) => 1. // a is not a stdlib doc, b is - put a after b
| _ => 0. // both same API status - maintain original order
}
})
}
@react.component
let make = () => {
let (state, setState) = React.useState(_ => Inactive)
let handleCloseModal = () => {
let () = switch WebAPI.Document.querySelector(document, ".DocSearch-Modal") {
| Value(modal) =>
switch WebAPI.Document.querySelector(document, "body") {
| Value(body) =>
WebAPI.DOMTokenList.remove(body.classList, "DocSearch--active")
modal->WebAPI.Element.addEventListener(Transitionend, () => {
setState(_ => Inactive)
})
| Null => setState(_ => Inactive)
}
| Null => ()
}
}
React.useEffect(() => {
let isEditableTag = (el: WebAPI.DOMAPI.element) =>
switch el.tagName {
| "TEXTAREA" | "SELECT" | "INPUT" => true
| _ => false
}
let focusSearch = (e: WebAPI.UIEventsAPI.keyboardEvent) => {
switch document.activeElement {
| Value(el)
if el->isEditableTag || (Obj.magic(el): WebAPI.DOMAPI.htmlElement).isContentEditable => ()
| _ =>
setState(_ => Active)
WebAPI.KeyboardEvent.preventDefault(e)
}
}
let handleGlobalKeyDown = (e: WebAPI.UIEventsAPI.keyboardEvent) => {
switch e.key {
| "/" => focusSearch(e)
| "k" if e.ctrlKey || e.metaKey => focusSearch(e)
| "Escape" => handleCloseModal()
| _ => ()
}
}
WebAPI.Window.addEventListener(window, Keydown, handleGlobalKeyDown)
Some(() => WebAPI.Window.removeEventListener(window, 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" ariaLabel="Search"
>
<Icon.MagnifierGlass className="fill-current" />
</button>
{switch state {
| Active =>
switch ReactDOM.querySelector("body") {
| Some(element) =>
ReactDOM.createPortal(
<DocSearch
apiKey
appId
indexName
onClose
initialScrollY={window.scrollY->Float.toInt}
transformItems={transformItems}
hitComponent=hit
/>,
element,
)
| None => React.null
}
| Inactive => React.null
}}
</>
}