| import * as React from 'react'; |
| import { Context, MutableRefObject, ReactNode, useRef } from 'react'; |
| import { Provider } from 'react-redux'; |
| import { Action, Store } from '@reduxjs/toolkit'; |
| import { createRechartsStore, RechartsRootState } from './store'; |
| import { useIsPanorama } from '../context/PanoramaContext'; |
| import { RechartsReduxContext, RechartsReduxContextValue } from './RechartsReduxContext'; |
|
|
| type RechartsStoreProviderProps = { |
| children: ReactNode; |
| preloadedState?: Partial<RechartsRootState>; |
| reduxStoreName?: string; |
| }; |
|
|
| export function RechartsStoreProvider({ preloadedState, children, reduxStoreName }: RechartsStoreProviderProps) { |
| const isPanorama = useIsPanorama(); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const storeRef: MutableRefObject<Store<RechartsRootState, Action> | null> = useRef(null); |
|
|
| |
| |
| |
| |
| |
| |
| |
| if (isPanorama) { |
| return children; |
| } |
|
|
| if (storeRef.current == null) { |
| storeRef.current = createRechartsStore(preloadedState, reduxStoreName); |
| } |
|
|
| |
| const nonNullContext: Context<RechartsReduxContextValue> = RechartsReduxContext; |
|
|
| return ( |
| <Provider context={nonNullContext} store={storeRef.current}> |
| {children} |
| </Provider> |
| ); |
| } |
|
|