searchBar.tsx 2.2 KB

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