mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
* Fixes under public/app/plugins * Fixes under public/app/plugins/datasource * Fixes under public/app/features * Fixes under public/app/features * Fixes under public/app/features * Fixes under public/app/components * Fix PanelNotSupported test * Fix one more warning * Fix warning in usePanelSave * Fix traceview empty response * Azure monitor fixes * More fixes * Fix tests for azure monitor * Fixes after merging master * Add comment for disabled rules * Fixes after merging master * Fixes after merging master * Adress review comments * Fix azure tests * Address review feedbacks
41 lines
1.4 KiB
TypeScript
41 lines
1.4 KiB
TypeScript
import { connect, MapDispatchToPropsParam, MapStateToPropsParam, useDispatch } from 'react-redux';
|
|
import { cleanUpAction, StateSelector } from '../actions/cleanUp';
|
|
import React, { ComponentType, FunctionComponent, useEffect } from 'react';
|
|
import hoistNonReactStatics from 'hoist-non-react-statics';
|
|
|
|
export const connectWithCleanUp = <
|
|
TStateProps extends {} = {},
|
|
TDispatchProps = {},
|
|
TOwnProps = {},
|
|
State = {},
|
|
TSelector extends object = {},
|
|
Statics = {}
|
|
>(
|
|
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
|
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
|
|
stateSelector: StateSelector<TSelector>
|
|
) => (Component: ComponentType<any>) => {
|
|
const ConnectedComponent = connect(
|
|
mapStateToProps,
|
|
mapDispatchToProps
|
|
// @ts-ignore
|
|
)(Component);
|
|
|
|
const ConnectedComponentWithCleanUp: FunctionComponent = (props) => {
|
|
const dispatch = useDispatch();
|
|
useEffect(() => {
|
|
return function cleanUp() {
|
|
dispatch(cleanUpAction({ stateSelector }));
|
|
};
|
|
}, [dispatch]);
|
|
// @ts-ignore
|
|
return <ConnectedComponent {...props} />;
|
|
};
|
|
|
|
ConnectedComponentWithCleanUp.displayName = `ConnectWithCleanUp(${ConnectedComponent.displayName})`;
|
|
hoistNonReactStatics(ConnectedComponentWithCleanUp, Component);
|
|
type Hoisted = typeof ConnectedComponentWithCleanUp & Statics;
|
|
|
|
return ConnectedComponentWithCleanUp as Hoisted;
|
|
};
|