searchBar.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import {useCallback, useMemo} from 'react';
  2. import orderBy from 'lodash/orderBy';
  3. // eslint-disable-next-line no-restricted-imports
  4. import {fetchTagValues} from 'sentry/actionCreators/tags';
  5. import {
  6. SearchQueryBuilder,
  7. type SearchQueryBuilderProps,
  8. } from 'sentry/components/searchQueryBuilder';
  9. import type {FilterKeySection} from 'sentry/components/searchQueryBuilder/types';
  10. import {t} from 'sentry/locale';
  11. import {SavedSearchType, type Tag, type TagCollection} from 'sentry/types/group';
  12. import type {Organization} from 'sentry/types/organization';
  13. import {getUtcDateString} from 'sentry/utils/dates';
  14. import {FieldKind} from 'sentry/utils/fields';
  15. import useApi from 'sentry/utils/useApi';
  16. import usePageFilters from 'sentry/utils/usePageFilters';
  17. import {Dataset} from 'sentry/views/alerts/rules/metric/types';
  18. import {mergeAndSortTagValues} from 'sentry/views/issueDetails/utils';
  19. import {makeGetIssueTagValues} from 'sentry/views/issueList/utils/getIssueTagValues';
  20. import {useFetchIssueTags} from 'sentry/views/issueList/utils/useFetchIssueTags';
  21. const getFilterKeySections = (tags: TagCollection): FilterKeySection[] => {
  22. const allTags: Tag[] = Object.values(tags).filter(
  23. tag => !EXCLUDED_TAGS.includes(tag.key)
  24. );
  25. const issueFields = orderBy(
  26. allTags.filter(tag => tag.kind === FieldKind.ISSUE_FIELD),
  27. ['key']
  28. ).map(tag => tag.key);
  29. const eventFields = orderBy(
  30. allTags.filter(tag => tag.kind === FieldKind.EVENT_FIELD),
  31. ['key']
  32. ).map(tag => tag.key);
  33. const eventTags = orderBy(
  34. allTags.filter(tag => tag.kind === FieldKind.TAG),
  35. ['totalValues', 'key'],
  36. ['desc', 'asc']
  37. ).map(tag => tag.key);
  38. return [
  39. {
  40. value: FieldKind.ISSUE_FIELD,
  41. label: t('Issues'),
  42. children: issueFields,
  43. },
  44. {
  45. value: FieldKind.EVENT_FIELD,
  46. label: t('Event Filters'),
  47. children: eventFields,
  48. },
  49. {
  50. value: FieldKind.TAG,
  51. label: t('Event Tags'),
  52. children: eventTags,
  53. },
  54. ];
  55. };
  56. interface Props extends Partial<SearchQueryBuilderProps> {
  57. organization: Organization;
  58. }
  59. const EXCLUDED_TAGS = ['environment'];
  60. function IssueListSearchBar({
  61. organization,
  62. searchSource = 'issues',
  63. initialQuery = '',
  64. ...props
  65. }: Props) {
  66. const api = useApi();
  67. const {selection: pageFilters} = usePageFilters();
  68. const {tags: issueTags} = useFetchIssueTags({
  69. org: organization,
  70. projectIds: pageFilters.projects.map(id => id.toString()),
  71. keepPreviousData: true,
  72. start: pageFilters.datetime.start
  73. ? getUtcDateString(pageFilters.datetime.start)
  74. : undefined,
  75. end: pageFilters.datetime.end
  76. ? getUtcDateString(pageFilters.datetime.end)
  77. : undefined,
  78. statsPeriod: pageFilters.datetime.period,
  79. });
  80. const tagValueLoader = useCallback(
  81. async (key: string, search: string) => {
  82. const orgSlug = organization.slug;
  83. const projectIds = pageFilters.projects.map(id => id.toString());
  84. const endpointParams = {
  85. start: pageFilters.datetime.start
  86. ? getUtcDateString(pageFilters.datetime.start)
  87. : undefined,
  88. end: pageFilters.datetime.end
  89. ? getUtcDateString(pageFilters.datetime.end)
  90. : undefined,
  91. statsPeriod: pageFilters.datetime.period,
  92. };
  93. const fetchTagValuesPayload = {
  94. api,
  95. orgSlug,
  96. tagKey: key,
  97. search,
  98. projectIds,
  99. endpointParams,
  100. sort: '-count' as const,
  101. };
  102. const [eventsDatasetValues, issuePlatformDatasetValues] = await Promise.all([
  103. fetchTagValues({
  104. ...fetchTagValuesPayload,
  105. dataset: Dataset.ERRORS,
  106. }),
  107. fetchTagValues({
  108. ...fetchTagValuesPayload,
  109. dataset: Dataset.ISSUE_PLATFORM,
  110. }),
  111. ]);
  112. return mergeAndSortTagValues(
  113. eventsDatasetValues,
  114. issuePlatformDatasetValues,
  115. 'count'
  116. );
  117. },
  118. [
  119. api,
  120. organization.slug,
  121. pageFilters.datetime.end,
  122. pageFilters.datetime.period,
  123. pageFilters.datetime.start,
  124. pageFilters.projects,
  125. ]
  126. );
  127. const getTagValues = useMemo(
  128. () => makeGetIssueTagValues(tagValueLoader),
  129. [tagValueLoader]
  130. );
  131. const filterKeySections = useMemo(() => {
  132. return getFilterKeySections(issueTags);
  133. }, [issueTags]);
  134. return (
  135. <SearchQueryBuilder
  136. initialQuery={initialQuery}
  137. getTagValues={getTagValues}
  138. filterKeySections={filterKeySections}
  139. filterKeys={issueTags}
  140. recentSearches={SavedSearchType.ISSUE}
  141. disallowLogicalOperators
  142. showUnsubmittedIndicator
  143. searchSource={searchSource}
  144. {...props}
  145. />
  146. );
  147. }
  148. export default IssueListSearchBar;