searchBar.tsx 7.1 KB

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