SoloPanelPage.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Libraries
  2. import React, { Component } from 'react';
  3. import { hot } from 'react-hot-loader';
  4. import { connect } from 'react-redux';
  5. // Components
  6. import { DashboardPanel } from '../dashgrid/DashboardPanel';
  7. // Redux
  8. import { initDashboard } from '../state/initDashboard';
  9. // Types
  10. import { StoreState, DashboardRouteInfo } from 'app/types';
  11. import { PanelModel, DashboardModel } from 'app/features/dashboard/state';
  12. interface Props {
  13. urlPanelId: string;
  14. urlUid?: string;
  15. urlSlug?: string;
  16. urlType?: string;
  17. $scope: any;
  18. $injector: any;
  19. routeInfo: DashboardRouteInfo;
  20. initDashboard: typeof initDashboard;
  21. dashboard: DashboardModel | null;
  22. }
  23. interface State {
  24. panel: PanelModel | null;
  25. notFound: boolean;
  26. }
  27. export class SoloPanelPage extends Component<Props, State> {
  28. state: State = {
  29. panel: null,
  30. notFound: false,
  31. };
  32. componentDidMount() {
  33. const { $injector, $scope, urlUid, urlType, urlSlug, routeInfo } = this.props;
  34. this.props.initDashboard({
  35. $injector: $injector,
  36. $scope: $scope,
  37. urlSlug: urlSlug,
  38. urlUid: urlUid,
  39. urlType: urlType,
  40. routeInfo: routeInfo,
  41. fixUrl: false,
  42. });
  43. }
  44. componentDidUpdate(prevProps: Props) {
  45. const { urlPanelId, dashboard } = this.props;
  46. if (!dashboard) {
  47. return;
  48. }
  49. // we just got the dashboard!
  50. if (!prevProps.dashboard) {
  51. const panelId = parseInt(urlPanelId, 10);
  52. // need to expand parent row if this panel is inside a row
  53. dashboard.expandParentRowFor(panelId);
  54. const panel = dashboard.getPanelById(panelId);
  55. if (!panel) {
  56. this.setState({ notFound: true });
  57. return;
  58. }
  59. this.setState({ panel });
  60. }
  61. }
  62. render() {
  63. const { urlPanelId, dashboard } = this.props;
  64. const { notFound, panel } = this.state;
  65. if (notFound) {
  66. return <div className="alert alert-error">Panel with id {urlPanelId} not found</div>;
  67. }
  68. if (!panel) {
  69. return <div>Loading & initializing dashboard</div>;
  70. }
  71. return (
  72. <div className="panel-solo">
  73. <DashboardPanel dashboard={dashboard} panel={panel} isEditing={false} isFullscreen={false} isInView={true} />
  74. </div>
  75. );
  76. }
  77. }
  78. const mapStateToProps = (state: StoreState) => ({
  79. urlUid: state.location.routeParams.uid,
  80. urlSlug: state.location.routeParams.slug,
  81. urlType: state.location.routeParams.type,
  82. urlPanelId: state.location.query.panelId,
  83. dashboard: state.dashboard.model as DashboardModel,
  84. });
  85. const mapDispatchToProps = {
  86. initDashboard,
  87. };
  88. export default hot(module)(
  89. connect(
  90. mapStateToProps,
  91. mapDispatchToProps
  92. )(SoloPanelPage)
  93. );