searchBar.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import * as React from 'react';
  2. import {fetchRecentSearches} from 'app/actionCreators/savedSearches';
  3. import {Client} from 'app/api';
  4. import SmartSearchBar from 'app/components/smartSearchBar';
  5. import {
  6. makePinSearchAction,
  7. makeSaveSearchAction,
  8. makeSearchBuilderAction,
  9. } from 'app/components/smartSearchBar/actions';
  10. import {ItemType, SearchItem} from 'app/components/smartSearchBar/types';
  11. import {t} from 'app/locale';
  12. import {Organization, SavedSearch, SavedSearchType, Tag} from 'app/types';
  13. import withApi from 'app/utils/withApi';
  14. import withOrganization from 'app/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: '',
  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: '',
  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. organization: Organization;
  51. tagValueLoader: TagValueLoader;
  52. projectIds?: string[];
  53. savedSearch?: SavedSearch;
  54. onSidebarToggle: (e: React.MouseEvent) => void;
  55. sort: string;
  56. };
  57. type State = {
  58. defaultSearchItems: [SearchItem[], SearchItem[]];
  59. recentSearches: string[];
  60. };
  61. class IssueListSearchBar extends React.Component<Props, State> {
  62. state: State = {
  63. defaultSearchItems: [SEARCH_ITEMS, []],
  64. recentSearches: [],
  65. };
  66. componentDidMount() {
  67. // Ideally, we would fetch on demand (e.g. when input gets focus)
  68. // but `<SmartSearchBar>` is a bit complicated and this is the easiest route
  69. this.fetchData();
  70. }
  71. fetchData = async () => {
  72. this.props.api.clear();
  73. const resp = await this.getRecentSearches();
  74. this.setState({
  75. defaultSearchItems: [
  76. SEARCH_ITEMS,
  77. resp
  78. ? resp.map(query => ({
  79. desc: query,
  80. value: query,
  81. type: ItemType.RECENT_SEARCH,
  82. }))
  83. : [],
  84. ],
  85. recentSearches: resp,
  86. });
  87. };
  88. /**
  89. * @returns array of tag values that substring match `query`
  90. */
  91. getTagValues = async (tag: Tag, query: string): Promise<string[]> => {
  92. const {tagValueLoader} = this.props;
  93. const values = await tagValueLoader(tag.key, query);
  94. return values.map(({value}) => value);
  95. };
  96. getRecentSearches = async (): Promise<string[]> => {
  97. const {api, organization} = this.props;
  98. const recent = await fetchRecentSearches(
  99. api,
  100. organization.slug,
  101. SavedSearchType.ISSUE
  102. );
  103. return recent?.map(({query}) => query) ?? [];
  104. };
  105. handleSavedRecentSearch = () => {
  106. // Reset recent searches
  107. this.fetchData();
  108. };
  109. render() {
  110. const {tagValueLoader: _, savedSearch, sort, onSidebarToggle, ...props} = this.props;
  111. const pinnedSearch = savedSearch?.isPinned ? savedSearch : undefined;
  112. return (
  113. <SmartSearchBar
  114. hasRecentSearches
  115. maxSearchItems={5}
  116. savedSearchType={SavedSearchType.ISSUE}
  117. onGetTagValues={this.getTagValues}
  118. defaultSearchItems={this.state.defaultSearchItems}
  119. onSavedRecentSearch={this.handleSavedRecentSearch}
  120. actionBarItems={[
  121. makePinSearchAction({sort, pinnedSearch}),
  122. makeSaveSearchAction({sort}),
  123. makeSearchBuilderAction({onSidebarToggle}),
  124. ]}
  125. {...props}
  126. />
  127. );
  128. }
  129. }
  130. export default withApi(withOrganization(IssueListSearchBar));