searchBar.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import React from 'react';
  2. import {ClassNames} from '@emotion/react';
  3. import assign from 'lodash/assign';
  4. import flatten from 'lodash/flatten';
  5. import isEqual from 'lodash/isEqual';
  6. import memoize from 'lodash/memoize';
  7. import omit from 'lodash/omit';
  8. import {fetchTagValues} from 'app/actionCreators/tags';
  9. import {Client} from 'app/api';
  10. import SmartSearchBar from 'app/components/smartSearchBar';
  11. import {NEGATION_OPERATOR, SEARCH_WILDCARD} from 'app/constants';
  12. import {Organization, SavedSearchType, TagCollection} from 'app/types';
  13. import {defined} from 'app/utils';
  14. import {
  15. Field,
  16. FIELD_TAGS,
  17. isAggregateField,
  18. isMeasurement,
  19. TRACING_FIELDS,
  20. } from 'app/utils/discover/fields';
  21. import Measurements from 'app/utils/measurements/measurements';
  22. import withApi from 'app/utils/withApi';
  23. import withTags from 'app/utils/withTags';
  24. const SEARCH_SPECIAL_CHARS_REGEXP = new RegExp(
  25. `^${NEGATION_OPERATOR}|\\${SEARCH_WILDCARD}`,
  26. 'g'
  27. );
  28. type SearchBarProps = Omit<React.ComponentProps<typeof SmartSearchBar>, 'tags'> & {
  29. api: Client;
  30. organization: Organization;
  31. tags: TagCollection;
  32. omitTags?: string[];
  33. projectIds?: number[] | Readonly<number[]>;
  34. fields?: Readonly<Field[]>;
  35. };
  36. class SearchBar extends React.PureComponent<SearchBarProps> {
  37. componentDidMount() {
  38. // Clear memoized data on mount to make tests more consistent.
  39. this.getEventFieldValues.cache.clear?.();
  40. }
  41. componentDidUpdate(prevProps) {
  42. if (!isEqual(this.props.projectIds, prevProps.projectIds)) {
  43. // Clear memoized data when projects change.
  44. this.getEventFieldValues.cache.clear?.();
  45. }
  46. }
  47. /**
  48. * Returns array of tag values that substring match `query`; invokes `callback`
  49. * with data when ready
  50. */
  51. getEventFieldValues = memoize(
  52. (tag, query, endpointParams): Promise<string[]> => {
  53. const {api, organization, projectIds} = this.props;
  54. const projectIdStrings = (projectIds as Readonly<number>[])?.map(String);
  55. if (isAggregateField(tag.key) || isMeasurement(tag.key)) {
  56. // We can't really auto suggest values for aggregate fields
  57. // or measurements, so we simply don't
  58. return Promise.resolve([]);
  59. }
  60. return fetchTagValues(
  61. api,
  62. organization.slug,
  63. tag.key,
  64. query,
  65. projectIdStrings,
  66. endpointParams,
  67. // allows searching for tags on transactions as well
  68. true
  69. ).then(
  70. results =>
  71. flatten(results.filter(({name}) => defined(name)).map(({name}) => name)),
  72. () => {
  73. throw new Error('Unable to fetch event field values');
  74. }
  75. );
  76. },
  77. ({key}, query) => `${key}-${query}`
  78. );
  79. /**
  80. * Prepare query string (e.g. strip special characters like negation operator)
  81. */
  82. prepareQuery = query => query.replace(SEARCH_SPECIAL_CHARS_REGEXP, '');
  83. getTagList(
  84. measurements: Parameters<
  85. React.ComponentProps<typeof Measurements>['children']
  86. >[0]['measurements']
  87. ) {
  88. const {fields, organization, tags, omitTags} = this.props;
  89. const functionTags = fields
  90. ? Object.fromEntries(
  91. fields
  92. .filter(item => !Object.keys(FIELD_TAGS).includes(item.field))
  93. .map(item => [item.field, {key: item.field, name: item.field}])
  94. )
  95. : {};
  96. const fieldTags = organization.features.includes('performance-view')
  97. ? Object.assign({}, measurements, FIELD_TAGS, functionTags)
  98. : omit(FIELD_TAGS, TRACING_FIELDS);
  99. const combined = assign({}, tags, fieldTags);
  100. combined.has = {
  101. key: 'has',
  102. name: 'Has property',
  103. values: Object.keys(combined),
  104. predefined: true,
  105. };
  106. return omit(combined, omitTags ?? []);
  107. }
  108. render() {
  109. return (
  110. <Measurements>
  111. {({measurements}) => {
  112. const tags = this.getTagList(measurements);
  113. return (
  114. <ClassNames>
  115. {({css}) => (
  116. <SmartSearchBar
  117. {...this.props}
  118. hasRecentSearches
  119. savedSearchType={SavedSearchType.EVENT}
  120. onGetTagValues={this.getEventFieldValues}
  121. supportedTags={tags}
  122. prepareQuery={this.prepareQuery}
  123. excludeEnvironment
  124. dropdownClassName={css`
  125. max-height: 300px;
  126. overflow-y: auto;
  127. `}
  128. />
  129. )}
  130. </ClassNames>
  131. );
  132. }}
  133. </Measurements>
  134. );
  135. }
  136. }
  137. export default withApi(withTags(SearchBar));