Files
grafana/public/app/features/dashboard/dashgrid/DashboardPanel.tsx
Alex Khomenko b1a57c6f77 Chore: Automatically infer types for dashgrid connected components (#29818)
* Refactor DashboardPanel

* Refactor PanelEditor

* Fix missing initializer

* Update public/app/features/dashboard/components/PanelEditor/PanelEditor.tsx

Co-authored-by: Torkel Ödegaard <torkel@grafana.org>

* Simplify type

* Remove unused type

* Move prop connectors on top

Co-authored-by: Torkel Ödegaard <torkel@grafana.org>
2020-12-15 13:18:40 +02:00

149 lines
3.5 KiB
TypeScript

// Libraries
import React, { PureComponent } from 'react';
import classNames from 'classnames';
import AutoSizer from 'react-virtualized-auto-sizer';
import { connect, ConnectedProps } from 'react-redux';
// Components
import { PanelChrome } from './PanelChrome';
import { PanelChromeAngular } from './PanelChromeAngular';
// Actions
import { initDashboardPanel } from '../state/actions';
import { updateLocation } from 'app/core/reducers/location';
// Types
import { PanelModel, DashboardModel } from '../state';
import { StoreState } from 'app/types';
import { PanelPlugin } from '@grafana/data';
export interface OwnProps {
panel: PanelModel;
dashboard: DashboardModel;
isEditing: boolean;
isViewing: boolean;
isInView: boolean;
}
export interface State {
isLazy: boolean;
}
const mapStateToProps = (state: StoreState, props: OwnProps) => {
const panelState = state.dashboard.panels[props.panel.id];
if (!panelState) {
return { plugin: null };
}
return {
plugin: panelState.plugin,
};
};
const mapDispatchToProps = { initDashboardPanel, updateLocation };
const connector = connect(mapStateToProps, mapDispatchToProps);
export type Props = OwnProps & ConnectedProps<typeof connector>;
export class DashboardPanelUnconnected extends PureComponent<Props, State> {
specialPanels: { [key: string]: Function } = {};
constructor(props: Props) {
super(props);
this.state = {
isLazy: !props.isInView,
};
}
componentDidMount() {
this.props.initDashboardPanel(this.props.panel);
}
componentDidUpdate() {
if (this.state.isLazy && this.props.isInView) {
this.setState({ isLazy: false });
}
}
onMouseEnter = () => {
this.props.dashboard.setPanelFocus(this.props.panel.id);
};
onMouseLeave = () => {
this.props.dashboard.setPanelFocus(0);
};
renderPanel(plugin: PanelPlugin) {
const { dashboard, panel, isViewing, isInView, isEditing, updateLocation } = this.props;
return (
<AutoSizer>
{({ width, height }) => {
if (width === 0) {
return null;
}
if (plugin.angularPanelCtrl) {
return (
<PanelChromeAngular
plugin={plugin}
panel={panel}
dashboard={dashboard}
isViewing={isViewing}
isEditing={isEditing}
isInView={isInView}
width={width}
height={height}
/>
);
}
return (
<PanelChrome
plugin={plugin}
panel={panel}
dashboard={dashboard}
isViewing={isViewing}
isEditing={isEditing}
isInView={isInView}
width={width}
height={height}
updateLocation={updateLocation}
/>
);
}}
</AutoSizer>
);
}
render() {
const { isViewing, plugin } = this.props;
const { isLazy } = this.state;
// if we have not loaded plugin exports yet, wait
if (!plugin) {
return null;
}
// If we are lazy state don't render anything
if (isLazy) {
return null;
}
const panelWrapperClass = classNames({
'panel-wrapper': true,
'panel-wrapper--view': isViewing,
});
return (
<div className={panelWrapperClass} onMouseEnter={this.onMouseEnter} onMouseLeave={this.onMouseLeave}>
{this.renderPanel(plugin)}
</div>
);
}
}
export const DashboardPanel = connector(DashboardPanelUnconnected);