tabsButtonBar.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. export enum TAB {
  22. Library = 'library',
  23. Custom = 'custom',
  24. }
  25. type Props = {
  26. activeTab: TAB;
  27. organization: Organization;
  28. dashboard: DashboardDetails;
  29. selectedWidgets?: WidgetTemplate[];
  30. customWidget?: Widget;
  31. onAddWidget?: (widgets: Widget[]) => void;
  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. if (defined(onAddWidget)) {
  77. openDashboardWidgetLibraryModal({
  78. organization,
  79. dashboard,
  80. customWidget,
  81. initialSelectedWidgets: selectedWidgets,
  82. onAddWidget,
  83. });
  84. }
  85. }}
  86. >
  87. {t('Widget Library')}
  88. <FeatureBadge type="beta" />
  89. </LibraryButton>
  90. </StyledButtonBar>
  91. );
  92. }
  93. const StyledButtonBar = styled(ButtonBar)`
  94. display: inline-flex;
  95. margin-bottom: ${space(2)};
  96. `;
  97. const LibraryButton = styled(Button)`
  98. border-top-left-radius: 0;
  99. border-bottom-left-radius: 0;
  100. `;
  101. const CustomButton = styled(Button)`
  102. border-top-right-radius: 0;
  103. border-bottom-right-radius: 0;
  104. line-height: 17px;
  105. `;