searchBar.tsx 7.5 KB

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