From fa46c877a90bf91b1bddfdbce33a1ca3b10249c4 Mon Sep 17 00:00:00 2001 From: Olof Bourghardt Date: Thu, 24 Jun 2021 08:21:19 +0200 Subject: [PATCH] Explore: refactor redux-connected components (#36027) * Remove some props since they are already inferred from mapStateToProps * Remove empty interface * Refactor TableContainer component * Did some small re-namings --- .../features/explore/NodeGraphContainer.tsx | 13 ++--- .../explore/ReturnToDashboardButton.tsx | 47 +++++++++---------- .../app/features/explore/TableContainer.tsx | 42 ++++++++--------- 3 files changed, 49 insertions(+), 53 deletions(-) diff --git a/public/app/features/explore/NodeGraphContainer.tsx b/public/app/features/explore/NodeGraphContainer.tsx index f54fca048a6..e98a2edc346 100644 --- a/public/app/features/explore/NodeGraphContainer.tsx +++ b/public/app/features/explore/NodeGraphContainer.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { useToggle } from 'react-use'; import { Badge, Collapse, useStyles2, useTheme2 } from '@grafana/ui'; -import { applyFieldOverrides, DataFrame, GrafanaTheme2, TimeRange } from '@grafana/data'; +import { applyFieldOverrides, DataFrame, GrafanaTheme2 } from '@grafana/data'; import { css } from '@emotion/css'; import { ExploreId, StoreState } from '../../types'; import { splitOpen } from './state/main'; @@ -18,16 +18,17 @@ const getStyles = (theme: GrafanaTheme2) => ({ `, }); -interface Props { +interface OwnProps { // Edges and Nodes are separate frames dataFrames: DataFrame[]; exploreId: ExploreId; - range: TimeRange; - splitOpen: typeof splitOpen; // When showing the node graph together with trace view we do some changes so it works better. withTraceView?: boolean; } -export function UnconnectedNodeGraphContainer(props: Props & ConnectedProps) { + +type Props = OwnProps & ConnectedProps; + +export function UnconnectedNodeGraphContainer(props: Props) { const { dataFrames, range, splitOpen, withTraceView } = props; const getLinks = useLinks(range, splitOpen); const theme = useTheme2(); @@ -75,7 +76,7 @@ export function UnconnectedNodeGraphContainer(props: Props & ConnectedProps; + export const UnconnectedReturnToDashboardButton: FC = ({ originPanelId, setDashboardQueriesToUpdateOnLoad, @@ -83,22 +96,4 @@ export const UnconnectedReturnToDashboardButton: FC = ({ ); }; -function mapStateToProps(state: StoreState, { exploreId }: { exploreId: ExploreId }) { - const explore = state.explore; - const splitted = isSplit(state); - const { datasourceInstance, queries, originPanelId } = explore[exploreId]!; - - return { - exploreId, - datasourceInstance, - queries, - originPanelId, - splitted, - }; -} - -const mapDispatchToProps = { - setDashboardQueriesToUpdateOnLoad, -}; - -export default connect(mapStateToProps, mapDispatchToProps)(UnconnectedReturnToDashboardButton); +export default connector(UnconnectedReturnToDashboardButton); diff --git a/public/app/features/explore/TableContainer.tsx b/public/app/features/explore/TableContainer.tsx index 042acc58f42..8b77f35b727 100644 --- a/public/app/features/explore/TableContainer.tsx +++ b/public/app/features/explore/TableContainer.tsx @@ -1,7 +1,7 @@ import React, { PureComponent } from 'react'; import { hot } from 'react-hot-loader'; -import { connect } from 'react-redux'; -import { DataFrame, TimeRange, ValueLinkConfig } from '@grafana/data'; +import { connect, ConnectedProps } from 'react-redux'; +import { ValueLinkConfig } from '@grafana/data'; import { Collapse, Table } from '@grafana/ui'; import { ExploreId, ExploreItemState } from 'app/types/explore'; import { StoreState } from 'app/types'; @@ -15,15 +15,28 @@ import { getFieldLinksForExplore } from './utils/links'; interface TableContainerProps { ariaLabel?: string; exploreId: ExploreId; - loading: boolean; width: number; onCellFilterAdded?: (filter: FilterItem) => void; - tableResult?: DataFrame; - splitOpen: typeof splitOpen; - range: TimeRange; } -export class TableContainer extends PureComponent { +function mapStateToProps(state: StoreState, { exploreId }: TableContainerProps) { + const explore = state.explore; + // @ts-ignore + const item: ExploreItemState = explore[exploreId]; + const { loading: loadingInState, tableResult, range } = item; + const loading = tableResult && tableResult.length > 0 ? false : loadingInState; + return { loading, tableResult, range }; +} + +const mapDispatchToProps = { + splitOpen, +}; + +const connector = connect(mapStateToProps, mapDispatchToProps); + +type Props = TableContainerProps & ConnectedProps; + +export class TableContainer extends PureComponent { getTableHeight() { const { tableResult } = this.props; @@ -71,17 +84,4 @@ export class TableContainer extends PureComponent { } } -function mapStateToProps(state: StoreState, { exploreId }: { exploreId: string }) { - const explore = state.explore; - // @ts-ignore - const item: ExploreItemState = explore[exploreId]; - const { loading: loadingInState, tableResult, range } = item; - const loading = tableResult && tableResult.length > 0 ? false : loadingInState; - return { loading, tableResult, range }; -} - -const mapDispatchToProps = { - splitOpen, -}; - -export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(TableContainer)); +export default hot(module)(connector)(TableContainer);