searchBar.tsx 7.6 KB

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