tabsButtonBar.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import styled from '@emotion/styled';
  2. import {
  3. openAddDashboardWidgetModal,
  4. openDashboardWidgetLibraryModal,
  5. } from 'sentry/actionCreators/modal';
  6. import FeatureBadge from 'sentry/components/featureBadge';
  7. import {t} from 'sentry/locale';
  8. import space from 'sentry/styles/space';
  9. import {Organization} from 'sentry/types';
  10. import {defined} from 'sentry/utils';
  11. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  12. import {
  13. DashboardDetails,
  14. DashboardWidgetSource,
  15. Widget,
  16. } from 'sentry/views/dashboardsV2/types';
  17. import {WidgetTemplate} from 'sentry/views/dashboardsV2/widgetLibrary/data';
  18. import Button from '../../button';
  19. import ButtonBar from '../../buttonBar';
  20. import {setWidgetLibraryVisit, shouldShowNewBadge} from './utils';
  21. export enum TAB {
  22. Library = 'library',
  23. Custom = 'custom',
  24. }
  25. type Props = {
  26. activeTab: TAB;
  27. dashboard: DashboardDetails;
  28. organization: Organization;
  29. customWidget?: Widget;
  30. onAddWidget?: (widgets: Widget[]) => void;
  31. selectedWidgets?: WidgetTemplate[];
  32. };
  33. export function TabsButtonBar({
  34. activeTab,
  35. organization,
  36. dashboard,
  37. selectedWidgets,
  38. customWidget,
  39. onAddWidget,
  40. }: Props) {
  41. return (
  42. <StyledButtonBar active={activeTab}>
  43. <CustomButton
  44. barId={TAB.Custom}
  45. onClick={() => {
  46. if (activeTab === TAB.Custom) {
  47. return;
  48. }
  49. trackAdvancedAnalyticsEvent('dashboards_views.widget_library.switch_tab', {
  50. organization,
  51. to: TAB.Custom,
  52. });
  53. openAddDashboardWidgetModal({
  54. organization,
  55. dashboard,
  56. selectedWidgets,
  57. widget: customWidget,
  58. source: DashboardWidgetSource.LIBRARY,
  59. onAddLibraryWidget: onAddWidget,
  60. });
  61. }}
  62. >
  63. {t('Custom Widget')}
  64. </CustomButton>
  65. <LibraryButton
  66. barId={TAB.Library}
  67. data-test-id="library-tab"
  68. onClick={() => {
  69. if (activeTab === TAB.Library) {
  70. return;
  71. }
  72. trackAdvancedAnalyticsEvent('dashboards_views.widget_library.switch_tab', {
  73. organization,
  74. to: TAB.Library,
  75. });
  76. setWidgetLibraryVisit();
  77. if (defined(onAddWidget)) {
  78. openDashboardWidgetLibraryModal({
  79. organization,
  80. dashboard,
  81. customWidget,
  82. initialSelectedWidgets: selectedWidgets,
  83. onAddWidget,
  84. });
  85. }
  86. }}
  87. >
  88. {t('Widget Library')}
  89. {shouldShowNewBadge() && <FeatureBadge type="new" />}
  90. </LibraryButton>
  91. </StyledButtonBar>
  92. );
  93. }
  94. const StyledButtonBar = styled(ButtonBar)`
  95. display: inline-flex;
  96. margin-bottom: ${space(2)};
  97. `;
  98. const LibraryButton = styled(Button)`
  99. border-top-left-radius: 0;
  100. border-bottom-left-radius: 0;
  101. `;
  102. const CustomButton = styled(Button)`
  103. border-top-right-radius: 0;
  104. border-bottom-right-radius: 0;
  105. line-height: 17px;
  106. `;