tabsButtonBar.tsx 3.0 KB

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