searchBar.tsx 4.8 KB

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