replaySearchBar.tsx 4.3 KB

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