controls.tsx 5.6 KB

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