searchBar.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import {Component} from 'react';
  2. import {Client} from 'sentry/api';
  3. import SmartSearchBar from 'sentry/components/smartSearchBar';
  4. import {
  5. makePinSearchAction,
  6. makeSaveSearchAction,
  7. makeSearchBuilderAction,
  8. } from 'sentry/components/smartSearchBar/actions';
  9. import {ItemType, SearchItem} from 'sentry/components/smartSearchBar/types';
  10. import {t} from 'sentry/locale';
  11. import {Organization, SavedSearch, SavedSearchType, Tag} from 'sentry/types';
  12. import {getFieldDoc} from 'sentry/utils/discover/fields';
  13. import withApi from 'sentry/utils/withApi';
  14. import withOrganization from 'sentry/utils/withOrganization';
  15. import {FieldValueKind} from '../eventsV2/table/types';
  16. import {TagValueLoader} from './types';
  17. const SEARCH_ITEMS: SearchItem[] = [
  18. {
  19. title: t('Tag'),
  20. desc: 'browser:"Chrome 34", has:browser',
  21. value: 'browser:',
  22. type: ItemType.DEFAULT,
  23. },
  24. {
  25. title: t('Status'),
  26. desc: 'is:resolved, unresolved, ignored, assigned, unassigned',
  27. value: 'is:',
  28. type: ItemType.DEFAULT,
  29. },
  30. {
  31. title: t('Time or Count'),
  32. desc: 'firstSeen, lastSeen, event.timestamp, timesSeen',
  33. value: 'firstSeen:',
  34. type: ItemType.DEFAULT,
  35. },
  36. {
  37. title: t('Assigned'),
  38. desc: 'assigned, assigned_or_suggested:[me|[me, none]|user@example.com|#team-example]',
  39. value: 'assigned:',
  40. type: ItemType.DEFAULT,
  41. },
  42. {
  43. title: t('Bookmarked By'),
  44. desc: 'bookmarks:[me|user@example.com]',
  45. value: 'bookmarks:',
  46. type: ItemType.DEFAULT,
  47. },
  48. ];
  49. const getSupportedTags = (supportedTags: {[key: string]: Tag}) => {
  50. const newTags = Object.fromEntries(
  51. Object.keys(supportedTags).map(key => [
  52. key,
  53. {
  54. ...supportedTags[key],
  55. kind: supportedTags[key].predefined ? FieldValueKind.FIELD : FieldValueKind.TAG,
  56. },
  57. ])
  58. );
  59. return newTags;
  60. };
  61. type Props = React.ComponentProps<typeof SmartSearchBar> & {
  62. api: Client;
  63. onSidebarToggle: (e: React.MouseEvent) => void;
  64. organization: Organization;
  65. sort: string;
  66. supportedTags: {[key: string]: Tag};
  67. tagValueLoader: TagValueLoader;
  68. /**
  69. * Used to define the max height of the menu in px.
  70. */
  71. maxMenuHeight?: number;
  72. projectIds?: string[];
  73. savedSearch?: SavedSearch;
  74. };
  75. type State = {
  76. defaultSearchItems: [SearchItem[], SearchItem[]];
  77. recentSearches: string[];
  78. supportedTags: {[key: string]: Tag};
  79. };
  80. class IssueListSearchBar extends Component<Props, State> {
  81. state: State = {
  82. defaultSearchItems: [SEARCH_ITEMS, []],
  83. recentSearches: [],
  84. supportedTags: {},
  85. };
  86. static getDerivedStateFromProps(props: Props) {
  87. return {
  88. supportedTags: getSupportedTags(props.supportedTags),
  89. };
  90. }
  91. /**
  92. * @returns array of tag values that substring match `query`
  93. */
  94. getTagValues = async (tag: Tag, query: string): Promise<string[]> => {
  95. const {tagValueLoader} = this.props;
  96. const values = await tagValueLoader(tag.key, query);
  97. return values.map(({value}) => value);
  98. };
  99. render() {
  100. const {tagValueLoader: _, savedSearch, sort, onSidebarToggle, ...props} = this.props;
  101. const pinnedSearch = savedSearch?.isPinned ? savedSearch : undefined;
  102. return (
  103. <SmartSearchBar
  104. searchSource="main_search"
  105. hasRecentSearches
  106. savedSearchType={SavedSearchType.ISSUE}
  107. onGetTagValues={this.getTagValues}
  108. actionBarItems={[
  109. makePinSearchAction({sort, pinnedSearch}),
  110. makeSaveSearchAction({sort}),
  111. makeSearchBuilderAction({onSidebarToggle}),
  112. ]}
  113. {...props}
  114. maxMenuHeight={500}
  115. supportedTags={this.state.supportedTags}
  116. getFieldDoc={getFieldDoc}
  117. />
  118. );
  119. }
  120. }
  121. export default withApi(withOrganization(IssueListSearchBar));