searchBar.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import {useEffect} from 'react';
  2. import assign from 'lodash/assign';
  3. import flatten from 'lodash/flatten';
  4. import memoize from 'lodash/memoize';
  5. import omit from 'lodash/omit';
  6. import {fetchTagValues} from 'sentry/actionCreators/tags';
  7. import SmartSearchBar from 'sentry/components/smartSearchBar';
  8. import {NEGATION_OPERATOR, SEARCH_WILDCARD} from 'sentry/constants';
  9. import {Organization, SavedSearchType, TagCollection} from 'sentry/types';
  10. import {defined} from 'sentry/utils';
  11. import {
  12. Field,
  13. FIELD_TAGS,
  14. getFieldDoc,
  15. isAggregateField,
  16. isEquation,
  17. isMeasurement,
  18. SEMVER_TAGS,
  19. TRACING_FIELDS,
  20. } from 'sentry/utils/discover/fields';
  21. import Measurements from 'sentry/utils/measurements/measurements';
  22. import useApi from 'sentry/utils/useApi';
  23. import withTags from 'sentry/utils/withTags';
  24. const SEARCH_SPECIAL_CHARS_REGEXP = new RegExp(
  25. `^${NEGATION_OPERATOR}|\\${SEARCH_WILDCARD}`,
  26. 'g'
  27. );
  28. export type SearchBarProps = Omit<React.ComponentProps<typeof SmartSearchBar>, 'tags'> & {
  29. organization: Organization;
  30. tags: TagCollection;
  31. fields?: Readonly<Field[]>;
  32. includeSessionTagsValues?: boolean;
  33. /**
  34. * Used to define the max height of the menu in px.
  35. */
  36. maxMenuHeight?: number;
  37. maxSearchItems?: React.ComponentProps<typeof SmartSearchBar>['maxSearchItems'];
  38. omitTags?: string[];
  39. projectIds?: number[] | Readonly<number[]>;
  40. };
  41. function SearchBar(props: SearchBarProps) {
  42. const {
  43. maxSearchItems,
  44. organization,
  45. tags,
  46. omitTags,
  47. fields,
  48. projectIds,
  49. includeSessionTagsValues,
  50. maxMenuHeight,
  51. } = props;
  52. const api = useApi();
  53. useEffect(() => {
  54. // Clear memoized data on mount to make tests more consistent.
  55. getEventFieldValues.cache.clear?.();
  56. // eslint-disable-next-line react-hooks/exhaustive-deps
  57. }, [projectIds]);
  58. // Returns array of tag values that substring match `query`; invokes `callback`
  59. // with data when ready
  60. const getEventFieldValues = memoize(
  61. (tag, query, endpointParams): Promise<string[]> => {
  62. const projectIdStrings = (projectIds as Readonly<number>[])?.map(String);
  63. if (isAggregateField(tag.key) || isMeasurement(tag.key)) {
  64. // We can't really auto suggest values for aggregate fields
  65. // or measurements, so we simply don't
  66. return Promise.resolve([]);
  67. }
  68. return fetchTagValues(
  69. api,
  70. organization.slug,
  71. tag.key,
  72. query,
  73. projectIdStrings,
  74. endpointParams,
  75. // allows searching for tags on transactions as well
  76. true,
  77. // allows searching for tags on sessions as well
  78. includeSessionTagsValues
  79. ).then(
  80. results =>
  81. flatten(results.filter(({name}) => defined(name)).map(({name}) => name)),
  82. () => {
  83. throw new Error('Unable to fetch event field values');
  84. }
  85. );
  86. },
  87. ({key}, query) => `${key}-${query}`
  88. );
  89. const getTagList = (
  90. measurements: Parameters<
  91. React.ComponentProps<typeof Measurements>['children']
  92. >[0]['measurements']
  93. ) => {
  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. return (
  117. <Measurements>
  118. {({measurements}) => (
  119. <SmartSearchBar
  120. hasRecentSearches
  121. savedSearchType={SavedSearchType.EVENT}
  122. onGetTagValues={getEventFieldValues}
  123. supportedTags={getTagList(measurements)}
  124. prepareQuery={query => {
  125. // Prepare query string (e.g. strip special characters like negation operator)
  126. return query.replace(SEARCH_SPECIAL_CHARS_REGEXP, '');
  127. }}
  128. maxSearchItems={maxSearchItems}
  129. excludeEnvironment
  130. maxMenuHeight={maxMenuHeight ?? 300}
  131. getFieldDoc={getFieldDoc}
  132. {...props}
  133. />
  134. )}
  135. </Measurements>
  136. );
  137. }
  138. export default withTags(SearchBar);