searchBar.tsx 8.2 KB

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