replaySearchBar.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 {isAggregateField} from 'sentry/utils/discover/fields';
  15. import {
  16. FieldKind,
  17. getFieldDefinition,
  18. REPLAY_CLICK_FIELDS,
  19. REPLAY_FIELDS,
  20. } from 'sentry/utils/fields';
  21. import useApi from 'sentry/utils/useApi';
  22. import useTags from 'sentry/utils/useTags';
  23. const SEARCH_SPECIAL_CHARS_REGEXP = new RegExp(
  24. `^${NEGATION_OPERATOR}|\\${SEARCH_WILDCARD}`,
  25. 'g'
  26. );
  27. /**
  28. * Prepare query string (e.g. strip special characters like negation operator)
  29. */
  30. function prepareQuery(searchQuery: string) {
  31. return searchQuery.replace(SEARCH_SPECIAL_CHARS_REGEXP, '');
  32. }
  33. const getReplayFieldDefinition = (key: string) => getFieldDefinition(key, 'replay');
  34. function fieldDefinitionsToTagCollection(fieldKeys: string[]): TagCollection {
  35. return Object.fromEntries(
  36. fieldKeys.map(key => [
  37. key,
  38. {
  39. key,
  40. name: key,
  41. ...getReplayFieldDefinition(key),
  42. },
  43. ])
  44. );
  45. }
  46. const REPLAY_FIELDS_AS_TAGS = fieldDefinitionsToTagCollection(REPLAY_FIELDS);
  47. const REPLAY_CLICK_FIELDS_AS_TAGS = fieldDefinitionsToTagCollection(REPLAY_CLICK_FIELDS);
  48. function getSupportedTags(supportedTags: TagCollection, organization: Organization) {
  49. return {
  50. ...Object.fromEntries(
  51. Object.keys(supportedTags).map(key => [
  52. key,
  53. {
  54. ...supportedTags[key],
  55. kind: getReplayFieldDefinition(key)?.kind ?? FieldKind.TAG,
  56. },
  57. ])
  58. ),
  59. ...(organization && organization.features.includes('session-replay-dom-search')
  60. ? REPLAY_CLICK_FIELDS_AS_TAGS
  61. : {}),
  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, organization)}
  105. placeholder={t('Search for users, duration, count_errors, and more')}
  106. prepareQuery={prepareQuery}
  107. maxQueryLength={MAX_QUERY_LENGTH}
  108. searchSource="replay_index"
  109. savedSearchType={SavedSearchType.REPLAY}
  110. maxMenuHeight={500}
  111. hasRecentSearches
  112. fieldDefinitionGetter={getReplayFieldDefinition}
  113. />
  114. );
  115. }
  116. export default ReplaySearchBar;