import React , { useEffect , useMemo , useRef } from 'react' ; import { EuiResizeObserver } from '@elastic/eui' ; import { debounce } from 'lodash' ; import { IInterpreterRenderHandlers } from '../../../expressions/public' ; import type { PersistedState } from '../../../visualizations/public' ; import { ChartsPluginSetup } from '../../../charts/public' ; import { VislibRenderValue } from './vis_type_vislib_vis_fn' ; import { createVislibVisController , VislibVisController } from './vis_controller' ; import { VisTypeVislibCoreSetup } from './plugin' ; import { PieRenderValue } from './pie_fn' ; import './index.scss' ; type VislibWrapperProps = ( VislibRenderValue | PieRenderValue ) & { core : VisTypeVislibCoreSetup ; charts : ChartsPluginSetup ; handlers : IInterpreterRenderHandlers ; } ; const VislibWrapper = ( { core , charts , visData , visConfig , handlers } ) => { const chartDiv = useRef < HTMLDivElement > ( null ) ; const visController = useRef < VislibVisController | null > ( null ) ; const updateChart = useMemo ( ( ) => debounce ( ( ) => { if ( visController . current ) { visController . current . render ( visData , visConfig , handlers ) ; } } , 100 ) , [ visConfig , visData , handlers ] ) ; useEffect ( ( ) => { if ( chartDiv . current ) { const Controller = createVislibVisController ( core , charts ) ; visController . current = new Controller ( chartDiv . current ) ; } return ( ) => { visController . current ?. destroy ( ) ; visController . current = null ; } ; } , [ core , charts ] ) ; useEffect ( updateChart , [ updateChart ] ) ; useEffect ( ( ) => { if ( handlers . uiState ) { const uiState = handlers . uiState as PersistedState ; uiState . on ( 'change' , updateChart ) ; return ( ) => { uiState ?. off ( 'change' , updateChart ) ; } ; } } , [ handlers . uiState , updateChart ] ) ; return ( < EuiResizeObserver onResize = { updateChart } > { ( resizeRef ) => ( < div className = "vislib__wrapper" ref = { resizeRef } > < div className = "vislib__container" ref = { chartDiv } / > < /div>)} < /EuiResizeObserver>); } ; export { VislibWrapper as default } ;