replaySearchBar.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {useCallback, useEffect} from 'react';
  2. import {fetchTagValues, loadOrganizationTags} from 'sentry/actionCreators/tags';
  3. import FeatureBadge from 'sentry/components/featureBadge';
  4. import SmartSearchBar from 'sentry/components/smartSearchBar';
  5. import {MAX_QUERY_LENGTH, NEGATION_OPERATOR, SEARCH_WILDCARD} from 'sentry/constants';
  6. import {t} from 'sentry/locale';
  7. import {
  8. Organization,
  9. PageFilters,
  10. SavedSearchType,
  11. Tag,
  12. TagCollection,
  13. TagValue,
  14. } from 'sentry/types';
  15. import {trackAnalytics} from 'sentry/utils/analytics';
  16. import {isAggregateField} from 'sentry/utils/discover/fields';
  17. import {
  18. FieldKind,
  19. getFieldDefinition,
  20. REPLAY_CLICK_FIELDS,
  21. REPLAY_FIELDS,
  22. } from 'sentry/utils/fields';
  23. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  24. import useApi from 'sentry/utils/useApi';
  25. import useTags from 'sentry/utils/useTags';
  26. const SEARCH_SPECIAL_CHARS_REGEXP = new RegExp(
  27. `^${NEGATION_OPERATOR}|\\${SEARCH_WILDCARD}`,
  28. 'g'
  29. );
  30. /**
  31. * Prepare query string (e.g. strip special characters like negation operator)
  32. */
  33. function prepareQuery(searchQuery: string) {
  34. return searchQuery.replace(SEARCH_SPECIAL_CHARS_REGEXP, '');
  35. }
  36. const getReplayFieldDefinition = (key: string) => getFieldDefinition(key, 'replay');
  37. function fieldDefinitionsToTagCollection(fieldKeys: string[]): TagCollection {
  38. return Object.fromEntries(
  39. fieldKeys.map(key => [
  40. key,
  41. {
  42. key,
  43. name: key,
  44. ...getReplayFieldDefinition(key),
  45. },
  46. ])
  47. );
  48. }
  49. const REPLAY_FIELDS_AS_TAGS = fieldDefinitionsToTagCollection(REPLAY_FIELDS);
  50. const REPLAY_CLICK_FIELDS_AS_TAGS = fieldDefinitionsToTagCollection(REPLAY_CLICK_FIELDS);
  51. function getSupportedTags(supportedTags: TagCollection) {
  52. return {
  53. ...Object.fromEntries(
  54. Object.keys(supportedTags).map(key => [
  55. key,
  56. {
  57. ...supportedTags[key],
  58. kind: getReplayFieldDefinition(key)?.kind ?? FieldKind.TAG,
  59. },
  60. ])
  61. ),
  62. ...REPLAY_CLICK_FIELDS_AS_TAGS,
  63. ...REPLAY_FIELDS_AS_TAGS,
  64. };
  65. }
  66. type Props = React.ComponentProps<typeof SmartSearchBar> & {
  67. organization: Organization;
  68. pageFilters: PageFilters;
  69. };
  70. function ReplaySearchBar(props: Props) {
  71. const {organization, pageFilters} = props;
  72. const api = useApi();
  73. const projectIdStrings = pageFilters.projects?.map(String);
  74. const tags = useTags();
  75. useEffect(() => {
  76. loadOrganizationTags(api, organization.slug, pageFilters);
  77. }, [api, organization.slug, pageFilters]);
  78. const getTagValues = useCallback(
  79. (tag: Tag, searchQuery: string, _params: object): Promise<string[]> => {
  80. if (isAggregateField(tag.key)) {
  81. // We can't really auto suggest values for aggregate fields
  82. // or measurements, so we simply don't
  83. return Promise.resolve([]);
  84. }
  85. return fetchTagValues({
  86. api,
  87. orgSlug: organization.slug,
  88. tagKey: tag.key,
  89. search: searchQuery,
  90. projectIds: projectIdStrings,
  91. includeReplays: true,
  92. }).then(
  93. tagValues => (tagValues as TagValue[]).map(({value}) => value),
  94. () => {
  95. throw new Error('Unable to fetch event field values');
  96. }
  97. );
  98. },
  99. [api, organization.slug, projectIdStrings]
  100. );
  101. return (
  102. <SmartSearchBar
  103. {...props}
  104. onGetTagValues={getTagValues}
  105. supportedTags={getSupportedTags(tags)}
  106. placeholder={t(
  107. 'Search for users, duration, clicked elements, count_errors, and more'
  108. )}
  109. prepareQuery={prepareQuery}
  110. maxQueryLength={MAX_QUERY_LENGTH}
  111. searchSource="replay_index"
  112. savedSearchType={SavedSearchType.REPLAY}
  113. maxMenuHeight={500}
  114. hasRecentSearches
  115. fieldDefinitionGetter={getReplayFieldDefinition}
  116. mergeSearchGroupWith={{
  117. click: {
  118. documentation: t('Search by click selector. (Requires SDK version >= 7.44.0)'),
  119. titleBadge: <FeatureBadge type="new">{t('New')}</FeatureBadge>,
  120. },
  121. }}
  122. onSearch={(query: string) => {
  123. props.onSearch?.(query);
  124. const conditions = new MutableSearch(query);
  125. const searchKeys = conditions.tokens.map(({key}) => key).filter(Boolean);
  126. if (searchKeys.length > 0) {
  127. trackAnalytics('replay.search', {
  128. search_keys: searchKeys.join(','),
  129. organization,
  130. });
  131. }
  132. }}
  133. />
  134. );
  135. }
  136. export default ReplaySearchBar;