DashboardPage.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // Libraries
  2. import $ from 'jquery';
  3. import React, { PureComponent, MouseEvent } from 'react';
  4. import { hot } from 'react-hot-loader';
  5. import { connect } from 'react-redux';
  6. import classNames from 'classnames';
  7. // Services & Utils
  8. import { createErrorNotification } from 'app/core/copy/appNotification';
  9. import { getMessageFromError } from 'app/core/utils/errors';
  10. // Components
  11. import { DashboardGrid } from '../dashgrid/DashboardGrid';
  12. import { DashNav } from '../components/DashNav';
  13. import { SubMenu } from '../components/SubMenu';
  14. import { DashboardSettings } from '../components/DashboardSettings';
  15. import { CustomScrollbar } from '@grafana/ui';
  16. import { AlertBox } from 'app/core/components/AlertBox/AlertBox';
  17. // Redux
  18. import { initDashboard } from '../state/initDashboard';
  19. import { cleanUpDashboard } from '../state/actions';
  20. import { updateLocation } from 'app/core/actions';
  21. import { notifyApp } from 'app/core/actions';
  22. // Types
  23. import {
  24. StoreState,
  25. DashboardInitPhase,
  26. DashboardRouteInfo,
  27. DashboardInitError,
  28. AppNotificationSeverity,
  29. } from 'app/types';
  30. import { DashboardModel, PanelModel } from 'app/features/dashboard/state';
  31. export interface Props {
  32. urlUid?: string;
  33. urlSlug?: string;
  34. urlType?: string;
  35. editview?: string;
  36. urlPanelId?: string;
  37. urlFolderId?: string;
  38. $scope: any;
  39. $injector: any;
  40. routeInfo: DashboardRouteInfo;
  41. urlEdit: boolean;
  42. urlFullscreen: boolean;
  43. initPhase: DashboardInitPhase;
  44. isInitSlow: boolean;
  45. dashboard: DashboardModel | null;
  46. initError?: DashboardInitError;
  47. initDashboard: typeof initDashboard;
  48. cleanUpDashboard: typeof cleanUpDashboard;
  49. notifyApp: typeof notifyApp;
  50. updateLocation: typeof updateLocation;
  51. }
  52. export interface State {
  53. isSettingsOpening: boolean;
  54. isEditing: boolean;
  55. isFullscreen: boolean;
  56. fullscreenPanel: PanelModel | null;
  57. scrollTop: number;
  58. rememberScrollTop: number;
  59. showLoadingState: boolean;
  60. }
  61. export class DashboardPage extends PureComponent<Props, State> {
  62. state: State = {
  63. isSettingsOpening: false,
  64. isEditing: false,
  65. isFullscreen: false,
  66. showLoadingState: false,
  67. fullscreenPanel: null,
  68. scrollTop: 0,
  69. rememberScrollTop: 0,
  70. };
  71. async componentDidMount() {
  72. this.props.initDashboard({
  73. $injector: this.props.$injector,
  74. $scope: this.props.$scope,
  75. urlSlug: this.props.urlSlug,
  76. urlUid: this.props.urlUid,
  77. urlType: this.props.urlType,
  78. urlFolderId: this.props.urlFolderId,
  79. routeInfo: this.props.routeInfo,
  80. fixUrl: true,
  81. });
  82. }
  83. componentWillUnmount() {
  84. if (this.props.dashboard) {
  85. this.props.cleanUpDashboard();
  86. this.setPanelFullscreenClass(false);
  87. }
  88. }
  89. componentDidUpdate(prevProps: Props) {
  90. const { dashboard, editview, urlEdit, urlFullscreen, urlPanelId, urlUid } = this.props;
  91. if (!dashboard) {
  92. return;
  93. }
  94. // if we just got dashboard update title
  95. if (!prevProps.dashboard) {
  96. document.title = dashboard.title + ' - Grafana';
  97. }
  98. // Due to the angular -> react url bridge we can ge an update here with new uid before the container unmounts
  99. // Can remove this condition after we switch to react router
  100. if (prevProps.urlUid !== urlUid) {
  101. return;
  102. }
  103. // handle animation states when opening dashboard settings
  104. if (!prevProps.editview && editview) {
  105. this.setState({ isSettingsOpening: true });
  106. setTimeout(() => {
  107. this.setState({ isSettingsOpening: false });
  108. }, 10);
  109. }
  110. // Sync url state with model
  111. if (urlFullscreen !== dashboard.meta.fullscreen || urlEdit !== dashboard.meta.isEditing) {
  112. if (!isNaN(parseInt(urlPanelId, 10))) {
  113. this.onEnterFullscreen();
  114. } else {
  115. this.onLeaveFullscreen();
  116. }
  117. }
  118. }
  119. onEnterFullscreen() {
  120. const { dashboard, urlEdit, urlFullscreen, urlPanelId } = this.props;
  121. const panelId = parseInt(urlPanelId, 10);
  122. // need to expand parent row if this panel is inside a row
  123. dashboard.expandParentRowFor(panelId);
  124. const panel = dashboard.getPanelById(panelId);
  125. if (panel) {
  126. dashboard.setViewMode(panel, urlFullscreen, urlEdit);
  127. this.setState({
  128. isEditing: urlEdit && dashboard.meta.canEdit,
  129. isFullscreen: urlFullscreen,
  130. fullscreenPanel: panel,
  131. rememberScrollTop: this.state.scrollTop,
  132. });
  133. this.setPanelFullscreenClass(urlFullscreen);
  134. } else {
  135. this.handleFullscreenPanelNotFound(urlPanelId);
  136. }
  137. }
  138. onLeaveFullscreen() {
  139. const { dashboard } = this.props;
  140. if (this.state.fullscreenPanel) {
  141. dashboard.setViewMode(this.state.fullscreenPanel, false, false);
  142. }
  143. this.setState(
  144. {
  145. isEditing: false,
  146. isFullscreen: false,
  147. fullscreenPanel: null,
  148. scrollTop: this.state.rememberScrollTop,
  149. },
  150. this.triggerPanelsRendering.bind(this)
  151. );
  152. this.setPanelFullscreenClass(false);
  153. }
  154. triggerPanelsRendering() {
  155. try {
  156. this.props.dashboard.render();
  157. } catch (err) {
  158. this.props.notifyApp(createErrorNotification(`Panel rendering error`, err));
  159. }
  160. }
  161. handleFullscreenPanelNotFound(urlPanelId: string) {
  162. // Panel not found
  163. this.props.notifyApp(createErrorNotification(`Panel with id ${urlPanelId} not found`));
  164. // Clear url state
  165. this.props.updateLocation({
  166. query: {
  167. edit: null,
  168. fullscreen: null,
  169. panelId: null,
  170. },
  171. partial: true,
  172. });
  173. }
  174. setPanelFullscreenClass(isFullscreen: boolean) {
  175. $('body').toggleClass('panel-in-fullscreen', isFullscreen);
  176. }
  177. setScrollTop = (e: MouseEvent<HTMLElement>): void => {
  178. const target = e.target as HTMLElement;
  179. this.setState({ scrollTop: target.scrollTop });
  180. };
  181. onAddPanel = () => {
  182. const { dashboard } = this.props;
  183. // Return if the "Add panel" exists already
  184. if (dashboard.panels.length > 0 && dashboard.panels[0].type === 'add-panel') {
  185. return;
  186. }
  187. dashboard.addPanel({
  188. type: 'add-panel',
  189. gridPos: { x: 0, y: 0, w: 12, h: 8 },
  190. title: 'Panel Title',
  191. });
  192. // scroll to top after adding panel
  193. this.setState({ scrollTop: 0 });
  194. };
  195. renderSlowInitState() {
  196. return (
  197. <div className="dashboard-loading">
  198. <div className="dashboard-loading__text">
  199. <i className="fa fa-spinner fa-spin" /> {this.props.initPhase}
  200. </div>
  201. </div>
  202. );
  203. }
  204. renderInitFailedState() {
  205. const { initError } = this.props;
  206. return (
  207. <div className="dashboard-loading">
  208. <AlertBox
  209. severity={AppNotificationSeverity.Error}
  210. title={initError.message}
  211. text={getMessageFromError(initError.error)}
  212. />
  213. </div>
  214. );
  215. }
  216. render() {
  217. const { dashboard, editview, $injector, isInitSlow, initError } = this.props;
  218. const { isSettingsOpening, isEditing, isFullscreen, scrollTop } = this.state;
  219. if (!dashboard) {
  220. if (isInitSlow) {
  221. return this.renderSlowInitState();
  222. }
  223. return null;
  224. }
  225. const classes = classNames({
  226. 'dashboard-page--settings-opening': isSettingsOpening,
  227. 'dashboard-page--settings-open': !isSettingsOpening && editview,
  228. });
  229. const gridWrapperClasses = classNames({
  230. 'dashboard-container': true,
  231. 'dashboard-container--has-submenu': dashboard.meta.submenuEnabled,
  232. });
  233. // Only trigger render when the scroll has moved by 25
  234. const approximateScrollTop = Math.round(scrollTop / 25) * 25;
  235. return (
  236. <div className={classes}>
  237. <DashNav
  238. dashboard={dashboard}
  239. isEditing={isEditing}
  240. isFullscreen={isFullscreen}
  241. editview={editview}
  242. $injector={$injector}
  243. onAddPanel={this.onAddPanel}
  244. />
  245. <div className="scroll-canvas scroll-canvas--dashboard">
  246. <CustomScrollbar
  247. autoHeightMin={'100%'}
  248. setScrollTop={this.setScrollTop}
  249. scrollTop={scrollTop}
  250. updateAfterMountMs={500}
  251. className="custom-scrollbar--page"
  252. >
  253. {editview && <DashboardSettings dashboard={dashboard} />}
  254. {initError && this.renderInitFailedState()}
  255. <div className={gridWrapperClasses}>
  256. {dashboard.meta.submenuEnabled && <SubMenu dashboard={dashboard} />}
  257. <DashboardGrid
  258. dashboard={dashboard}
  259. isEditing={isEditing}
  260. isFullscreen={isFullscreen}
  261. scrollTop={approximateScrollTop}
  262. />
  263. </div>
  264. </CustomScrollbar>
  265. </div>
  266. </div>
  267. );
  268. }
  269. }
  270. export const mapStateToProps = (state: StoreState) => ({
  271. urlUid: state.location.routeParams.uid,
  272. urlSlug: state.location.routeParams.slug,
  273. urlType: state.location.routeParams.type,
  274. editview: state.location.query.editview,
  275. urlPanelId: state.location.query.panelId,
  276. urlFolderId: state.location.query.folderId,
  277. urlFullscreen: !!state.location.query.fullscreen,
  278. urlEdit: !!state.location.query.edit,
  279. initPhase: state.dashboard.initPhase,
  280. isInitSlow: state.dashboard.isInitSlow,
  281. initError: state.dashboard.initError,
  282. dashboard: state.dashboard.model as DashboardModel,
  283. });
  284. const mapDispatchToProps = {
  285. initDashboard,
  286. cleanUpDashboard,
  287. notifyApp,
  288. updateLocation,
  289. };
  290. export default hot(module)(
  291. connect(
  292. mapStateToProps,
  293. mapDispatchToProps
  294. )(DashboardPage)
  295. );