DashboardPage.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. }
  87. }
  88. componentDidUpdate(prevProps: Props) {
  89. const { dashboard, editview, urlEdit, urlFullscreen, urlPanelId } = this.props;
  90. if (!dashboard) {
  91. return;
  92. }
  93. // if we just got dashboard update title
  94. if (!prevProps.dashboard) {
  95. document.title = dashboard.title + ' - Grafana';
  96. }
  97. // handle animation states when opening dashboard settings
  98. if (!prevProps.editview && editview) {
  99. this.setState({ isSettingsOpening: true });
  100. setTimeout(() => {
  101. this.setState({ isSettingsOpening: false });
  102. }, 10);
  103. }
  104. // Sync url state with model
  105. if (urlFullscreen !== dashboard.meta.fullscreen || urlEdit !== dashboard.meta.isEditing) {
  106. if (!isNaN(parseInt(urlPanelId, 10))) {
  107. this.onEnterFullscreen();
  108. } else {
  109. this.onLeaveFullscreen();
  110. }
  111. }
  112. }
  113. onEnterFullscreen() {
  114. const { dashboard, urlEdit, urlFullscreen, urlPanelId } = this.props;
  115. const panelId = parseInt(urlPanelId, 10);
  116. // need to expand parent row if this panel is inside a row
  117. dashboard.expandParentRowFor(panelId);
  118. const panel = dashboard.getPanelById(panelId);
  119. if (panel) {
  120. dashboard.setViewMode(panel, urlFullscreen, urlEdit);
  121. this.setState({
  122. isEditing: urlEdit && dashboard.meta.canEdit,
  123. isFullscreen: urlFullscreen,
  124. fullscreenPanel: panel,
  125. rememberScrollTop: this.state.scrollTop,
  126. });
  127. this.setPanelFullscreenClass(urlFullscreen);
  128. } else {
  129. this.handleFullscreenPanelNotFound(urlPanelId);
  130. }
  131. }
  132. onLeaveFullscreen() {
  133. const { dashboard } = this.props;
  134. if (this.state.fullscreenPanel) {
  135. dashboard.setViewMode(this.state.fullscreenPanel, false, false);
  136. }
  137. this.setState(
  138. {
  139. isEditing: false,
  140. isFullscreen: false,
  141. fullscreenPanel: null,
  142. scrollTop: this.state.rememberScrollTop,
  143. },
  144. () => {
  145. dashboard.render();
  146. }
  147. );
  148. this.setPanelFullscreenClass(false);
  149. }
  150. handleFullscreenPanelNotFound(urlPanelId: string) {
  151. // Panel not found
  152. this.props.notifyApp(createErrorNotification(`Panel with id ${urlPanelId} not found`));
  153. // Clear url state
  154. this.props.updateLocation({
  155. query: {
  156. edit: null,
  157. fullscreen: null,
  158. panelId: null,
  159. },
  160. partial: true,
  161. });
  162. }
  163. setPanelFullscreenClass(isFullscreen: boolean) {
  164. $('body').toggleClass('panel-in-fullscreen', isFullscreen);
  165. }
  166. setScrollTop = (e: MouseEvent<HTMLElement>): void => {
  167. const target = e.target as HTMLElement;
  168. this.setState({ scrollTop: target.scrollTop });
  169. };
  170. onAddPanel = () => {
  171. const { dashboard } = this.props;
  172. // Return if the "Add panel" exists already
  173. if (dashboard.panels.length > 0 && dashboard.panels[0].type === 'add-panel') {
  174. return;
  175. }
  176. dashboard.addPanel({
  177. type: 'add-panel',
  178. gridPos: { x: 0, y: 0, w: 12, h: 8 },
  179. title: 'Panel Title',
  180. });
  181. // scroll to top after adding panel
  182. this.setState({ scrollTop: 0 });
  183. };
  184. renderSlowInitState() {
  185. return (
  186. <div className="dashboard-loading">
  187. <div className="dashboard-loading__text">
  188. <i className="fa fa-spinner fa-spin" /> {this.props.initPhase}
  189. </div>
  190. </div>
  191. );
  192. }
  193. renderInitFailedState() {
  194. const { initError } = this.props;
  195. return (
  196. <div className="dashboard-loading">
  197. <AlertBox
  198. severity={AppNotificationSeverity.Error}
  199. title={initError.message}
  200. text={getMessageFromError(initError.error)}
  201. />
  202. </div>
  203. );
  204. }
  205. render() {
  206. const { dashboard, editview, $injector, isInitSlow, initError } = this.props;
  207. const { isSettingsOpening, isEditing, isFullscreen, scrollTop } = this.state;
  208. if (!dashboard) {
  209. if (isInitSlow) {
  210. return this.renderSlowInitState();
  211. }
  212. return null;
  213. }
  214. const classes = classNames({
  215. 'dashboard-page--settings-opening': isSettingsOpening,
  216. 'dashboard-page--settings-open': !isSettingsOpening && editview,
  217. });
  218. const gridWrapperClasses = classNames({
  219. 'dashboard-container': true,
  220. 'dashboard-container--has-submenu': dashboard.meta.submenuEnabled,
  221. });
  222. return (
  223. <div className={classes}>
  224. <DashNav
  225. dashboard={dashboard}
  226. isEditing={isEditing}
  227. isFullscreen={isFullscreen}
  228. editview={editview}
  229. $injector={$injector}
  230. onAddPanel={this.onAddPanel}
  231. />
  232. <div className="scroll-canvas scroll-canvas--dashboard">
  233. <CustomScrollbar autoHeightMin={'100%'} setScrollTop={this.setScrollTop} scrollTop={scrollTop}>
  234. {editview && <DashboardSettings dashboard={dashboard} />}
  235. {initError && this.renderInitFailedState()}
  236. <div className={gridWrapperClasses}>
  237. {dashboard.meta.submenuEnabled && <SubMenu dashboard={dashboard} />}
  238. <DashboardGrid dashboard={dashboard} isEditing={isEditing} isFullscreen={isFullscreen} />
  239. </div>
  240. </CustomScrollbar>
  241. </div>
  242. </div>
  243. );
  244. }
  245. }
  246. export const mapStateToProps = (state: StoreState) => ({
  247. urlUid: state.location.routeParams.uid,
  248. urlSlug: state.location.routeParams.slug,
  249. urlType: state.location.routeParams.type,
  250. editview: state.location.query.editview,
  251. urlPanelId: state.location.query.panelId,
  252. urlFolderId: state.location.query.folderId,
  253. urlFullscreen: !!state.location.query.fullscreen,
  254. urlEdit: !!state.location.query.edit,
  255. initPhase: state.dashboard.initPhase,
  256. isInitSlow: state.dashboard.isInitSlow,
  257. initError: state.dashboard.initError,
  258. dashboard: state.dashboard.model as DashboardModel,
  259. });
  260. const mapDispatchToProps = {
  261. initDashboard,
  262. cleanUpDashboard,
  263. notifyApp,
  264. updateLocation,
  265. };
  266. export default hot(module)(
  267. connect(
  268. mapStateToProps,
  269. mapDispatchToProps
  270. )(DashboardPage)
  271. );