controls.tsx 6.9 KB

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