searchBar.tsx 4.0 KB

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