controls.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import * as React from 'react';
  2. import styled from '@emotion/styled';
  3. import Feature from 'sentry/components/acl/feature';
  4. import FeatureDisabled from 'sentry/components/acl/featureDisabled';
  5. import Button from 'sentry/components/button';
  6. import ButtonBar from 'sentry/components/buttonBar';
  7. import Confirm from 'sentry/components/confirm';
  8. import {Hovercard} from 'sentry/components/hovercard';
  9. import Tooltip from 'sentry/components/tooltip';
  10. import {IconAdd, IconEdit} from 'sentry/icons';
  11. import {t, tct} from 'sentry/locale';
  12. import space from 'sentry/styles/space';
  13. import {Organization} from 'sentry/types';
  14. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  15. import {DashboardListItem, DashboardState, MAX_WIDGETS} from './types';
  16. type Props = {
  17. dashboardState: DashboardState;
  18. dashboards: DashboardListItem[];
  19. onAddWidget: () => void;
  20. onCancel: () => void;
  21. onCommit: () => void;
  22. onDelete: () => void;
  23. onEdit: () => void;
  24. organization: Organization;
  25. widgetLimitReached: boolean;
  26. };
  27. class Controls extends React.Component<Props> {
  28. renderCancelButton(label = t('Cancel')) {
  29. const {onCancel} = this.props;
  30. return (
  31. <Button
  32. data-test-id="dashboard-cancel"
  33. onClick={e => {
  34. e.preventDefault();
  35. onCancel();
  36. }}
  37. >
  38. {label}
  39. </Button>
  40. );
  41. }
  42. render() {
  43. const {
  44. organization,
  45. dashboardState,
  46. dashboards,
  47. widgetLimitReached,
  48. onEdit,
  49. onCommit,
  50. onDelete,
  51. onAddWidget,
  52. } = this.props;
  53. if ([DashboardState.EDIT, DashboardState.PENDING_DELETE].includes(dashboardState)) {
  54. return (
  55. <StyledButtonBar gap={1} key="edit-controls">
  56. {this.renderCancelButton()}
  57. <Confirm
  58. priority="danger"
  59. message={t('Are you sure you want to delete this dashboard?')}
  60. onConfirm={onDelete}
  61. disabled={dashboards.length <= 1}
  62. >
  63. <Button data-test-id="dashboard-delete" priority="danger">
  64. {t('Delete')}
  65. </Button>
  66. </Confirm>
  67. <Button
  68. data-test-id="dashboard-commit"
  69. onClick={e => {
  70. e.preventDefault();
  71. onCommit();
  72. }}
  73. priority="primary"
  74. >
  75. {t('Save and Finish')}
  76. </Button>
  77. </StyledButtonBar>
  78. );
  79. }
  80. if (dashboardState === DashboardState.CREATE) {
  81. return (
  82. <StyledButtonBar gap={1} key="create-controls">
  83. {this.renderCancelButton()}
  84. <Button
  85. data-test-id="dashboard-commit"
  86. onClick={e => {
  87. e.preventDefault();
  88. onCommit();
  89. }}
  90. priority="primary"
  91. >
  92. {t('Save and Finish')}
  93. </Button>
  94. </StyledButtonBar>
  95. );
  96. }
  97. if (dashboardState === DashboardState.PREVIEW) {
  98. return (
  99. <StyledButtonBar gap={1} key="preview-controls">
  100. {this.renderCancelButton(t('Go Back'))}
  101. <Button
  102. data-test-id="dashboard-commit"
  103. onClick={e => {
  104. e.preventDefault();
  105. onCommit();
  106. }}
  107. priority="primary"
  108. >
  109. {t('Add Dashboard')}
  110. </Button>
  111. </StyledButtonBar>
  112. );
  113. }
  114. return (
  115. <StyledButtonBar gap={1} key="controls">
  116. <DashboardEditFeature>
  117. {hasFeature => (
  118. <React.Fragment>
  119. <Button
  120. data-test-id="dashboard-edit"
  121. onClick={e => {
  122. e.preventDefault();
  123. onEdit();
  124. }}
  125. icon={<IconEdit />}
  126. disabled={!hasFeature}
  127. priority={
  128. organization.features.includes('widget-library') ? 'default' : 'primary'
  129. }
  130. >
  131. {t('Edit Dashboard')}
  132. </Button>
  133. {organization.features.includes('widget-library') && hasFeature ? (
  134. <Tooltip
  135. title={tct('Max widgets ([maxWidgets]) per dashboard reached.', {
  136. maxWidgets: MAX_WIDGETS,
  137. })}
  138. disabled={!!!widgetLimitReached}
  139. >
  140. <Button
  141. data-test-id="add-widget-library"
  142. priority="primary"
  143. disabled={widgetLimitReached}
  144. icon={<IconAdd isCircled />}
  145. onClick={() => {
  146. trackAdvancedAnalyticsEvent(
  147. 'dashboards_views.widget_library.opened',
  148. {
  149. organization,
  150. }
  151. );
  152. onAddWidget();
  153. }}
  154. >
  155. {t('Add Widget')}
  156. </Button>
  157. </Tooltip>
  158. ) : null}
  159. </React.Fragment>
  160. )}
  161. </DashboardEditFeature>
  162. </StyledButtonBar>
  163. );
  164. }
  165. }
  166. const DashboardEditFeature = ({
  167. children,
  168. }: {
  169. children: (hasFeature: boolean) => React.ReactNode;
  170. }) => {
  171. const noFeatureMessage = t('Requires dashboard editing.');
  172. const renderDisabled = p => (
  173. <Hovercard
  174. body={
  175. <FeatureDisabled
  176. features={p.features}
  177. hideHelpToggle
  178. message={noFeatureMessage}
  179. featureName={noFeatureMessage}
  180. />
  181. }
  182. >
  183. {p.children(p)}
  184. </Hovercard>
  185. );
  186. return (
  187. <Feature
  188. hookName="feature-disabled:dashboards-edit"
  189. features={['organizations:dashboards-edit']}
  190. renderDisabled={renderDisabled}
  191. >
  192. {({hasFeature}) => children(hasFeature)}
  193. </Feature>
  194. );
  195. };
  196. const StyledButtonBar = styled(ButtonBar)`
  197. @media (max-width: ${p => p.theme.breakpoints[0]}) {
  198. grid-auto-flow: row;
  199. grid-row-gap: ${space(1)};
  200. width: 100%;
  201. }
  202. `;
  203. export default Controls;