searchBar.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 {ItemType, SearchGroup} from 'sentry/components/smartSearchBar/types';
  7. import {IconStar} from 'sentry/icons';
  8. import {t} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import {Organization, SavedSearchType, Tag, TagCollection} from 'sentry/types';
  11. import {getUtcDateString} from 'sentry/utils/dates';
  12. import {
  13. DEVICE_CLASS_TAG_VALUES,
  14. FieldKind,
  15. getFieldDefinition,
  16. isDeviceClass,
  17. } from 'sentry/utils/fields';
  18. import useApi from 'sentry/utils/useApi';
  19. import usePageFilters from 'sentry/utils/usePageFilters';
  20. import withIssueTags, {WithIssueTagsProps} from 'sentry/utils/withIssueTags';
  21. const getSupportedTags = (supportedTags: TagCollection) =>
  22. Object.fromEntries(
  23. Object.keys(supportedTags).map(key => [
  24. key,
  25. {
  26. ...supportedTags[key],
  27. kind:
  28. getFieldDefinition(key)?.kind ??
  29. (supportedTags[key].predefined ? FieldKind.FIELD : FieldKind.TAG),
  30. },
  31. ])
  32. );
  33. interface Props extends React.ComponentProps<typeof SmartSearchBar>, WithIssueTagsProps {
  34. organization: Organization;
  35. }
  36. const EXCLUDED_TAGS = ['environment'];
  37. function IssueListSearchBar({organization, tags, ...props}: Props) {
  38. const api = useApi();
  39. const {selection: pageFilters} = usePageFilters();
  40. const tagValueLoader = useCallback(
  41. (key: string, search: string) => {
  42. const orgSlug = organization.slug;
  43. const projectIds = pageFilters.projects.map(id => id.toString());
  44. const endpointParams = {
  45. start: getUtcDateString(pageFilters.datetime.start),
  46. end: getUtcDateString(pageFilters.datetime.end),
  47. statsPeriod: pageFilters.datetime.period,
  48. };
  49. return fetchTagValues({
  50. api,
  51. orgSlug,
  52. tagKey: key,
  53. search,
  54. projectIds,
  55. endpointParams,
  56. });
  57. },
  58. [
  59. api,
  60. organization.slug,
  61. pageFilters.datetime.end,
  62. pageFilters.datetime.period,
  63. pageFilters.datetime.start,
  64. pageFilters.projects,
  65. ]
  66. );
  67. const getTagValues = useCallback(
  68. async (tag: Tag, query: string): Promise<string[]> => {
  69. // device.class is stored as "numbers" in snuba, but we want to suggest high, medium,
  70. // and low search filter values because discover maps device.class to these values.
  71. if (isDeviceClass(tag.key)) {
  72. return DEVICE_CLASS_TAG_VALUES;
  73. }
  74. const values = await tagValueLoader(tag.key, query);
  75. return values.map(({value}) => {
  76. // Truncate results to 5000 characters to avoid exceeding the max url query length
  77. // The message attribute for example can be 8192 characters.
  78. if (typeof value === 'string' && value.length > 5000) {
  79. return value.substring(0, 5000);
  80. }
  81. return value;
  82. });
  83. },
  84. [tagValueLoader]
  85. );
  86. const hasSearchShortcuts = organization.features.includes('issue-search-shortcuts');
  87. const recommendedGroup: SearchGroup = {
  88. title: t('Recommended'),
  89. type: 'header',
  90. icon: <IconStar size="xs" />,
  91. childrenWrapper: RecommendedWrapper,
  92. children: [
  93. {
  94. type: ItemType.RECOMMENDED,
  95. kind: FieldKind.FIELD,
  96. title: t('Assignee'),
  97. desc: t('Filter by team or member.'),
  98. value: 'assigned_or_suggested:',
  99. },
  100. {
  101. type: ItemType.RECOMMENDED,
  102. kind: FieldKind.FIELD,
  103. title: t('Release'),
  104. desc: t('Filter by release version.'),
  105. value: 'release:',
  106. },
  107. {
  108. type: ItemType.RECOMMENDED,
  109. kind: FieldKind.FIELD,
  110. title: t('Level'),
  111. desc: t('Filter by fatal, error, etc.'),
  112. value: 'level:',
  113. },
  114. {
  115. type: ItemType.RECOMMENDED,
  116. kind: FieldKind.FIELD,
  117. title: t('Device'),
  118. desc: t('Filter events by device.'),
  119. value: 'device.',
  120. },
  121. {
  122. type: ItemType.RECOMMENDED,
  123. kind: FieldKind.FIELD,
  124. title: t('Unhandled'),
  125. desc: t('Filter by unhandled events.'),
  126. value: 'error.unhandled:true ',
  127. },
  128. {
  129. type: ItemType.RECOMMENDED,
  130. kind: FieldKind.TAG,
  131. title: t('Custom Tags'),
  132. desc: t('Filter events by custom tags.'),
  133. // Shows only tags when clicked
  134. applyFilter: item => item.kind === FieldKind.TAG,
  135. },
  136. ],
  137. };
  138. return (
  139. <SmartSearchBar
  140. hasRecentSearches
  141. savedSearchType={SavedSearchType.ISSUE}
  142. onGetTagValues={getTagValues}
  143. excludedTags={EXCLUDED_TAGS}
  144. maxMenuHeight={500}
  145. supportedTags={getSupportedTags(tags)}
  146. defaultSearchGroup={hasSearchShortcuts ? recommendedGroup : undefined}
  147. organization={organization}
  148. {...props}
  149. />
  150. );
  151. }
  152. export default withIssueTags(IssueListSearchBar);
  153. const RecommendedWrapper = styled('div')`
  154. display: grid;
  155. grid-template-columns: 1fr 1fr 1fr;
  156. gap: ${space(1.5)};
  157. padding: ${space(1.5)};
  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.5)} ${space(2)};
  163. margin: 0;
  164. }
  165. `;