replaySearchBar.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. placeholder?: string;
  69. };
  70. function ReplaySearchBar(props: Props) {
  71. const {organization, pageFilters, placeholder} = 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={
  107. placeholder ??
  108. t('Search for users, duration, clicked elements, count_errors, and more')
  109. }
  110. prepareQuery={prepareQuery}
  111. maxQueryLength={MAX_QUERY_LENGTH}
  112. searchSource="replay_index"
  113. savedSearchType={SavedSearchType.REPLAY}
  114. maxMenuHeight={500}
  115. hasRecentSearches
  116. fieldDefinitionGetter={getReplayFieldDefinition}
  117. mergeSearchGroupWith={{
  118. click: {
  119. documentation: t('Search by click selector. (Requires SDK version >= 7.44.0)'),
  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;