File size: 1,598 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { createAction, createListenerMiddleware, ListenerEffectAPI, PayloadAction } from '@reduxjs/toolkit';
import { SyntheticEvent } from 'react';
import { CategoricalChartFunc } from '../chart/types';
import { MouseHandlerDataParam } from '../synchronisation/types';
import {
  selectActiveLabel,
  selectActiveTooltipCoordinate,
  selectActiveTooltipDataKey,
  selectActiveTooltipIndex,
  selectIsTooltipActive,
} from './selectors/tooltipSelectors';
import { AppDispatch, RechartsRootState } from './store';

type ExternalEventActionPayload = {
  reactEvent: SyntheticEvent;
  handler: CategoricalChartFunc | undefined;
};

export const externalEventAction = createAction<ExternalEventActionPayload>('externalEvent');

export const externalEventsMiddleware = createListenerMiddleware();

externalEventsMiddleware.startListening({
  actionCreator: externalEventAction,
  effect: (
    action: PayloadAction<ExternalEventActionPayload>,
    listenerApi: ListenerEffectAPI<RechartsRootState, AppDispatch>,
  ) => {
    if (action.payload.handler == null) {
      return;
    }

    const state: RechartsRootState = listenerApi.getState();
    const nextState: MouseHandlerDataParam = {
      activeCoordinate: selectActiveTooltipCoordinate(state),
      activeDataKey: selectActiveTooltipDataKey(state),
      activeIndex: selectActiveTooltipIndex(state),
      activeLabel: selectActiveLabel(state),
      activeTooltipIndex: selectActiveTooltipIndex(state),
      isTooltipActive: selectIsTooltipActive(state),
    };

    action.payload.handler(nextState, action.payload.reactEvent);
  },
});