searchBar.tsx 8.1 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 {
  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 [eventsDatasetValues, issuePlatformDatasetValues] = await Promise.all([
  118. fetchTagValues({
  119. api,
  120. orgSlug,
  121. tagKey: key,
  122. search,
  123. projectIds,
  124. endpointParams,
  125. dataset: Dataset.ERRORS,
  126. }),
  127. fetchTagValues({
  128. api,
  129. orgSlug,
  130. tagKey: key,
  131. search,
  132. projectIds,
  133. endpointParams,
  134. dataset: Dataset.ISSUE_PLATFORM,
  135. }),
  136. ]);
  137. return mergeAndSortTagValues(eventsDatasetValues, issuePlatformDatasetValues);
  138. },
  139. [
  140. api,
  141. organization.slug,
  142. pageFilters.datetime.end,
  143. pageFilters.datetime.period,
  144. pageFilters.datetime.start,
  145. pageFilters.projects,
  146. ]
  147. );
  148. const getTagValues = useMemo(
  149. () => makeGetIssueTagValues(tagValueLoader),
  150. [tagValueLoader]
  151. );
  152. const recommendedGroup: SearchGroup = {
  153. title: t('Popular Filters'),
  154. type: 'header',
  155. icon: <IconStar size="xs" />,
  156. childrenWrapper: RecommendedWrapper,
  157. children: [
  158. {
  159. type: ItemType.RECOMMENDED,
  160. kind: FieldKind.FIELD,
  161. title: t('Issue Category'),
  162. value: 'issue.category:',
  163. },
  164. {
  165. type: ItemType.RECOMMENDED,
  166. kind: FieldKind.FIELD,
  167. title: t('Error Level'),
  168. value: 'level:',
  169. },
  170. {
  171. type: ItemType.RECOMMENDED,
  172. kind: FieldKind.FIELD,
  173. title: t('Assignee'),
  174. value: 'assigned_or_suggested:',
  175. },
  176. {
  177. type: ItemType.RECOMMENDED,
  178. kind: FieldKind.FIELD,
  179. title: t('Unhandled Events'),
  180. value: 'error.unhandled:true ',
  181. },
  182. {
  183. type: ItemType.RECOMMENDED,
  184. kind: FieldKind.FIELD,
  185. title: t('Latest Release'),
  186. value: 'release:latest ',
  187. },
  188. {
  189. type: ItemType.RECOMMENDED,
  190. kind: FieldKind.TAG,
  191. title: t('Custom Tags'),
  192. // Shows only tags when clicked
  193. applyFilter: item => item.kind === FieldKind.TAG,
  194. },
  195. ],
  196. };
  197. const filterKeySections = useMemo(() => {
  198. return getFilterKeySections(issueTags, organization);
  199. }, [organization, issueTags]);
  200. const onChange = useCallback(
  201. (value: string) => {
  202. onClose?.(value, {validSearch: true});
  203. },
  204. [onClose]
  205. );
  206. if (organization.features.includes('issue-stream-search-query-builder')) {
  207. return (
  208. <SearchQueryBuilder
  209. className={props.className}
  210. initialQuery={props.query ?? ''}
  211. getTagValues={getTagValues}
  212. filterKeySections={filterKeySections}
  213. filterKeys={issueTags}
  214. onSearch={props.onSearch}
  215. onBlur={props.onBlur}
  216. onChange={onChange}
  217. searchSource={props.searchSource ?? 'issues'}
  218. recentSearches={SavedSearchType.ISSUE}
  219. disallowLogicalOperators
  220. placeholder={props.placeholder}
  221. />
  222. );
  223. }
  224. return (
  225. <SmartSearchBar
  226. hasRecentSearches
  227. projectIds={pageFilters.projects}
  228. savedSearchType={SavedSearchType.ISSUE}
  229. onGetTagValues={getTagValues}
  230. excludedTags={EXCLUDED_TAGS}
  231. maxMenuHeight={500}
  232. supportedTags={getSupportedTags(tags)}
  233. defaultSearchGroup={recommendedGroup}
  234. organization={organization}
  235. onClose={onClose}
  236. {...props}
  237. />
  238. );
  239. }
  240. export default withIssueTags(IssueListSearchBar);
  241. // Using grid-template-rows to order the items top to bottom, then left to right
  242. const RecommendedWrapper = styled('div')`
  243. display: grid;
  244. grid-template-rows: 1fr 1fr 1fr;
  245. grid-auto-flow: column;
  246. gap: ${space(1)};
  247. padding: ${space(1)};
  248. text-align: left;
  249. line-height: 1.2;
  250. & > li {
  251. ${p => p.theme.overflowEllipsis}
  252. border-radius: ${p => p.theme.borderRadius};
  253. border: 1px solid ${p => p.theme.border};
  254. padding: ${space(1)} ${space(1.5)};
  255. margin: 0;
  256. }
  257. @media (min-width: ${p => p.theme.breakpoints.small}) {
  258. grid-template-rows: 1fr 1fr;
  259. gap: ${space(1.5)};
  260. padding: ${space(1.5)};
  261. text-align: center;
  262. & > li {
  263. padding: ${space(1.5)} ${space(2)};
  264. }
  265. }
  266. `;