searchBar.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import {useCallback} from 'react';
  2. import styled from '@emotion/styled';
  3. // eslint-disable-next-line no-restricted-imports
  4. import {fetchTagValues} from 'sentry/actionCreators/tags';
  5. import SmartSearchBar from 'sentry/components/smartSearchBar';
  6. import type {SearchGroup} from 'sentry/components/smartSearchBar/types';
  7. import {ItemType} from 'sentry/components/smartSearchBar/types';
  8. import {IconStar} from 'sentry/icons';
  9. import {t} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. import type {Organization, Tag, TagCollection} from 'sentry/types';
  12. import {SavedSearchType} from 'sentry/types';
  13. import {getUtcDateString} from 'sentry/utils/dates';
  14. import {
  15. DEVICE_CLASS_TAG_VALUES,
  16. FieldKind,
  17. getFieldDefinition,
  18. isDeviceClass,
  19. } from 'sentry/utils/fields';
  20. import useApi from 'sentry/utils/useApi';
  21. import usePageFilters from 'sentry/utils/usePageFilters';
  22. import type {WithIssueTagsProps} from 'sentry/utils/withIssueTags';
  23. import withIssueTags from 'sentry/utils/withIssueTags';
  24. const getSupportedTags = (supportedTags: TagCollection) =>
  25. Object.fromEntries(
  26. Object.keys(supportedTags).map(key => [
  27. key,
  28. {
  29. ...supportedTags[key],
  30. kind:
  31. getFieldDefinition(key)?.kind ??
  32. (supportedTags[key].predefined ? FieldKind.FIELD : FieldKind.TAG),
  33. },
  34. ])
  35. );
  36. interface Props extends React.ComponentProps<typeof SmartSearchBar>, WithIssueTagsProps {
  37. organization: Organization;
  38. }
  39. const EXCLUDED_TAGS = ['environment'];
  40. function IssueListSearchBar({organization, tags, ...props}: Props) {
  41. const api = useApi();
  42. const {selection: pageFilters} = usePageFilters();
  43. const tagValueLoader = useCallback(
  44. (key: string, search: string) => {
  45. const orgSlug = organization.slug;
  46. const projectIds = pageFilters.projects.map(id => id.toString());
  47. const endpointParams = {
  48. start: getUtcDateString(pageFilters.datetime.start),
  49. end: getUtcDateString(pageFilters.datetime.end),
  50. statsPeriod: pageFilters.datetime.period,
  51. };
  52. return fetchTagValues({
  53. api,
  54. orgSlug,
  55. tagKey: key,
  56. search,
  57. projectIds,
  58. endpointParams,
  59. });
  60. },
  61. [
  62. api,
  63. organization.slug,
  64. pageFilters.datetime.end,
  65. pageFilters.datetime.period,
  66. pageFilters.datetime.start,
  67. pageFilters.projects,
  68. ]
  69. );
  70. const getTagValues = useCallback(
  71. async (tag: Tag, query: string): Promise<string[]> => {
  72. // device.class is stored as "numbers" in snuba, but we want to suggest high, medium,
  73. // and low search filter values because discover maps device.class to these values.
  74. if (isDeviceClass(tag.key)) {
  75. return DEVICE_CLASS_TAG_VALUES;
  76. }
  77. const values = await tagValueLoader(tag.key, query);
  78. return values.map(({value}) => {
  79. // Truncate results to 5000 characters to avoid exceeding the max url query length
  80. // The message attribute for example can be 8192 characters.
  81. if (typeof value === 'string' && value.length > 5000) {
  82. return value.substring(0, 5000);
  83. }
  84. return value;
  85. });
  86. },
  87. [tagValueLoader]
  88. );
  89. const recommendedGroup: SearchGroup = {
  90. title: t('Popular Filters'),
  91. type: 'header',
  92. icon: <IconStar size="xs" />,
  93. childrenWrapper: RecommendedWrapper,
  94. children: [
  95. {
  96. type: ItemType.RECOMMENDED,
  97. kind: FieldKind.FIELD,
  98. title: t('Issue Category'),
  99. value: 'issue.category:',
  100. },
  101. {
  102. type: ItemType.RECOMMENDED,
  103. kind: FieldKind.FIELD,
  104. title: t('Error Level'),
  105. value: 'level:',
  106. },
  107. {
  108. type: ItemType.RECOMMENDED,
  109. kind: FieldKind.FIELD,
  110. title: t('Assignee'),
  111. value: 'assigned_or_suggested:',
  112. },
  113. {
  114. type: ItemType.RECOMMENDED,
  115. kind: FieldKind.FIELD,
  116. title: t('Unhandled Events'),
  117. value: 'error.unhandled:true ',
  118. },
  119. {
  120. type: ItemType.RECOMMENDED,
  121. kind: FieldKind.FIELD,
  122. title: t('Latest Release'),
  123. value: 'release:latest ',
  124. },
  125. {
  126. type: ItemType.RECOMMENDED,
  127. kind: FieldKind.TAG,
  128. title: t('Custom Tags'),
  129. // Shows only tags when clicked
  130. applyFilter: item => item.kind === FieldKind.TAG,
  131. },
  132. ],
  133. };
  134. return (
  135. <SmartSearchBar
  136. hasRecentSearches
  137. savedSearchType={SavedSearchType.ISSUE}
  138. onGetTagValues={getTagValues}
  139. excludedTags={EXCLUDED_TAGS}
  140. maxMenuHeight={500}
  141. supportedTags={getSupportedTags(tags)}
  142. defaultSearchGroup={recommendedGroup}
  143. organization={organization}
  144. {...props}
  145. />
  146. );
  147. }
  148. export default withIssueTags(IssueListSearchBar);
  149. // Using grid-template-rows to order the items top to bottom, then left to right
  150. const RecommendedWrapper = styled('div')`
  151. display: grid;
  152. grid-template-rows: 1fr 1fr 1fr;
  153. grid-auto-flow: column;
  154. gap: ${space(1)};
  155. padding: ${space(1)};
  156. text-align: left;
  157. line-height: 1.2;
  158. & > li {
  159. ${p => p.theme.overflowEllipsis}
  160. border-radius: ${p => p.theme.borderRadius};
  161. border: 1px solid ${p => p.theme.border};
  162. padding: ${space(1)} ${space(1.5)};
  163. margin: 0;
  164. }
  165. @media (min-width: ${p => p.theme.breakpoints.small}) {
  166. grid-template-rows: 1fr 1fr;
  167. gap: ${space(1.5)};
  168. padding: ${space(1.5)};
  169. text-align: center;
  170. & > li {
  171. padding: ${space(1.5)} ${space(2)};
  172. }
  173. }
  174. `;