SoloPanelPage.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import React, { Component } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import AutoSizer from 'react-virtualized-auto-sizer';
  4. import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
  5. import { DashboardModel, PanelModel } from 'app/features/dashboard/state';
  6. import { StoreState } from 'app/types';
  7. import { DashboardPanel } from '../dashgrid/DashboardPanel';
  8. import { initDashboard } from '../state/initDashboard';
  9. export interface DashboardPageRouteParams {
  10. uid?: string;
  11. type?: string;
  12. slug?: string;
  13. }
  14. const mapStateToProps = (state: StoreState) => ({
  15. dashboard: state.dashboard.getModel(),
  16. });
  17. const mapDispatchToProps = {
  18. initDashboard,
  19. };
  20. const connector = connect(mapStateToProps, mapDispatchToProps);
  21. export type Props = GrafanaRouteComponentProps<DashboardPageRouteParams, { panelId: string }> &
  22. ConnectedProps<typeof connector>;
  23. export 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 { match, route } = this.props;
  34. this.props.initDashboard({
  35. urlSlug: match.params.slug,
  36. urlUid: match.params.uid,
  37. urlType: match.params.type,
  38. routeName: route.routeName,
  39. fixUrl: false,
  40. });
  41. }
  42. getPanelId(): number {
  43. return parseInt(this.props.queryParams.panelId ?? '0', 10);
  44. }
  45. componentDidUpdate(prevProps: Props) {
  46. const { dashboard } = this.props;
  47. if (!dashboard) {
  48. return;
  49. }
  50. // we just got a new dashboard
  51. if (!prevProps.dashboard || prevProps.dashboard.uid !== dashboard.uid) {
  52. const panel = dashboard.getPanelByUrlId(this.props.queryParams.panelId);
  53. if (!panel) {
  54. this.setState({ notFound: true });
  55. return;
  56. }
  57. this.setState({ panel });
  58. }
  59. }
  60. render() {
  61. return (
  62. <SoloPanel
  63. dashboard={this.props.dashboard}
  64. notFound={this.state.notFound}
  65. panel={this.state.panel}
  66. panelId={this.getPanelId()}
  67. />
  68. );
  69. }
  70. }
  71. export interface SoloPanelProps extends State {
  72. dashboard: DashboardModel | null;
  73. panelId: number;
  74. }
  75. export const SoloPanel = ({ dashboard, notFound, panel, panelId }: SoloPanelProps) => {
  76. if (notFound) {
  77. return <div className="alert alert-error">Panel with id {panelId} not found</div>;
  78. }
  79. if (!panel || !dashboard) {
  80. return <div>Loading & initializing dashboard</div>;
  81. }
  82. return (
  83. <div className="panel-solo">
  84. <AutoSizer>
  85. {({ width, height }) => {
  86. if (width === 0) {
  87. return null;
  88. }
  89. return (
  90. <DashboardPanel
  91. stateKey={panel.key}
  92. width={width}
  93. height={height}
  94. dashboard={dashboard}
  95. panel={panel}
  96. isEditing={false}
  97. isViewing={false}
  98. lazy={false}
  99. />
  100. );
  101. }}
  102. </AutoSizer>
  103. </div>
  104. );
  105. };
  106. export default connector(SoloPanelPage);