/
searchBar.jsx
102 lines (88 loc) · 2.62 KB
/
searchBar.jsx
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
import {css} from 'react-emotion';
import {flatten, memoize} from 'lodash';
import PropTypes from 'prop-types';
import React from 'react';
import {COLUMNS} from 'app/views/organizationDiscover/data';
import {NEGATION_OPERATOR, SEARCH_WILDCARD} from 'app/constants';
import {addErrorMessage} from 'app/actionCreators/indicator';
import {defined} from 'app/utils';
import {fetchEventFieldValues} from 'app/actionCreators/events';
import {fetchOrganizationTags} from 'app/actionCreators/tags';
import {t} from 'app/locale';
import SentryTypes from 'app/sentryTypes';
import SmartSearchBar from 'app/components/smartSearchBar';
import withApi from 'app/utils/withApi';
const tagToObjectReducer = (acc, name) => {
acc[name] = {
key: name,
name,
};
return acc;
};
const TAGS = COLUMNS.map(({name}) => name);
const SEARCH_SPECIAL_CHARS_REGEXP = new RegExp(
`^${NEGATION_OPERATOR}|\\${SEARCH_WILDCARD}`,
'g'
);
class SearchBar extends React.PureComponent {
static propTypes = {
api: PropTypes.object,
organization: SentryTypes.Organization,
};
constructor() {
super();
this.state = {
tags: {},
};
}
componentDidMount() {
let {api, organization} = this.props;
fetchOrganizationTags(api, organization.slug).then(
results => {
this.setState({
tags: this.getAllTags(results.map(({key}) => key)),
});
},
() => addErrorMessage(t('There was a problem fetching tags'))
);
}
/**
* Returns array of tag values that substring match `query`; invokes `callback`
* with data when ready
*/
getEventFieldValues = memoize((tag, query) => {
let {api, organization} = this.props;
return fetchEventFieldValues(api, organization.slug, tag.key, query).then(
results => flatten(results.filter(({name}) => defined(name)).map(({name}) => name)),
() => {
throw new Error('Unable to fetch event field values');
}
);
}, ({key}, query) => `${key}-${query}`);
getAllTags = (orgTags = []) =>
TAGS.concat(orgTags)
.sort()
.reduce(tagToObjectReducer, {});
/**
* Prepare query string (e.g. strip special characters like negation operator)
*/
prepareQuery = query => {
return query.replace(SEARCH_SPECIAL_CHARS_REGEXP, '');
};
render() {
return (
<SmartSearchBar
{...this.props}
onGetTagValues={this.getEventFieldValues}
supportedTags={this.state.tags}
prepareQuery={this.prepareQuery}
excludeEnvironment
dropdownClassName={css`
max-height: 300px;
overflow-y: auto;
`}
/>
);
}
}
export default withApi(SearchBar);