Commit b1a57c6f by Alex Khomenko Committed by GitHub

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>
parent dbf04709
// Libaries // Libraries
import React, { Component } from 'react'; import React, { Component } from 'react';
import { dateMath, GrafanaTheme, TimeZone } from '@grafana/data'; import { dateMath, GrafanaTheme, TimeZone } from '@grafana/data';
import { css } from 'emotion'; import { css } from 'emotion';
...@@ -8,9 +8,6 @@ import { DashboardModel } from '../../state'; ...@@ -8,9 +8,6 @@ import { DashboardModel } from '../../state';
import { LocationState, CoreEvents } from 'app/types'; import { LocationState, CoreEvents } from 'app/types';
import { TimeRange } from '@grafana/data'; import { TimeRange } from '@grafana/data';
// State
import { updateTimeZoneForSession } from 'app/features/profile/state/reducers';
// Components // Components
import { RefreshPicker, withTheme, stylesFactory, Themeable, defaultIntervals } from '@grafana/ui'; import { RefreshPicker, withTheme, stylesFactory, Themeable, defaultIntervals } from '@grafana/ui';
import { TimePickerWithHistory } from 'app/core/components/TimePicker/TimePickerWithHistory'; import { TimePickerWithHistory } from 'app/core/components/TimePicker/TimePickerWithHistory';
...@@ -31,7 +28,7 @@ const getStyles = stylesFactory((theme: GrafanaTheme) => { ...@@ -31,7 +28,7 @@ const getStyles = stylesFactory((theme: GrafanaTheme) => {
export interface Props extends Themeable { export interface Props extends Themeable {
dashboard: DashboardModel; dashboard: DashboardModel;
location: LocationState; location: LocationState;
onChangeTimeZone: typeof updateTimeZoneForSession; onChangeTimeZone: (timeZone: TimeZone) => void;
} }
class UnthemedDashNavTimeControls extends Component<Props> { class UnthemedDashNavTimeControls extends Component<Props> {
componentDidMount() { componentDidMount() {
......
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import { connect, MapDispatchToProps, MapStateToProps } from 'react-redux'; import { connect, ConnectedProps } from 'react-redux';
import AutoSizer from 'react-virtualized-auto-sizer'; import AutoSizer from 'react-virtualized-auto-sizer';
import { css, cx } from 'emotion'; import { css, cx } from 'emotion';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { FieldConfigSource, GrafanaTheme, PanelPlugin } from '@grafana/data'; import { FieldConfigSource, GrafanaTheme } from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors'; import { selectors } from '@grafana/e2e-selectors';
import { Button, HorizontalGroup, Icon, RadioButtonGroup, stylesFactory } from '@grafana/ui'; import { Button, HorizontalGroup, Icon, RadioButtonGroup, stylesFactory } from '@grafana/ui';
...@@ -27,15 +27,14 @@ import { initPanelEditor, panelEditorCleanUp, updatePanelEditorUIState } from '. ...@@ -27,15 +27,14 @@ import { initPanelEditor, panelEditorCleanUp, updatePanelEditorUIState } from '.
import { updateTimeZoneForSession } from 'app/features/profile/state/reducers'; import { updateTimeZoneForSession } from 'app/features/profile/state/reducers';
import { updateLocation } from 'app/core/reducers/location'; import { updateLocation } from 'app/core/reducers/location';
import { PanelEditorUIState, setDiscardChanges } from './state/reducers'; import { setDiscardChanges } from './state/reducers';
import { getPanelEditorTabs } from './state/selectors'; import { getPanelEditorTabs } from './state/selectors';
import { getPanelStateById } from '../../state/selectors'; import { getPanelStateById } from '../../state/selectors';
import { getVariables } from 'app/features/variables/state/selectors'; import { getVariables } from 'app/features/variables/state/selectors';
import { CoreEvents, LocationState, StoreState } from 'app/types'; import { CoreEvents, StoreState } from 'app/types';
import { DisplayMode, displayModes, PanelEditorTab } from './types'; import { DisplayMode, displayModes, PanelEditorTab } from './types';
import { VariableModel } from 'app/features/variables/types';
import { DashboardModel, PanelModel } from '../../state'; import { DashboardModel, PanelModel } from '../../state';
import { PanelOptionsChangedEvent } from 'app/types/events'; import { PanelOptionsChangedEvent } from 'app/types/events';
...@@ -44,26 +43,33 @@ interface OwnProps { ...@@ -44,26 +43,33 @@ interface OwnProps {
sourcePanel: PanelModel; sourcePanel: PanelModel;
} }
interface ConnectedProps { const mapStateToProps = (state: StoreState) => {
location: LocationState; const panel = state.panelEditor.getPanel();
plugin?: PanelPlugin; const { plugin } = getPanelStateById(state.dashboard, panel.id);
panel: PanelModel;
initDone: boolean;
tabs: PanelEditorTab[];
uiState: PanelEditorUIState;
variables: VariableModel[];
}
interface DispatchProps { return {
updateLocation: typeof updateLocation; location: state.location,
initPanelEditor: typeof initPanelEditor; plugin: plugin,
panelEditorCleanUp: typeof panelEditorCleanUp; panel,
setDiscardChanges: typeof setDiscardChanges; initDone: state.panelEditor.initDone,
updatePanelEditorUIState: typeof updatePanelEditorUIState; tabs: getPanelEditorTabs(state.location, plugin),
updateTimeZoneForSession: typeof updateTimeZoneForSession; uiState: state.panelEditor.ui,
} variables: getVariables(state),
};
};
const mapDispatchToProps = {
updateLocation,
initPanelEditor,
panelEditorCleanUp,
setDiscardChanges,
updatePanelEditorUIState,
updateTimeZoneForSession,
};
const connector = connect(mapStateToProps, mapDispatchToProps);
type Props = OwnProps & ConnectedProps & DispatchProps; type Props = OwnProps & ConnectedProps<typeof connector>;
export class PanelEditorUnconnected extends PureComponent<Props> { export class PanelEditorUnconnected extends PureComponent<Props> {
private eventSubs?: Subscription; private eventSubs?: Subscription;
...@@ -322,31 +328,7 @@ export class PanelEditorUnconnected extends PureComponent<Props> { ...@@ -322,31 +328,7 @@ export class PanelEditorUnconnected extends PureComponent<Props> {
} }
} }
const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, StoreState> = state => { export const PanelEditor = connector(PanelEditorUnconnected);
const panel = state.panelEditor.getPanel();
const { plugin } = getPanelStateById(state.dashboard, panel.id);
return {
location: state.location,
plugin: plugin,
panel,
initDone: state.panelEditor.initDone,
tabs: getPanelEditorTabs(state.location, plugin),
uiState: state.panelEditor.ui,
variables: getVariables(state),
};
};
const mapDispatchToProps: MapDispatchToProps<DispatchProps, OwnProps> = {
updateLocation,
initPanelEditor,
panelEditorCleanUp,
setDiscardChanges,
updatePanelEditorUIState,
updateTimeZoneForSession,
};
export const PanelEditor = connect(mapStateToProps, mapDispatchToProps)(PanelEditorUnconnected);
/* /*
* Styles * Styles
......
// Libaries // Libraries
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import { hot } from 'react-hot-loader'; import { hot } from 'react-hot-loader';
import ReactGridLayout, { ItemCallback } from 'react-grid-layout'; import ReactGridLayout, { ItemCallback } from 'react-grid-layout';
...@@ -102,7 +102,7 @@ export interface Props { ...@@ -102,7 +102,7 @@ export interface Props {
} }
export class DashboardGrid extends PureComponent<Props> { export class DashboardGrid extends PureComponent<Props> {
private panelMap: { [id: string]: PanelModel }; private panelMap: { [id: string]: PanelModel } = {};
private panelRef: { [id: string]: HTMLElement } = {}; private panelRef: { [id: string]: HTMLElement } = {};
private eventSubs = new Subscription(); private eventSubs = new Subscription();
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
import AutoSizer from 'react-virtualized-auto-sizer'; import AutoSizer from 'react-virtualized-auto-sizer';
import { connect, MapStateToProps, MapDispatchToProps } from 'react-redux'; import { connect, ConnectedProps } from 'react-redux';
// Components // Components
import { PanelChrome } from './PanelChrome'; import { PanelChrome } from './PanelChrome';
...@@ -25,23 +25,28 @@ export interface OwnProps { ...@@ -25,23 +25,28 @@ export interface OwnProps {
isInView: boolean; isInView: boolean;
} }
export interface ConnectedProps { export interface State {
plugin?: PanelPlugin | null; isLazy: boolean;
} }
export interface DispatchProps { const mapStateToProps = (state: StoreState, props: OwnProps) => {
initDashboardPanel: typeof initDashboardPanel; const panelState = state.dashboard.panels[props.panel.id];
updateLocation: typeof updateLocation; if (!panelState) {
} return { plugin: null };
}
export type Props = OwnProps & ConnectedProps & DispatchProps; return {
plugin: panelState.plugin,
};
};
export interface State { const mapDispatchToProps = { initDashboardPanel, updateLocation };
isLazy: boolean;
} const connector = connect(mapStateToProps, mapDispatchToProps);
export type Props = OwnProps & ConnectedProps<typeof connector>;
export class DashboardPanelUnconnected extends PureComponent<Props, State> { export class DashboardPanelUnconnected extends PureComponent<Props, State> {
element: HTMLElement;
specialPanels: { [key: string]: Function } = {}; specialPanels: { [key: string]: Function } = {};
constructor(props: Props) { constructor(props: Props) {
...@@ -140,17 +145,4 @@ export class DashboardPanelUnconnected extends PureComponent<Props, State> { ...@@ -140,17 +145,4 @@ export class DashboardPanelUnconnected extends PureComponent<Props, State> {
} }
} }
const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, StoreState> = (state, props) => { export const DashboardPanel = connector(DashboardPanelUnconnected);
const panelState = state.dashboard.panels[props.panel.id];
if (!panelState) {
return { plugin: null };
}
return {
plugin: panelState.plugin,
};
};
const mapDispatchToProps: MapDispatchToProps<DispatchProps, OwnProps> = { initDashboardPanel, updateLocation };
export const DashboardPanel = connect(mapStateToProps, mapDispatchToProps)(DashboardPanelUnconnected);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment