controls.tsx 6.0 KB

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