controls.tsx 5.9 KB

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