controls.tsx 7.1 KB

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