| 1 | import { useLayoutEffect, useRef } from 'react';
|
|---|
| 2 | import { useIsPanorama } from '../context/PanoramaContext';
|
|---|
| 3 | import { selectChartLayout } from '../context/chartLayoutContext';
|
|---|
| 4 | import { useAppDispatch, useAppSelector } from './hooks';
|
|---|
| 5 | import { addLegendPayload, replaceLegendPayload, removeLegendPayload } from './legendSlice';
|
|---|
| 6 | export function SetLegendPayload(_ref) {
|
|---|
| 7 | var {
|
|---|
| 8 | legendPayload
|
|---|
| 9 | } = _ref;
|
|---|
| 10 | var dispatch = useAppDispatch();
|
|---|
| 11 | var isPanorama = useIsPanorama();
|
|---|
| 12 | var prevPayloadRef = useRef(null);
|
|---|
| 13 | useLayoutEffect(() => {
|
|---|
| 14 | if (isPanorama) {
|
|---|
| 15 | return;
|
|---|
| 16 | }
|
|---|
| 17 | if (prevPayloadRef.current === null) {
|
|---|
| 18 | dispatch(addLegendPayload(legendPayload));
|
|---|
| 19 | } else if (prevPayloadRef.current !== legendPayload) {
|
|---|
| 20 | dispatch(replaceLegendPayload({
|
|---|
| 21 | prev: prevPayloadRef.current,
|
|---|
| 22 | next: legendPayload
|
|---|
| 23 | }));
|
|---|
| 24 | }
|
|---|
| 25 | prevPayloadRef.current = legendPayload;
|
|---|
| 26 | }, [dispatch, isPanorama, legendPayload]);
|
|---|
| 27 | useLayoutEffect(() => {
|
|---|
| 28 | return () => {
|
|---|
| 29 | if (prevPayloadRef.current) {
|
|---|
| 30 | dispatch(removeLegendPayload(prevPayloadRef.current));
|
|---|
| 31 | prevPayloadRef.current = null;
|
|---|
| 32 | }
|
|---|
| 33 | };
|
|---|
| 34 | }, [dispatch]);
|
|---|
| 35 | return null;
|
|---|
| 36 | }
|
|---|
| 37 | export function SetPolarLegendPayload(_ref2) {
|
|---|
| 38 | var {
|
|---|
| 39 | legendPayload
|
|---|
| 40 | } = _ref2;
|
|---|
| 41 | var dispatch = useAppDispatch();
|
|---|
| 42 | var layout = useAppSelector(selectChartLayout);
|
|---|
| 43 | var prevPayloadRef = useRef(null);
|
|---|
| 44 | useLayoutEffect(() => {
|
|---|
| 45 | if (layout !== 'centric' && layout !== 'radial') {
|
|---|
| 46 | return;
|
|---|
| 47 | }
|
|---|
| 48 | if (prevPayloadRef.current === null) {
|
|---|
| 49 | dispatch(addLegendPayload(legendPayload));
|
|---|
| 50 | } else if (prevPayloadRef.current !== legendPayload) {
|
|---|
| 51 | dispatch(replaceLegendPayload({
|
|---|
| 52 | prev: prevPayloadRef.current,
|
|---|
| 53 | next: legendPayload
|
|---|
| 54 | }));
|
|---|
| 55 | }
|
|---|
| 56 | prevPayloadRef.current = legendPayload;
|
|---|
| 57 | }, [dispatch, layout, legendPayload]);
|
|---|
| 58 | useLayoutEffect(() => {
|
|---|
| 59 | return () => {
|
|---|
| 60 | if (prevPayloadRef.current) {
|
|---|
| 61 | dispatch(removeLegendPayload(prevPayloadRef.current));
|
|---|
| 62 | prevPayloadRef.current = null;
|
|---|
| 63 | }
|
|---|
| 64 | };
|
|---|
| 65 | }, [dispatch]);
|
|---|
| 66 | return null;
|
|---|
| 67 | } |
|---|