forked from hyperdxio/hyperdx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutocompleteInput.tsx
More file actions
325 lines (308 loc) · 10.3 KB
/
Copy pathAutocompleteInput.tsx
File metadata and controls
325 lines (308 loc) · 10.3 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import Fuse from 'fuse.js';
import { OverlayTrigger } from 'react-bootstrap';
import { TextInput, UnstyledButton } from '@mantine/core';
import { useQueryHistory } from '@/utils';
import InputLanguageSwitch from './components/InputLanguageSwitch';
import { useDebounce } from './utils';
export default function AutocompleteInput({
inputRef,
value,
onChange,
placeholder = 'Search your events for anything...',
autocompleteOptions,
size = 'sm',
aboveSuggestions,
belowSuggestions,
showSuggestionsOnEmpty,
suggestionsHeader = 'Properties',
zIndex = 999,
onLanguageChange,
language,
showHotkey,
onSubmit,
queryHistoryType,
}: {
inputRef: React.RefObject<HTMLInputElement>;
value?: string;
onChange: (value: string) => void;
onSubmit?: () => void;
placeholder?: string;
size?: 'xs' | 'sm' | 'lg';
autocompleteOptions?: { value: string; label: string }[];
aboveSuggestions?: React.ReactNode;
belowSuggestions?: React.ReactNode;
showSuggestionsOnEmpty?: boolean;
suggestionsHeader?: string;
zIndex?: number;
onLanguageChange?: (language: 'sql' | 'lucene') => void;
language?: 'sql' | 'lucene';
showHotkey?: boolean;
queryHistoryType?: string;
}) {
const suggestionsLimit = 10;
const [isSearchInputFocused, setIsSearchInputFocused] = useState(false);
const [isInputDropdownOpen, setIsInputDropdownOpen] = useState(false);
const [showSearchHistory, setShowSearchHistory] = useState(false);
const [selectedAutocompleteIndex, setSelectedAutocompleteIndex] =
useState(-1);
const [selectedQueryHistoryIndex, setSelectedQueryHistoryIndex] =
useState(-1);
// query search history
const [queryHistory, setQueryHistory] = useQueryHistory(queryHistoryType);
const queryHistoryList = useMemo(() => {
if (!queryHistoryType || !queryHistory) return [];
return queryHistory.map(q => {
return {
value: q,
label: q,
};
});
}, [queryHistory, queryHistoryType]);
useEffect(() => {
if (isSearchInputFocused) {
setIsInputDropdownOpen(true);
}
}, [isSearchInputFocused]);
useEffect(() => {
// only show search history when: 1.no input, 2.has search type, 3.has history list
if (
value != null &&
value.length === 0 &&
queryHistoryList.length > 0 &&
queryHistoryType
) {
setShowSearchHistory(true);
} else {
setShowSearchHistory(false);
}
}, [value, queryHistoryType, queryHistoryList]);
const fuse = useMemo(
() =>
new Fuse(autocompleteOptions ?? [], {
keys: ['value'],
threshold: 0,
ignoreLocation: true,
}),
[autocompleteOptions],
);
const debouncedValue = useDebounce(value ?? '', 200);
const suggestedProperties = useMemo(() => {
const tokens = debouncedValue.split(' ');
const lastToken = tokens[tokens.length - 1];
if (lastToken.length === 0 && showSuggestionsOnEmpty) {
return autocompleteOptions ?? [];
}
return fuse.search(lastToken).map(result => result.item);
}, [debouncedValue, fuse, autocompleteOptions, showSuggestionsOnEmpty]);
const onSelectSearchHistory = (query: string) => {
setSelectedQueryHistoryIndex(-1);
onChange(query); // update inputText bar
setQueryHistory(query); // update history order
setIsInputDropdownOpen(false); // close dropdown since we execute search
onSubmit?.(); // search
};
const onAcceptSuggestion = (suggestion: string) => {
setSelectedAutocompleteIndex(-1);
const newValue =
value == null
? suggestion
: value.split(' ').slice(0, -1).join(' ') +
`${value.split(' ').length > 1 ? ' ' : ''}${suggestion}`;
onChange(newValue);
inputRef.current?.focus();
};
const ref = useRef<HTMLDivElement>(null);
return (
<OverlayTrigger
rootClose
onToggle={opened => {
// if opened is transitioning to false, but input is focused, ignore it
if (!opened && isSearchInputFocused) {
return;
}
setIsInputDropdownOpen(opened);
}}
show={isInputDropdownOpen}
placement="bottom-start"
delay={{ show: 0, hide: 0 }}
overlay={({ style, ...props }) => (
<div
className="bg-body border border-dark rounded"
style={{
...style,
maxWidth:
(inputRef.current?.clientWidth || 0) > 300
? inputRef.current?.clientWidth
: 720,
width: '100%',
zIndex,
}}
{...props}
>
{aboveSuggestions != null && (
<div className="d-flex p-2 flex-wrap px-3">{aboveSuggestions}</div>
)}
<div>
{suggestedProperties.length > 0 && (
<div className="border-top border-dark fs-8 py-2">
<div className="d-flex justify-content-between px-3 mb-2">
<div className="me-2 text-light">{suggestionsHeader}</div>
{suggestedProperties.length > suggestionsLimit && (
<div className="text-muted">
(Showing Top {suggestionsLimit})
</div>
)}
</div>
{suggestedProperties
.slice(0, suggestionsLimit)
.map(({ value, label }, i) => (
<div
className={`py-2 px-3 ${
selectedAutocompleteIndex === i ? 'bg-hdx-dark' : ''
}`}
role="button"
key={value}
onMouseOver={() => {
setSelectedAutocompleteIndex(i);
}}
onClick={() => {
onAcceptSuggestion(value);
}}
>
<span className="me-1">{label}</span>
</div>
))}
</div>
)}
</div>
{belowSuggestions != null && (
<div className="border-top border-dark bg-body px-3 pt-2 pb-1 mt-2 fs-8 d-flex align-items-center text-muted flex-wrap">
{belowSuggestions}
</div>
)}
<div>
{showSearchHistory && (
<div className="border-top border-dark fs-8 py-2">
<div className="text-muted fs-8 fw-bold me-1 px-3">
Search History:
</div>
{queryHistoryList.map(({ value, label }, i) => {
return (
<UnstyledButton
className={`d-block w-100 text-start text-muted fw-normal px-3 py-2 fs-8 ${
selectedQueryHistoryIndex === i ? 'bg-hdx-dark' : ''
}`}
key={value}
onMouseOver={() => setSelectedQueryHistoryIndex(i)}
onClick={() => onSelectSearchHistory(value)}
>
<span className="me-1 text-truncate">{label}</span>
</UnstyledButton>
);
})}
</div>
)}
</div>
</div>
)}
popperConfig={{
modifiers: [
{
name: 'offset',
options: {
offset: [0, 8],
},
},
],
}}
trigger={[]}
>
<TextInput
ref={inputRef}
type="text"
style={{ flexGrow: 1 }}
placeholder={placeholder}
className="border-0 fs-8"
value={value}
size={size}
onChange={e => onChange(e.target.value)}
onFocus={() => {
setSelectedAutocompleteIndex(-1);
setSelectedQueryHistoryIndex(-1);
setIsSearchInputFocused(true);
}}
onBlur={() => {
setSelectedAutocompleteIndex(-1);
setSelectedQueryHistoryIndex(-1);
setIsSearchInputFocused(false);
}}
onKeyDown={e => {
if (e.key === 'Escape' && e.target instanceof HTMLInputElement) {
e.target.blur();
}
// Autocomplete Navigation/Acceptance Keys
if (e.key === 'Tab' && e.target instanceof HTMLInputElement) {
if (
suggestedProperties.length > 0 &&
selectedAutocompleteIndex < suggestedProperties.length &&
selectedAutocompleteIndex >= 0
) {
e.preventDefault();
onAcceptSuggestion(
suggestedProperties[selectedAutocompleteIndex].value,
);
}
}
if (e.key === 'Enter' && e.target instanceof HTMLInputElement) {
if (
suggestedProperties.length > 0 &&
selectedAutocompleteIndex < suggestedProperties.length &&
selectedAutocompleteIndex >= 0
) {
onAcceptSuggestion(
suggestedProperties[selectedAutocompleteIndex].value,
);
} else {
if (queryHistoryType && value) {
setQueryHistory(value);
}
setIsInputDropdownOpen(false); // Close suggestion box on Enter
onSubmit?.();
}
}
if (e.key === 'ArrowDown' && e.target instanceof HTMLInputElement) {
if (suggestedProperties.length > 0) {
setSelectedAutocompleteIndex(
Math.min(
selectedAutocompleteIndex + 1,
suggestedProperties.length - 1,
suggestionsLimit - 1,
),
);
}
}
if (e.key === 'ArrowUp' && e.target instanceof HTMLInputElement) {
if (suggestedProperties.length > 0) {
setSelectedAutocompleteIndex(
Math.max(selectedAutocompleteIndex - 1, 0),
);
}
}
}}
rightSectionWidth={ref.current?.clientWidth ?? 'auto'}
rightSection={
<div ref={ref}>
{language != null && onLanguageChange != null && (
<InputLanguageSwitch
showHotkey={showHotkey && isSearchInputFocused}
language={language}
onLanguageChange={onLanguageChange}
/>
)}
</div>
}
/>
</OverlayTrigger>
);
}