SoloPanelPage.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Libraries
  2. import React, { Component } from 'react';
  3. import { hot } from 'react-hot-loader';
  4. import { connect } from 'react-redux';
  5. // Utils & Services
  6. import appEvents from 'app/core/app_events';
  7. import locationUtil from 'app/core/utils/location_util';
  8. import { getBackendSrv } from 'app/core/services/backend_srv';
  9. // Components
  10. import { DashboardPanel } from '../dashgrid/DashboardPanel';
  11. // Redux
  12. import { updateLocation } from 'app/core/actions';
  13. // Types
  14. import { StoreState } from 'app/types';
  15. import { PanelModel, DashboardModel } from 'app/features/dashboard/state';
  16. interface Props {
  17. panelId: string;
  18. urlUid?: string;
  19. urlSlug?: string;
  20. urlType?: string;
  21. $scope: any;
  22. $injector: any;
  23. updateLocation: typeof updateLocation;
  24. }
  25. interface State {
  26. panel: PanelModel | null;
  27. dashboard: DashboardModel | null;
  28. notFound: boolean;
  29. }
  30. export class SoloPanelPage extends Component<Props, State> {
  31. state: State = {
  32. panel: null,
  33. dashboard: null,
  34. notFound: false,
  35. };
  36. componentDidMount() {
  37. const { $injector, $scope, urlUid, urlType, urlSlug } = this.props;
  38. // handle old urls with no uid
  39. if (!urlUid && !(urlType === 'script' || urlType === 'snapshot')) {
  40. this.redirectToNewUrl();
  41. return;
  42. }
  43. const dashboardLoaderSrv = $injector.get('dashboardLoaderSrv');
  44. // subscribe to event to know when dashboard controller is done with inititalization
  45. appEvents.on('dashboard-initialized', this.onDashoardInitialized);
  46. dashboardLoaderSrv.loadDashboard(urlType, urlSlug, urlUid).then(result => {
  47. result.meta.soloMode = true;
  48. $scope.initDashboard(result, $scope);
  49. });
  50. }
  51. redirectToNewUrl() {
  52. getBackendSrv().getDashboardBySlug(this.props.urlSlug).then(res => {
  53. if (res) {
  54. const url = locationUtil.stripBaseFromUrl(res.meta.url.replace('/d/', '/d-solo/'));
  55. this.props.updateLocation(url);
  56. }
  57. });
  58. }
  59. onDashoardInitialized = () => {
  60. const { $scope, panelId } = this.props;
  61. const dashboard: DashboardModel = $scope.dashboard;
  62. const panel = dashboard.getPanelById(parseInt(panelId, 10));
  63. if (!panel) {
  64. this.setState({ notFound: true });
  65. return;
  66. }
  67. this.setState({ dashboard, panel });
  68. };
  69. render() {
  70. const { panelId } = this.props;
  71. const { notFound, panel, dashboard } = this.state;
  72. if (notFound) {
  73. return (
  74. <div className="alert alert-error">
  75. Panel with id { panelId } not found
  76. </div>
  77. );
  78. }
  79. if (!panel) {
  80. return <div>Loading & initializing dashboard</div>;
  81. }
  82. return (
  83. <div className="panel-solo">
  84. <DashboardPanel dashboard={dashboard} panel={panel} isEditing={false} isFullscreen={false} />
  85. </div>
  86. );
  87. }
  88. }
  89. const mapStateToProps = (state: StoreState) => ({
  90. urlUid: state.location.routeParams.uid,
  91. urlSlug: state.location.routeParams.slug,
  92. urlType: state.location.routeParams.type,
  93. panelId: state.location.query.panelId
  94. });
  95. const mapDispatchToProps = {
  96. updateLocation
  97. };
  98. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(SoloPanelPage));