searchBar.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import {useCallback, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import orderBy from 'lodash/orderBy';
  4. // eslint-disable-next-line no-restricted-imports
  5. import {fetchTagValues} from 'sentry/actionCreators/tags';
  6. import {SearchQueryBuilder} from 'sentry/components/searchQueryBuilder';
  7. import type {FilterKeySection} from 'sentry/components/searchQueryBuilder/types';
  8. import SmartSearchBar from 'sentry/components/smartSearchBar';
  9. import type {SearchGroup} from 'sentry/components/smartSearchBar/types';
  10. import {ItemType} from 'sentry/components/smartSearchBar/types';
  11. import {IconStar} from 'sentry/icons';
  12. import {t} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import {SavedSearchType, type Tag, type TagCollection} from 'sentry/types/group';
  15. import type {Organization} from 'sentry/types/organization';
  16. import {getUtcDateString} from 'sentry/utils/dates';
  17. import {FieldKind, getFieldDefinition} from 'sentry/utils/fields';
  18. import useApi from 'sentry/utils/useApi';
  19. import usePageFilters from 'sentry/utils/usePageFilters';
  20. import type {WithIssueTagsProps} from 'sentry/utils/withIssueTags';
  21. import withIssueTags from 'sentry/utils/withIssueTags';
  22. import {Dataset} from 'sentry/views/alerts/rules/metric/types';
  23. import {mergeAndSortTagValues} from 'sentry/views/issueDetails/utils';
  24. import {makeGetIssueTagValues} from 'sentry/views/issueList/utils/getIssueTagValues';
  25. import {useFetchIssueTags} from 'sentry/views/issueList/utils/useFetchIssueTags';
  26. const getSupportedTags = (supportedTags: TagCollection): TagCollection => {
  27. return Object.fromEntries(
  28. Object.keys(supportedTags).map(key => [
  29. key,
  30. {
  31. ...supportedTags[key],
  32. kind:
  33. getFieldDefinition(key)?.kind ??
  34. (supportedTags[key].predefined ? FieldKind.FIELD : FieldKind.TAG),
  35. },
  36. ])
  37. );
  38. };
  39. const getFilterKeySections = (
  40. tags: TagCollection,
  41. organization: Organization
  42. ): FilterKeySection[] => {
  43. if (!organization.features.includes('issue-stream-search-query-builder')) {
  44. return [];
  45. }
  46. const allTags: Tag[] = Object.values(tags).filter(
  47. tag => !EXCLUDED_TAGS.includes(tag.key)
  48. );
  49. const issueFields = orderBy(
  50. allTags.filter(tag => tag.kind === FieldKind.ISSUE_FIELD),
  51. ['key']
  52. ).map(tag => tag.key);
  53. const eventFields = orderBy(
  54. allTags.filter(tag => tag.kind === FieldKind.EVENT_FIELD),
  55. ['key']
  56. ).map(tag => tag.key);
  57. const eventTags = orderBy(
  58. allTags.filter(tag => tag.kind === FieldKind.TAG),
  59. ['totalValues', 'key'],
  60. ['desc', 'asc']
  61. ).map(tag => tag.key);
  62. return [
  63. {
  64. value: FieldKind.ISSUE_FIELD,
  65. label: t('Issues'),
  66. children: issueFields,
  67. },
  68. {
  69. value: FieldKind.EVENT_FIELD,
  70. label: t('Event Filters'),
  71. children: eventFields,
  72. },
  73. {
  74. value: FieldKind.TAG,
  75. label: t('Event Tags'),
  76. children: eventTags,
  77. },
  78. ];
  79. };
  80. interface Props extends React.ComponentProps<typeof SmartSearchBar>, WithIssueTagsProps {
  81. organization: Organization;
  82. }
  83. const EXCLUDED_TAGS = ['environment'];
  84. function IssueListSearchBar({organization, tags, onClose, ...props}: Props) {
  85. const api = useApi();
  86. const {selection: pageFilters} = usePageFilters();
  87. const {tags: issueTags} = useFetchIssueTags({
  88. org: organization,
  89. projectIds: pageFilters.projects.map(id => id.toString()),
  90. keepPreviousData: true,
  91. enabled: organization.features.includes('issue-stream-search-query-builder'),
  92. start: pageFilters.datetime.start
  93. ? getUtcDateString(pageFilters.datetime.start)
  94. : undefined,
  95. end: pageFilters.datetime.end
  96. ? getUtcDateString(pageFilters.datetime.end)
  97. : undefined,
  98. statsPeriod: pageFilters.datetime.period,
  99. });
  100. const tagValueLoader = useCallback(
  101. async (key: string, search: string) => {
  102. const orgSlug = organization.slug;
  103. const projectIds = pageFilters.projects.map(id => id.toString());
  104. const endpointParams = {
  105. start: pageFilters.datetime.start
  106. ? getUtcDateString(pageFilters.datetime.start)
  107. : undefined,
  108. end: pageFilters.datetime.end
  109. ? getUtcDateString(pageFilters.datetime.end)
  110. : undefined,
  111. statsPeriod: pageFilters.datetime.period,
  112. };
  113. const fetchTagValuesPayload = {
  114. api,
  115. orgSlug,
  116. tagKey: key,
  117. search,
  118. projectIds,
  119. endpointParams,
  120. sort: '-count' as const,
  121. };
  122. const [eventsDatasetValues, issuePlatformDatasetValues] = await Promise.all([
  123. fetchTagValues({
  124. ...fetchTagValuesPayload,
  125. dataset: Dataset.ERRORS,
  126. }),
  127. fetchTagValues({
  128. ...fetchTagValuesPayload,
  129. dataset: Dataset.ISSUE_PLATFORM,
  130. }),
  131. ]);
  132. return mergeAndSortTagValues(
  133. eventsDatasetValues,
  134. issuePlatformDatasetValues,
  135. 'count'
  136. );
  137. },
  138. [
  139. api,
  140. organization.slug,
  141. pageFilters.datetime.end,
  142. pageFilters.datetime.period,
  143. pageFilters.datetime.start,
  144. pageFilters.projects,
  145. ]
  146. );
  147. const getTagValues = useMemo(
  148. () => makeGetIssueTagValues(tagValueLoader),
  149. [tagValueLoader]
  150. );
  151. const recommendedGroup: SearchGroup = {
  152. title: t('Popular Filters'),
  153. type: 'header',
  154. icon: <IconStar size="xs" />,
  155. childrenWrapper: RecommendedWrapper,
  156. children: [
  157. {
  158. type: ItemType.RECOMMENDED,
  159. kind: FieldKind.FIELD,
  160. title: t('Issue Category'),
  161. value: 'issue.category:',
  162. },
  163. {
  164. type: ItemType.RECOMMENDED,
  165. kind: FieldKind.FIELD,
  166. title: t('Error Level'),
  167. value: 'level:',
  168. },
  169. {
  170. type: ItemType.RECOMMENDED,
  171. kind: FieldKind.FIELD,
  172. title: t('Assignee'),
  173. value: 'assigned_or_suggested:',
  174. },
  175. {
  176. type: ItemType.RECOMMENDED,
  177. kind: FieldKind.FIELD,
  178. title: t('Unhandled Events'),
  179. value: 'error.unhandled:true ',
  180. },
  181. {
  182. type: ItemType.RECOMMENDED,
  183. kind: FieldKind.FIELD,
  184. title: t('Latest Release'),
  185. value: 'release:latest ',
  186. },
  187. {
  188. type: ItemType.RECOMMENDED,
  189. kind: FieldKind.TAG,
  190. title: t('Custom Tags'),
  191. // Shows only tags when clicked
  192. applyFilter: item => item.kind === FieldKind.TAG,
  193. },
  194. ],
  195. };
  196. const filterKeySections = useMemo(() => {
  197. return getFilterKeySections(issueTags, organization);
  198. }, [organization, issueTags]);
  199. const onChange = useCallback(
  200. (value: string) => {
  201. onClose?.(value, {validSearch: true});
  202. },
  203. [onClose]
  204. );
  205. if (organization.features.includes('issue-stream-search-query-builder')) {
  206. return (
  207. <SearchQueryBuilder
  208. className={props.className}
  209. initialQuery={props.query ?? ''}
  210. getTagValues={getTagValues}
  211. filterKeySections={filterKeySections}
  212. filterKeys={issueTags}
  213. onSearch={props.onSearch}
  214. onBlur={props.onBlur}
  215. onChange={onChange}
  216. searchSource={props.searchSource ?? 'issues'}
  217. recentSearches={SavedSearchType.ISSUE}
  218. disallowLogicalOperators
  219. placeholder={props.placeholder}
  220. />
  221. );
  222. }
  223. return (
  224. <SmartSearchBar
  225. hasRecentSearches
  226. projectIds={pageFilters.projects}
  227. savedSearchType={SavedSearchType.ISSUE}
  228. onGetTagValues={getTagValues}
  229. excludedTags={EXCLUDED_TAGS}
  230. maxMenuHeight={500}
  231. supportedTags={getSupportedTags(tags)}
  232. defaultSearchGroup={recommendedGroup}
  233. organization={organization}
  234. onClose={onClose}
  235. {...props}
  236. />
  237. );
  238. }
  239. export default withIssueTags(IssueListSearchBar);
  240. // Using grid-template-rows to order the items top to bottom, then left to right
  241. const RecommendedWrapper = styled('div')`
  242. display: grid;
  243. grid-template-rows: 1fr 1fr 1fr;
  244. grid-auto-flow: column;
  245. gap: ${space(1)};
  246. padding: ${space(1)};
  247. text-align: left;
  248. line-height: 1.2;
  249. & > li {
  250. ${p => p.theme.overflowEllipsis}
  251. border-radius: ${p => p.theme.borderRadius};
  252. border: 1px solid ${p => p.theme.border};
  253. padding: ${space(1)} ${space(1.5)};
  254. margin: 0;
  255. }
  256. @media (min-width: ${p => p.theme.breakpoints.small}) {
  257. grid-template-rows: 1fr 1fr;
  258. gap: ${space(1.5)};
  259. padding: ${space(1.5)};
  260. text-align: center;
  261. & > li {
  262. padding: ${space(1.5)} ${space(2)};
  263. }
  264. }
  265. `;