controls.tsx 6.7 KB

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