searchBar.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import {useCallback} from 'react';
  2. import SmartSearchBar from 'sentry/components/smartSearchBar';
  3. import {
  4. makePinSearchAction,
  5. makeSaveSearchAction,
  6. makeSearchBuilderAction,
  7. } from 'sentry/components/smartSearchBar/actions';
  8. import {SavedSearch, SavedSearchType, Tag} from 'sentry/types';
  9. import {FieldKind, getFieldDefinition} from 'sentry/utils/fields';
  10. import {TagValueLoader} from './types';
  11. const getSupportedTags = (supportedTags: {[key: string]: Tag}) =>
  12. Object.fromEntries(
  13. Object.keys(supportedTags).map(key => [
  14. key,
  15. {
  16. ...supportedTags[key],
  17. kind:
  18. getFieldDefinition(key)?.kind ??
  19. (supportedTags[key].predefined ? FieldKind.FIELD : FieldKind.TAG),
  20. },
  21. ])
  22. );
  23. interface Props extends React.ComponentProps<typeof SmartSearchBar> {
  24. onSidebarToggle: (e: React.MouseEvent) => void;
  25. sort: string;
  26. supportedTags: {[key: string]: Tag};
  27. tagValueLoader: TagValueLoader;
  28. savedSearch?: SavedSearch;
  29. }
  30. function IssueListSearchBar({
  31. onSidebarToggle,
  32. sort,
  33. supportedTags,
  34. tagValueLoader,
  35. savedSearch,
  36. ...props
  37. }: Props) {
  38. const getTagValues = useCallback(
  39. async (tag: Tag, query: string): Promise<string[]> => {
  40. const values = await tagValueLoader(tag.key, query);
  41. return values.map(({value}) => value);
  42. },
  43. [tagValueLoader]
  44. );
  45. const pinnedSearch = savedSearch?.isPinned ? savedSearch : undefined;
  46. return (
  47. <SmartSearchBar
  48. searchSource="main_search"
  49. hasRecentSearches
  50. savedSearchType={SavedSearchType.ISSUE}
  51. onGetTagValues={getTagValues}
  52. actionBarItems={[
  53. makePinSearchAction({sort, pinnedSearch}),
  54. makeSaveSearchAction({sort}),
  55. makeSearchBuilderAction({onSidebarToggle}),
  56. ]}
  57. {...props}
  58. maxMenuHeight={500}
  59. supportedTags={getSupportedTags(supportedTags)}
  60. />
  61. );
  62. }
  63. export default IssueListSearchBar;