forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSoloPanelPage.tsx
138 lines (114 loc) · 3.39 KB
/
SoloPanelPage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import React, { Component } from 'react';
import { connect, ConnectedProps } from 'react-redux';
import AutoSizer from 'react-virtualized-auto-sizer';
import { GrafanaContext, GrafanaContextType } from 'app/core/context/GrafanaContext';
import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
import { DashboardModel, PanelModel } from 'app/features/dashboard/state';
import { StoreState } from 'app/types';
import { DashboardPanel } from '../dashgrid/DashboardPanel';
import { initDashboard } from '../state/initDashboard';
export interface DashboardPageRouteParams {
uid?: string;
type?: string;
slug?: string;
}
const mapStateToProps = (state: StoreState) => ({
dashboard: state.dashboard.getModel(),
});
const mapDispatchToProps = {
initDashboard,
};
const connector = connect(mapStateToProps, mapDispatchToProps);
export type Props = GrafanaRouteComponentProps<DashboardPageRouteParams, { panelId: string; timezone?: string }> &
ConnectedProps<typeof connector>;
export interface State {
panel: PanelModel | null;
notFound: boolean;
}
export class SoloPanelPage extends Component<Props, State> {
declare context: GrafanaContextType;
static contextType = GrafanaContext;
state: State = {
panel: null,
notFound: false,
};
componentDidMount() {
const { match, route } = this.props;
this.props.initDashboard({
urlSlug: match.params.slug,
urlUid: match.params.uid,
urlType: match.params.type,
routeName: route.routeName,
fixUrl: false,
keybindingSrv: this.context?.keybindings,
});
}
getPanelId(): number {
return parseInt(this.props.queryParams.panelId ?? '0', 10);
}
componentDidUpdate(prevProps: Props) {
const { dashboard } = this.props;
if (!dashboard) {
return;
}
// we just got a new dashboard
if (!prevProps.dashboard || prevProps.dashboard.uid !== dashboard.uid) {
const panel = dashboard.getPanelByUrlId(this.props.queryParams.panelId);
if (!panel) {
this.setState({ notFound: true });
return;
}
this.setState({ panel });
}
}
render() {
return (
<SoloPanel
dashboard={this.props.dashboard}
notFound={this.state.notFound}
panel={this.state.panel}
panelId={this.getPanelId()}
timezone={this.props.queryParams.timezone}
/>
);
}
}
export interface SoloPanelProps extends State {
dashboard: DashboardModel | null;
panelId: number;
timezone?: string;
}
export const SoloPanel = ({ dashboard, notFound, panel, panelId, timezone }: SoloPanelProps) => {
if (notFound) {
return <div className="alert alert-error">Panel with id {panelId} not found</div>;
}
if (!panel || !dashboard) {
return <div>Loading & initializing dashboard</div>;
}
return (
<div className="panel-solo">
<AutoSizer>
{({ width, height }) => {
if (width === 0) {
return null;
}
return (
<DashboardPanel
stateKey={panel.key}
width={width}
height={height}
dashboard={dashboard}
panel={panel}
isEditing={false}
isViewing={true}
lazy={false}
timezone={timezone}
hideMenu={true}
/>
);
}}
</AutoSizer>
</div>
);
};
export default connector(SoloPanelPage);