searchBar.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 {useIssueListFilterKeys} from 'sentry/views/issueList/utils/useIssueListFilterKeys';
  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 filterKeys = useIssueListFilterKeys();
  69. const tagValueLoader = useCallback(
  70. async (key: string, search: string) => {
  71. const orgSlug = organization.slug;
  72. const projectIds = pageFilters.projects.map(id => id.toString());
  73. const endpointParams = {
  74. start: pageFilters.datetime.start
  75. ? getUtcDateString(pageFilters.datetime.start)
  76. : undefined,
  77. end: pageFilters.datetime.end
  78. ? getUtcDateString(pageFilters.datetime.end)
  79. : undefined,
  80. statsPeriod: pageFilters.datetime.period,
  81. };
  82. const fetchTagValuesPayload = {
  83. api,
  84. orgSlug,
  85. tagKey: key,
  86. search,
  87. projectIds,
  88. endpointParams,
  89. sort: '-count' as const,
  90. };
  91. const [eventsDatasetValues, issuePlatformDatasetValues] = await Promise.all([
  92. fetchTagValues({
  93. ...fetchTagValuesPayload,
  94. dataset: Dataset.ERRORS,
  95. }),
  96. fetchTagValues({
  97. ...fetchTagValuesPayload,
  98. dataset: Dataset.ISSUE_PLATFORM,
  99. }),
  100. ]);
  101. return mergeAndSortTagValues(
  102. eventsDatasetValues,
  103. issuePlatformDatasetValues,
  104. 'count'
  105. );
  106. },
  107. [
  108. api,
  109. organization.slug,
  110. pageFilters.datetime.end,
  111. pageFilters.datetime.period,
  112. pageFilters.datetime.start,
  113. pageFilters.projects,
  114. ]
  115. );
  116. const getTagValues = useMemo(
  117. () => makeGetIssueTagValues(tagValueLoader),
  118. [tagValueLoader]
  119. );
  120. const filterKeySections = useMemo(() => {
  121. return getFilterKeySections(filterKeys);
  122. }, [filterKeys]);
  123. return (
  124. <SearchQueryBuilder
  125. initialQuery={initialQuery}
  126. getTagValues={getTagValues}
  127. filterKeySections={filterKeySections}
  128. filterKeys={filterKeys}
  129. recentSearches={SavedSearchType.ISSUE}
  130. disallowLogicalOperators
  131. showUnsubmittedIndicator
  132. searchSource={searchSource}
  133. {...props}
  134. />
  135. );
  136. }
  137. export default IssueListSearchBar;