detail.tsx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. import {cloneElement, Component, isValidElement} from 'react';
  2. import {browserHistory, PlainRoute, RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import cloneDeep from 'lodash/cloneDeep';
  5. import isEqual from 'lodash/isEqual';
  6. import omit from 'lodash/omit';
  7. import {
  8. createDashboard,
  9. deleteDashboard,
  10. updateDashboard,
  11. } from 'sentry/actionCreators/dashboards';
  12. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  13. import {
  14. openAddDashboardWidgetModal,
  15. openWidgetViewerModal,
  16. } from 'sentry/actionCreators/modal';
  17. import {Client} from 'sentry/api';
  18. import Feature from 'sentry/components/acl/feature';
  19. import Breadcrumbs from 'sentry/components/breadcrumbs';
  20. import ButtonBar from 'sentry/components/buttonBar';
  21. import DatePageFilter from 'sentry/components/datePageFilter';
  22. import EnvironmentPageFilter from 'sentry/components/environmentPageFilter';
  23. import HookOrDefault from 'sentry/components/hookOrDefault';
  24. import * as Layout from 'sentry/components/layouts/thirds';
  25. import {
  26. isWidgetViewerPath,
  27. WidgetViewerQueryField,
  28. } from 'sentry/components/modals/widgetViewerModal/utils';
  29. import NoProjectMessage from 'sentry/components/noProjectMessage';
  30. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  31. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  32. import ProjectPageFilter from 'sentry/components/projectPageFilter';
  33. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  34. import {t} from 'sentry/locale';
  35. import {PageContent} from 'sentry/styles/organization';
  36. import space from 'sentry/styles/space';
  37. import {Organization, PageFilters} from 'sentry/types';
  38. import {defined} from 'sentry/utils';
  39. import {trackAnalyticsEvent} from 'sentry/utils/analytics';
  40. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  41. import {ReleasesProvider} from 'sentry/utils/releases/releasesProvider';
  42. import withApi from 'sentry/utils/withApi';
  43. import withOrganization from 'sentry/utils/withOrganization';
  44. import withPageFilters from 'sentry/utils/withPageFilters';
  45. import {
  46. WidgetViewerContext,
  47. WidgetViewerContextProps,
  48. } from './widgetViewer/widgetViewerContext';
  49. import Controls from './controls';
  50. import Dashboard from './dashboard';
  51. import {DEFAULT_STATS_PERIOD} from './data';
  52. import {
  53. assignDefaultLayout,
  54. calculateColumnDepths,
  55. getDashboardLayout,
  56. } from './layoutUtils';
  57. import ReleasesSelectControl from './releasesSelectControl';
  58. import DashboardTitle from './title';
  59. import {
  60. DashboardDetails,
  61. DashboardFilters,
  62. DashboardListItem,
  63. DashboardState,
  64. DashboardWidgetSource,
  65. MAX_WIDGETS,
  66. Widget,
  67. WidgetType,
  68. } from './types';
  69. import {cloneDashboard, hasSavedPageFilters} from './utils';
  70. const UNSAVED_MESSAGE = t('You have unsaved changes, are you sure you want to leave?');
  71. const HookHeader = HookOrDefault({hookName: 'component:dashboards-header'});
  72. type RouteParams = {
  73. orgId: string;
  74. dashboardId?: string;
  75. widgetId?: number;
  76. widgetIndex?: number;
  77. };
  78. type Props = RouteComponentProps<RouteParams, {}> & {
  79. api: Client;
  80. dashboard: DashboardDetails;
  81. dashboards: DashboardListItem[];
  82. initialState: DashboardState;
  83. organization: Organization;
  84. route: PlainRoute;
  85. selection: PageFilters;
  86. newWidget?: Widget;
  87. onDashboardUpdate?: (updatedDashboard: DashboardDetails) => void;
  88. onSetNewWidget?: () => void;
  89. };
  90. type State = {
  91. dashboardState: DashboardState;
  92. modifiedDashboard: DashboardDetails | null;
  93. widgetLimitReached: boolean;
  94. } & WidgetViewerContextProps;
  95. class DashboardDetail extends Component<Props, State> {
  96. state: State = {
  97. dashboardState: this.props.initialState,
  98. modifiedDashboard: this.updateModifiedDashboard(this.props.initialState),
  99. widgetLimitReached: this.props.dashboard.widgets.length >= MAX_WIDGETS,
  100. setData: data => {
  101. this.setState(data);
  102. },
  103. };
  104. componentDidMount() {
  105. const {route, router} = this.props;
  106. router.setRouteLeaveHook(route, this.onRouteLeave);
  107. window.addEventListener('beforeunload', this.onUnload);
  108. this.checkIfShouldMountWidgetViewerModal();
  109. }
  110. componentDidUpdate(prevProps: Props) {
  111. this.checkIfShouldMountWidgetViewerModal();
  112. if (prevProps.initialState !== this.props.initialState) {
  113. // Widget builder can toggle Edit state when saving
  114. this.setState({dashboardState: this.props.initialState});
  115. }
  116. }
  117. componentWillUnmount() {
  118. window.removeEventListener('beforeunload', this.onUnload);
  119. }
  120. checkIfShouldMountWidgetViewerModal() {
  121. const {
  122. params: {widgetId, dashboardId},
  123. organization,
  124. dashboard,
  125. location,
  126. router,
  127. } = this.props;
  128. const {seriesData, tableData, pageLinks, totalIssuesCount} = this.state;
  129. if (isWidgetViewerPath(location.pathname)) {
  130. const widget =
  131. defined(widgetId) &&
  132. (dashboard.widgets.find(({id}) => id === String(widgetId)) ??
  133. dashboard.widgets[widgetId]);
  134. if (widget) {
  135. openWidgetViewerModal({
  136. organization,
  137. widget,
  138. seriesData,
  139. tableData,
  140. pageLinks,
  141. totalIssuesCount,
  142. onClose: () => {
  143. // Filter out Widget Viewer Modal query params when exiting the Modal
  144. const query = omit(location.query, Object.values(WidgetViewerQueryField));
  145. router.push({
  146. pathname: location.pathname.replace(/widget\/[0-9]+\/$/, ''),
  147. query,
  148. });
  149. },
  150. onEdit: () => {
  151. if (
  152. organization.features.includes('new-widget-builder-experience-design') &&
  153. !organization.features.includes(
  154. 'new-widget-builder-experience-modal-access'
  155. )
  156. ) {
  157. const widgetIndex = dashboard.widgets.indexOf(widget);
  158. if (dashboardId) {
  159. router.push({
  160. pathname: `/organizations/${organization.slug}/dashboard/${dashboardId}/widget/${widgetIndex}/edit/`,
  161. query: {
  162. ...location.query,
  163. source: DashboardWidgetSource.DASHBOARDS,
  164. },
  165. });
  166. return;
  167. }
  168. }
  169. openAddDashboardWidgetModal({
  170. organization,
  171. widget,
  172. onUpdateWidget: (nextWidget: Widget) => {
  173. const updateIndex = dashboard.widgets.indexOf(widget);
  174. const nextWidgetsList = cloneDeep(dashboard.widgets);
  175. nextWidgetsList[updateIndex] = nextWidget;
  176. this.handleUpdateWidgetList(nextWidgetsList);
  177. },
  178. source: DashboardWidgetSource.DASHBOARDS,
  179. });
  180. },
  181. });
  182. trackAdvancedAnalyticsEvent('dashboards_views.widget_viewer.open', {
  183. organization,
  184. widget_type: widget.widgetType ?? WidgetType.DISCOVER,
  185. display_type: widget.displayType,
  186. });
  187. } else {
  188. // Replace the URL if the widget isn't found and raise an error in toast
  189. router.replace({
  190. pathname: `/organizations/${organization.slug}/dashboard/${dashboard.id}/`,
  191. query: location.query,
  192. });
  193. addErrorMessage(t('Widget not found'));
  194. }
  195. }
  196. }
  197. updateModifiedDashboard(dashboardState: DashboardState) {
  198. const {dashboard} = this.props;
  199. switch (dashboardState) {
  200. case DashboardState.PREVIEW:
  201. case DashboardState.CREATE:
  202. case DashboardState.EDIT:
  203. return cloneDashboard(dashboard);
  204. default: {
  205. return null;
  206. }
  207. }
  208. }
  209. get isPreview() {
  210. const {dashboardState} = this.state;
  211. return DashboardState.PREVIEW === dashboardState;
  212. }
  213. get isEditing() {
  214. const {dashboardState} = this.state;
  215. return [
  216. DashboardState.EDIT,
  217. DashboardState.CREATE,
  218. DashboardState.PENDING_DELETE,
  219. ].includes(dashboardState);
  220. }
  221. get isWidgetBuilderRouter() {
  222. const {location, params, organization} = this.props;
  223. const {dashboardId, widgetIndex} = params;
  224. const widgetBuilderRoutes = [
  225. `/organizations/${organization.slug}/dashboards/new/widget/new/`,
  226. `/organizations/${organization.slug}/dashboard/${dashboardId}/widget/new/`,
  227. `/organizations/${organization.slug}/dashboards/new/widget/${widgetIndex}/edit/`,
  228. `/organizations/${organization.slug}/dashboard/${dashboardId}/widget/${widgetIndex}/edit/`,
  229. ];
  230. return widgetBuilderRoutes.includes(location.pathname);
  231. }
  232. get dashboardTitle() {
  233. const {dashboard} = this.props;
  234. const {modifiedDashboard} = this.state;
  235. return modifiedDashboard ? modifiedDashboard.title : dashboard.title;
  236. }
  237. onEdit = () => {
  238. const {dashboard} = this.props;
  239. trackAnalyticsEvent({
  240. eventKey: 'dashboards2.edit.start',
  241. eventName: 'Dashboards2: Edit start',
  242. organization_id: parseInt(this.props.organization.id, 10),
  243. });
  244. this.setState({
  245. dashboardState: DashboardState.EDIT,
  246. modifiedDashboard: cloneDashboard(dashboard),
  247. });
  248. };
  249. onRouteLeave = () => {
  250. const {dashboard} = this.props;
  251. const {modifiedDashboard} = this.state;
  252. if (
  253. ![
  254. DashboardState.VIEW,
  255. DashboardState.PENDING_DELETE,
  256. DashboardState.PREVIEW,
  257. ].includes(this.state.dashboardState) &&
  258. !isEqual(modifiedDashboard, dashboard)
  259. ) {
  260. return UNSAVED_MESSAGE;
  261. }
  262. return undefined;
  263. };
  264. onUnload = (event: BeforeUnloadEvent) => {
  265. const {dashboard} = this.props;
  266. const {modifiedDashboard} = this.state;
  267. if (
  268. [
  269. DashboardState.VIEW,
  270. DashboardState.PENDING_DELETE,
  271. DashboardState.PREVIEW,
  272. ].includes(this.state.dashboardState) ||
  273. isEqual(modifiedDashboard, dashboard)
  274. ) {
  275. return;
  276. }
  277. event.preventDefault();
  278. event.returnValue = UNSAVED_MESSAGE;
  279. };
  280. onDelete = (dashboard: State['modifiedDashboard']) => () => {
  281. const {api, organization, location} = this.props;
  282. if (!dashboard?.id) {
  283. return;
  284. }
  285. const previousDashboardState = this.state.dashboardState;
  286. this.setState({dashboardState: DashboardState.PENDING_DELETE}, () => {
  287. deleteDashboard(api, organization.slug, dashboard.id)
  288. .then(() => {
  289. addSuccessMessage(t('Dashboard deleted'));
  290. trackAnalyticsEvent({
  291. eventKey: 'dashboards2.delete',
  292. eventName: 'Dashboards2: Delete',
  293. organization_id: parseInt(this.props.organization.id, 10),
  294. });
  295. browserHistory.replace({
  296. pathname: `/organizations/${organization.slug}/dashboards/`,
  297. query: location.query,
  298. });
  299. })
  300. .catch(() => {
  301. this.setState({
  302. dashboardState: previousDashboardState,
  303. });
  304. });
  305. });
  306. };
  307. onCancel = () => {
  308. const {organization, dashboard, location, params} = this.props;
  309. const {modifiedDashboard} = this.state;
  310. let hasDashboardChanged = !isEqual(modifiedDashboard, dashboard);
  311. // If a dashboard has every layout undefined, then ignore the layout field
  312. // when checking equality because it is a dashboard from before the grid feature
  313. const isLegacyLayout = dashboard.widgets.every(({layout}) => !defined(layout));
  314. if (isLegacyLayout) {
  315. hasDashboardChanged = !isEqual(
  316. {
  317. ...modifiedDashboard,
  318. widgets: modifiedDashboard?.widgets.map(widget => omit(widget, 'layout')),
  319. },
  320. {...dashboard, widgets: dashboard.widgets.map(widget => omit(widget, 'layout'))}
  321. );
  322. }
  323. // Don't confirm preview cancellation regardless of dashboard state
  324. if (hasDashboardChanged && !this.isPreview) {
  325. // Ignore no-alert here, so that the confirm on cancel matches onUnload & onRouteLeave
  326. /* eslint no-alert:0 */
  327. if (!confirm(UNSAVED_MESSAGE)) {
  328. return;
  329. }
  330. }
  331. if (params.dashboardId) {
  332. trackAnalyticsEvent({
  333. eventKey: 'dashboards2.edit.cancel',
  334. eventName: 'Dashboards2: Edit cancel',
  335. organization_id: parseInt(this.props.organization.id, 10),
  336. });
  337. this.setState({
  338. dashboardState: DashboardState.VIEW,
  339. modifiedDashboard: null,
  340. });
  341. return;
  342. }
  343. trackAnalyticsEvent({
  344. eventKey: 'dashboards2.create.cancel',
  345. eventName: 'Dashboards2: Create cancel',
  346. organization_id: parseInt(this.props.organization.id, 10),
  347. });
  348. browserHistory.replace({
  349. pathname: `/organizations/${organization.slug}/dashboards/`,
  350. query: location.query,
  351. });
  352. };
  353. handleChangeFilter = (activeFilters: DashboardFilters) => {
  354. const {dashboard} = this.props;
  355. const {modifiedDashboard} = this.state;
  356. const newModifiedDashboard = modifiedDashboard || dashboard;
  357. this.setState({
  358. modifiedDashboard: {
  359. ...newModifiedDashboard,
  360. filters: {...newModifiedDashboard.filters, ...activeFilters},
  361. },
  362. });
  363. };
  364. handleUpdateWidgetList = (widgets: Widget[]) => {
  365. const {organization, dashboard, api, onDashboardUpdate, location} = this.props;
  366. const {modifiedDashboard} = this.state;
  367. // Use the new widgets for calculating layout because widgets has
  368. // the most up to date information in edit state
  369. const currentLayout = getDashboardLayout(widgets);
  370. const layoutColumnDepths = calculateColumnDepths(currentLayout);
  371. const newModifiedDashboard = {
  372. ...cloneDashboard(modifiedDashboard || dashboard),
  373. widgets: assignDefaultLayout(widgets, layoutColumnDepths),
  374. };
  375. this.setState({
  376. modifiedDashboard: newModifiedDashboard,
  377. widgetLimitReached: widgets.length >= MAX_WIDGETS,
  378. });
  379. if (this.isEditing || this.isPreview) {
  380. return;
  381. }
  382. updateDashboard(api, organization.slug, newModifiedDashboard).then(
  383. (newDashboard: DashboardDetails) => {
  384. if (onDashboardUpdate) {
  385. onDashboardUpdate(newDashboard);
  386. this.setState({
  387. modifiedDashboard: null,
  388. });
  389. }
  390. addSuccessMessage(t('Dashboard updated'));
  391. if (dashboard && newDashboard.id !== dashboard.id) {
  392. browserHistory.replace({
  393. pathname: `/organizations/${organization.slug}/dashboard/${newDashboard.id}/`,
  394. query: {
  395. ...location.query,
  396. },
  397. });
  398. return;
  399. }
  400. },
  401. () => undefined
  402. );
  403. };
  404. handleAddCustomWidget = (widget: Widget) => {
  405. const {dashboard} = this.props;
  406. const {modifiedDashboard} = this.state;
  407. const newModifiedDashboard = modifiedDashboard || dashboard;
  408. this.onUpdateWidget([...newModifiedDashboard.widgets, widget]);
  409. };
  410. onAddWidget = () => {
  411. const {
  412. organization,
  413. dashboard,
  414. router,
  415. location,
  416. params: {dashboardId},
  417. } = this.props;
  418. this.setState({
  419. modifiedDashboard: cloneDashboard(dashboard),
  420. });
  421. if (
  422. organization.features.includes('new-widget-builder-experience-design') &&
  423. !organization.features.includes('new-widget-builder-experience-modal-access')
  424. ) {
  425. if (dashboardId) {
  426. router.push({
  427. pathname: `/organizations/${organization.slug}/dashboard/${dashboardId}/widget/new/`,
  428. query: {
  429. ...location.query,
  430. source: DashboardWidgetSource.DASHBOARDS,
  431. },
  432. });
  433. return;
  434. }
  435. }
  436. openAddDashboardWidgetModal({
  437. organization,
  438. dashboard,
  439. onAddLibraryWidget: (widgets: Widget[]) => this.handleUpdateWidgetList(widgets),
  440. source: DashboardWidgetSource.LIBRARY,
  441. });
  442. };
  443. onCommit = () => {
  444. const {api, organization, location, dashboard, onDashboardUpdate} = this.props;
  445. const {modifiedDashboard, dashboardState} = this.state;
  446. switch (dashboardState) {
  447. case DashboardState.PREVIEW:
  448. case DashboardState.CREATE: {
  449. if (modifiedDashboard) {
  450. if (this.isPreview) {
  451. trackAdvancedAnalyticsEvent('dashboards_manage.templates.add', {
  452. organization,
  453. dashboard_id: dashboard.id,
  454. dashboard_title: dashboard.title,
  455. was_previewed: true,
  456. });
  457. }
  458. let newModifiedDashboard = modifiedDashboard;
  459. if (organization.features.includes('dashboards-top-level-filter')) {
  460. const {project, environment, statsPeriod, start, end} = location.query;
  461. newModifiedDashboard = {
  462. ...cloneDashboard(modifiedDashboard),
  463. // Ensure projects and environment are sent as arrays, or undefined in the request
  464. // location.query will return a string if there's only one value
  465. projects:
  466. project === undefined
  467. ? []
  468. : typeof project === 'string'
  469. ? [Number(project)]
  470. : project.map(Number),
  471. environment: typeof environment === 'string' ? [environment] : environment,
  472. period: statsPeriod,
  473. start,
  474. end,
  475. };
  476. }
  477. createDashboard(
  478. api,
  479. organization.slug,
  480. newModifiedDashboard,
  481. this.isPreview
  482. ).then(
  483. (newDashboard: DashboardDetails) => {
  484. addSuccessMessage(t('Dashboard created'));
  485. trackAnalyticsEvent({
  486. eventKey: 'dashboards2.create.complete',
  487. eventName: 'Dashboards2: Create complete',
  488. organization_id: parseInt(organization.id, 10),
  489. });
  490. this.setState({
  491. dashboardState: DashboardState.VIEW,
  492. });
  493. // redirect to new dashboard
  494. browserHistory.replace({
  495. pathname: `/organizations/${organization.slug}/dashboard/${newDashboard.id}/`,
  496. query: {
  497. ...location.query,
  498. },
  499. });
  500. },
  501. () => undefined
  502. );
  503. }
  504. break;
  505. }
  506. case DashboardState.EDIT: {
  507. // only update the dashboard if there are changes
  508. if (modifiedDashboard) {
  509. if (isEqual(dashboard, modifiedDashboard)) {
  510. this.setState({
  511. dashboardState: DashboardState.VIEW,
  512. modifiedDashboard: null,
  513. });
  514. return;
  515. }
  516. updateDashboard(api, organization.slug, modifiedDashboard).then(
  517. (newDashboard: DashboardDetails) => {
  518. if (onDashboardUpdate) {
  519. onDashboardUpdate(newDashboard);
  520. }
  521. addSuccessMessage(t('Dashboard updated'));
  522. trackAnalyticsEvent({
  523. eventKey: 'dashboards2.edit.complete',
  524. eventName: 'Dashboards2: Edit complete',
  525. organization_id: parseInt(organization.id, 10),
  526. });
  527. this.setState({
  528. dashboardState: DashboardState.VIEW,
  529. modifiedDashboard: null,
  530. });
  531. if (dashboard && newDashboard.id !== dashboard.id) {
  532. browserHistory.replace({
  533. pathname: `/organizations/${organization.slug}/dashboard/${newDashboard.id}/`,
  534. query: {
  535. ...location.query,
  536. },
  537. });
  538. return;
  539. }
  540. },
  541. () => undefined
  542. );
  543. return;
  544. }
  545. this.setState({
  546. dashboardState: DashboardState.VIEW,
  547. modifiedDashboard: null,
  548. });
  549. break;
  550. }
  551. case DashboardState.VIEW:
  552. default: {
  553. this.setState({
  554. dashboardState: DashboardState.VIEW,
  555. modifiedDashboard: null,
  556. });
  557. break;
  558. }
  559. }
  560. };
  561. setModifiedDashboard = (dashboard: DashboardDetails) => {
  562. this.setState({
  563. modifiedDashboard: dashboard,
  564. });
  565. };
  566. onUpdateWidget = (widgets: Widget[]) => {
  567. this.setState((state: State) => ({
  568. ...state,
  569. widgetLimitReached: widgets.length >= MAX_WIDGETS,
  570. modifiedDashboard: {
  571. ...(state.modifiedDashboard || this.props.dashboard),
  572. widgets,
  573. },
  574. }));
  575. };
  576. renderWidgetBuilder() {
  577. const {children, dashboard} = this.props;
  578. const {modifiedDashboard} = this.state;
  579. return isValidElement(children)
  580. ? cloneElement(children, {
  581. dashboard: modifiedDashboard ?? dashboard,
  582. onSave: this.isEditing ? this.onUpdateWidget : this.handleUpdateWidgetList,
  583. })
  584. : children;
  585. }
  586. renderDefaultDashboardDetail() {
  587. const {organization, dashboard, dashboards, params, router, location} = this.props;
  588. const {modifiedDashboard, dashboardState, widgetLimitReached} = this.state;
  589. const {dashboardId} = params;
  590. return (
  591. <PageFiltersContainer
  592. defaultSelection={{
  593. datetime: {
  594. start: null,
  595. end: null,
  596. utc: false,
  597. period: DEFAULT_STATS_PERIOD,
  598. },
  599. }}
  600. skipLoadLastUsed={
  601. organization.features.includes('dashboards-top-level-filter') &&
  602. hasSavedPageFilters(dashboard)
  603. }
  604. >
  605. <PageContent>
  606. <NoProjectMessage organization={organization}>
  607. <StyledPageHeader>
  608. <StyledTitle>
  609. <DashboardTitle
  610. dashboard={modifiedDashboard ?? dashboard}
  611. onUpdate={this.setModifiedDashboard}
  612. isEditing={this.isEditing}
  613. />
  614. </StyledTitle>
  615. <Controls
  616. organization={organization}
  617. dashboards={dashboards}
  618. onEdit={this.onEdit}
  619. onCancel={this.onCancel}
  620. onCommit={this.onCommit}
  621. onAddWidget={this.onAddWidget}
  622. onDelete={this.onDelete(dashboard)}
  623. dashboardState={dashboardState}
  624. widgetLimitReached={widgetLimitReached}
  625. />
  626. </StyledPageHeader>
  627. <PageFilterBar condensed>
  628. <ProjectPageFilter />
  629. <EnvironmentPageFilter />
  630. <DatePageFilter alignDropdown="left" />
  631. </PageFilterBar>
  632. <HookHeader organization={organization} />
  633. <Dashboard
  634. paramDashboardId={dashboardId}
  635. dashboard={modifiedDashboard ?? dashboard}
  636. organization={organization}
  637. isEditing={this.isEditing}
  638. widgetLimitReached={widgetLimitReached}
  639. onUpdate={this.onUpdateWidget}
  640. handleUpdateWidgetList={this.handleUpdateWidgetList}
  641. handleAddCustomWidget={this.handleAddCustomWidget}
  642. isPreview={this.isPreview}
  643. router={router}
  644. location={location}
  645. />
  646. </NoProjectMessage>
  647. </PageContent>
  648. </PageFiltersContainer>
  649. );
  650. }
  651. getBreadcrumbLabel() {
  652. const {dashboardState} = this.state;
  653. let label = this.dashboardTitle;
  654. if (dashboardState === DashboardState.CREATE) {
  655. label = t('Create Dashboard');
  656. } else if (this.isPreview) {
  657. label = t('Preview Dashboard');
  658. }
  659. return label;
  660. }
  661. renderDashboardDetail() {
  662. const {
  663. organization,
  664. dashboard,
  665. dashboards,
  666. params,
  667. router,
  668. location,
  669. newWidget,
  670. selection,
  671. onSetNewWidget,
  672. } = this.props;
  673. const {modifiedDashboard, dashboardState, widgetLimitReached, seriesData, setData} =
  674. this.state;
  675. const {dashboardId} = params;
  676. const {filters} = modifiedDashboard || dashboard;
  677. return (
  678. <SentryDocumentTitle title={dashboard.title} orgSlug={organization.slug}>
  679. <PageFiltersContainer
  680. defaultSelection={{
  681. datetime: {
  682. start: null,
  683. end: null,
  684. utc: false,
  685. period: DEFAULT_STATS_PERIOD,
  686. },
  687. }}
  688. skipLoadLastUsed={
  689. organization.features.includes('dashboards-top-level-filter') &&
  690. hasSavedPageFilters(dashboard)
  691. }
  692. >
  693. <StyledPageContent>
  694. <NoProjectMessage organization={organization}>
  695. <Layout.Header>
  696. <Layout.HeaderContent>
  697. <Breadcrumbs
  698. crumbs={[
  699. {
  700. label: t('Dashboards'),
  701. to: `/organizations/${organization.slug}/dashboards/`,
  702. },
  703. {
  704. label: this.getBreadcrumbLabel(),
  705. },
  706. ]}
  707. />
  708. <Layout.Title>
  709. <DashboardTitle
  710. dashboard={modifiedDashboard ?? dashboard}
  711. onUpdate={this.setModifiedDashboard}
  712. isEditing={this.isEditing}
  713. />
  714. </Layout.Title>
  715. </Layout.HeaderContent>
  716. <Layout.HeaderActions>
  717. <Controls
  718. organization={organization}
  719. dashboards={dashboards}
  720. onEdit={this.onEdit}
  721. onCancel={this.onCancel}
  722. onCommit={this.onCommit}
  723. onAddWidget={this.onAddWidget}
  724. onDelete={this.onDelete(dashboard)}
  725. dashboardState={dashboardState}
  726. widgetLimitReached={widgetLimitReached}
  727. />
  728. </Layout.HeaderActions>
  729. </Layout.Header>
  730. <Layout.Body>
  731. <Layout.Main fullWidth>
  732. <Wrapper>
  733. <PageFilterBar condensed>
  734. <ProjectPageFilter />
  735. <EnvironmentPageFilter />
  736. <DatePageFilter alignDropdown="left" />
  737. </PageFilterBar>
  738. <Feature features={['dashboards-top-level-filter']}>
  739. <FilterButtons>
  740. <ReleasesProvider
  741. organization={organization}
  742. selection={selection}
  743. >
  744. <ReleasesSelectControl
  745. handleChangeFilter={this.handleChangeFilter}
  746. selectedReleases={filters?.release || []}
  747. />
  748. </ReleasesProvider>
  749. </FilterButtons>
  750. </Feature>
  751. </Wrapper>
  752. <WidgetViewerContext.Provider value={{seriesData, setData}}>
  753. <Dashboard
  754. paramDashboardId={dashboardId}
  755. dashboard={modifiedDashboard ?? dashboard}
  756. organization={organization}
  757. isEditing={this.isEditing}
  758. widgetLimitReached={widgetLimitReached}
  759. onUpdate={this.onUpdateWidget}
  760. handleUpdateWidgetList={this.handleUpdateWidgetList}
  761. handleAddCustomWidget={this.handleAddCustomWidget}
  762. router={router}
  763. location={location}
  764. newWidget={newWidget}
  765. onSetNewWidget={onSetNewWidget}
  766. isPreview={this.isPreview}
  767. />
  768. </WidgetViewerContext.Provider>
  769. </Layout.Main>
  770. </Layout.Body>
  771. </NoProjectMessage>
  772. </StyledPageContent>
  773. </PageFiltersContainer>
  774. </SentryDocumentTitle>
  775. );
  776. }
  777. render() {
  778. const {organization} = this.props;
  779. if (this.isWidgetBuilderRouter) {
  780. return this.renderWidgetBuilder();
  781. }
  782. if (organization.features.includes('dashboards-edit')) {
  783. return this.renderDashboardDetail();
  784. }
  785. return this.renderDefaultDashboardDetail();
  786. }
  787. }
  788. const StyledPageHeader = styled('div')`
  789. display: grid;
  790. grid-template-columns: minmax(0, 1fr);
  791. grid-row-gap: ${space(2)};
  792. align-items: center;
  793. margin-bottom: ${space(2)};
  794. @media (min-width: ${p => p.theme.breakpoints.medium}) {
  795. grid-template-columns: minmax(0, 1fr) max-content;
  796. grid-column-gap: ${space(2)};
  797. height: 40px;
  798. }
  799. `;
  800. const StyledTitle = styled(Layout.Title)`
  801. margin-top: 0;
  802. `;
  803. const StyledPageContent = styled(PageContent)`
  804. padding: 0;
  805. `;
  806. const Wrapper = styled('div')`
  807. display: grid;
  808. gap: ${space(1.5)};
  809. margin-bottom: ${space(2)};
  810. @media (min-width: ${p => p.theme.breakpoints.small}) {
  811. grid-template-columns: min-content 1fr;
  812. }
  813. `;
  814. const FilterButtons = styled(ButtonBar)`
  815. @media (max-width: ${p => p.theme.breakpoints.small}) {
  816. display: flex;
  817. align-items: flex-start;
  818. gap: ${space(1.5)};
  819. }
  820. @media (min-width: ${p => p.theme.breakpoints.small}) {
  821. display: grid;
  822. grid-auto-columns: minmax(auto, 300px);
  823. }
  824. `;
  825. export default withApi(withOrganization(withPageFilters(DashboardDetail)));