replaySearchBar.tsx 4.2 KB

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