controls.tsx 5.6 KB

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