issueListSetAsDefault.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // eslint-disable-next-line no-restricted-imports
  2. import {browserHistory, withRouter, WithRouterProps} from 'react-router';
  3. import isNil from 'lodash/isNil';
  4. import {pinSearch, unpinSearch} from 'sentry/actionCreators/savedSearches';
  5. import Button from 'sentry/components/button';
  6. import {removeSpace} from 'sentry/components/smartSearchBar/utils';
  7. import {IconBookmark} from 'sentry/icons';
  8. import {t} from 'sentry/locale';
  9. import {Organization, SavedSearch, SavedSearchType} from 'sentry/types';
  10. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  11. import useApi from 'sentry/utils/useApi';
  12. interface IssueListSetAsDefaultProps extends WithRouterProps {
  13. organization: Organization;
  14. query: string;
  15. savedSearch: SavedSearch | null;
  16. sort: string;
  17. }
  18. const IssueListSetAsDefault = ({
  19. location,
  20. organization,
  21. savedSearch,
  22. sort,
  23. query,
  24. }: IssueListSetAsDefaultProps) => {
  25. const api = useApi();
  26. const pinnedSearch = savedSearch?.isPinned ? savedSearch : undefined;
  27. const pinnedSearchActive = !isNil(pinnedSearch);
  28. const onTogglePinnedSearch = async () => {
  29. const {cursor: _cursor, page: _page, ...currentQuery} = location.query;
  30. trackAdvancedAnalyticsEvent('search.pin', {
  31. organization,
  32. action: pinnedSearch ? 'unpin' : 'pin',
  33. search_type: 'issues',
  34. query: pinnedSearch?.query ?? query,
  35. });
  36. if (pinnedSearch) {
  37. await unpinSearch(api, organization.slug, SavedSearchType.ISSUE, pinnedSearch);
  38. browserHistory.push({
  39. ...location,
  40. pathname: `/organizations/${organization.slug}/issues/`,
  41. query: {
  42. referrer: 'search-bar',
  43. ...currentQuery,
  44. query: pinnedSearch.query,
  45. sort: pinnedSearch.sort,
  46. },
  47. });
  48. return;
  49. }
  50. const resp = await pinSearch(
  51. api,
  52. organization.slug,
  53. SavedSearchType.ISSUE,
  54. removeSpace(query),
  55. sort
  56. );
  57. if (!resp || !resp.id) {
  58. return;
  59. }
  60. browserHistory.push({
  61. ...location,
  62. pathname: `/organizations/${organization.slug}/issues/searches/${resp.id}/`,
  63. query: {referrer: 'search-bar', ...currentQuery},
  64. });
  65. };
  66. if (!organization.features.includes('issue-list-saved-searches-v2')) {
  67. return null;
  68. }
  69. return (
  70. <Button
  71. onClick={onTogglePinnedSearch}
  72. size="sm"
  73. icon={<IconBookmark isSolid={pinnedSearchActive} />}
  74. >
  75. {pinnedSearchActive ? t('Remove Default') : t('Set as Default')}
  76. </Button>
  77. );
  78. };
  79. export default withRouter(IssueListSetAsDefault);