mirror of
https://github.com/grafana/grafana.git
synced 2025-09-23 18:52:33 +08:00

* Change nav structure when topnav is enable to do initial tests with new information architecture * Support for nested sections * Updated * sentance case * Progress on plugin challange * Rewrite to functional component * Progress * Updates * Progress * Progress on things * missing file * Fixing issue with runtime, need to use setter way to set component exposed via runtime * Move PageLayoutType to grafana/data * Fixing breadcrumb issue, adding more tests * reverted backend change * fix recursive issue with cleanup
37 lines
1.4 KiB
TypeScript
37 lines
1.4 KiB
TypeScript
import hoistNonReactStatics from 'hoist-non-react-statics';
|
|
import React, { ComponentType, FunctionComponent, useEffect } from 'react';
|
|
import { connect, MapDispatchToPropsParam, MapStateToPropsParam, useDispatch } from 'react-redux';
|
|
|
|
import { cleanUpAction, CleanUpAction } from '../actions/cleanUp';
|
|
|
|
export const connectWithCleanUp =
|
|
<TStateProps extends {} = {}, TDispatchProps = {}, TOwnProps = {}, State = {}, Statics = {}>(
|
|
mapStateToProps: MapStateToPropsParam<TStateProps, TOwnProps, State>,
|
|
mapDispatchToProps: MapDispatchToPropsParam<TDispatchProps, TOwnProps>,
|
|
cleanupAction: CleanUpAction
|
|
) =>
|
|
(Component: ComponentType<any>) => {
|
|
const ConnectedComponent = connect(
|
|
mapStateToProps,
|
|
mapDispatchToProps
|
|
// @ts-ignore
|
|
)(Component);
|
|
|
|
const ConnectedComponentWithCleanUp: FunctionComponent = (props) => {
|
|
const dispatch = useDispatch();
|
|
useEffect(() => {
|
|
return function cleanUp() {
|
|
dispatch(cleanUpAction({ cleanupAction: cleanupAction }));
|
|
};
|
|
}, [dispatch]);
|
|
// @ts-ignore
|
|
return <ConnectedComponent {...props} />;
|
|
};
|
|
|
|
ConnectedComponentWithCleanUp.displayName = `ConnectWithCleanUp(${ConnectedComponent.displayName})`;
|
|
hoistNonReactStatics(ConnectedComponentWithCleanUp, Component);
|
|
type Hoisted = typeof ConnectedComponentWithCleanUp & Statics;
|
|
|
|
return ConnectedComponentWithCleanUp as Hoisted;
|
|
};
|