-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
index.ts
56 lines (48 loc) · 1.46 KB
/
index.ts
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { ITagsCache } from '../../../../../src/plugins/saved_objects_tagging_oss/public';
interface GetSuggestionOptions {
searchTerm: string;
searchableTypes: string[];
tagCache?: ITagsCache;
}
export interface SearchSuggestion {
key: string;
label: string;
description: string;
icon: string;
suggestedSearch: string;
}
export const getSuggestions = ({
searchTerm,
searchableTypes,
tagCache,
}: GetSuggestionOptions): SearchSuggestion[] => {
const results: SearchSuggestion[] = [];
const trimmedTerm = searchTerm.trim();
if (searchableTypes.includes(trimmedTerm)) {
results.push({
key: '__type__suggestion__',
label: `type: ${trimmedTerm}`,
icon: 'tag',
description: 'Filter by type',
suggestedSearch: `type:${searchTerm}`, // TODO: escape if necessary
});
}
if (tagCache && searchTerm) {
const matchingTag = tagCache.getState().find((tag) => tag.name === trimmedTerm);
if (matchingTag) {
results.push({
key: '__tag__suggestion__',
label: `tag: ${matchingTag.name}`,
icon: 'tag',
description: 'Filter by tag name',
suggestedSearch: `tag:${searchTerm}`, // TODO: escape if necessary
});
}
}
return results;
};