detail.tsx 30 KB

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