replaySearchBar.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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, organization: Organization) {
  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. ...(organization && organization.features.includes('session-replay-dom-search')
  63. ? REPLAY_CLICK_FIELDS_AS_TAGS
  64. : {}),
  65. ...REPLAY_FIELDS_AS_TAGS,
  66. };
  67. }
  68. type Props = React.ComponentProps<typeof SmartSearchBar> & {
  69. organization: Organization;
  70. pageFilters: PageFilters;
  71. };
  72. function ReplaySearchBar(props: Props) {
  73. const {organization, pageFilters} = props;
  74. const api = useApi();
  75. const projectIdStrings = pageFilters.projects?.map(String);
  76. const tags = useTags();
  77. useEffect(() => {
  78. loadOrganizationTags(api, organization.slug, pageFilters);
  79. }, [api, organization.slug, pageFilters]);
  80. const getTagValues = useCallback(
  81. (tag: Tag, searchQuery: string, _params: object): Promise<string[]> => {
  82. if (isAggregateField(tag.key)) {
  83. // We can't really auto suggest values for aggregate fields
  84. // or measurements, so we simply don't
  85. return Promise.resolve([]);
  86. }
  87. return fetchTagValues({
  88. api,
  89. orgSlug: organization.slug,
  90. tagKey: tag.key,
  91. search: searchQuery,
  92. projectIds: projectIdStrings,
  93. includeReplays: true,
  94. }).then(
  95. tagValues => (tagValues as TagValue[]).map(({value}) => value),
  96. () => {
  97. throw new Error('Unable to fetch event field values');
  98. }
  99. );
  100. },
  101. [api, organization.slug, projectIdStrings]
  102. );
  103. return (
  104. <SmartSearchBar
  105. {...props}
  106. onGetTagValues={getTagValues}
  107. supportedTags={getSupportedTags(tags, organization)}
  108. placeholder={t(
  109. 'Search for users, duration, clicked elements, count_errors, and more'
  110. )}
  111. prepareQuery={prepareQuery}
  112. maxQueryLength={MAX_QUERY_LENGTH}
  113. searchSource="replay_index"
  114. savedSearchType={SavedSearchType.REPLAY}
  115. maxMenuHeight={500}
  116. hasRecentSearches
  117. fieldDefinitionGetter={getReplayFieldDefinition}
  118. mergeSearchGroupWith={{
  119. click: {
  120. documentation: t('Requires SDK version >= 7.44.0'),
  121. titleBadge: <FeatureBadge type="new">{t('New')}</FeatureBadge>,
  122. },
  123. }}
  124. onSearch={(query: string) => {
  125. props.onSearch?.(query);
  126. const conditions = new MutableSearch(query);
  127. const searchKeys = conditions.tokens.map(({key}) => key).filter(Boolean);
  128. if (searchKeys.length > 0) {
  129. trackAnalytics('replay.search', {
  130. search_keys: searchKeys.join(','),
  131. organization,
  132. });
  133. }
  134. }}
  135. />
  136. );
  137. }
  138. export default ReplaySearchBar;