1717 * under the License.
1818 */
1919import { useState , useEffect , useCallback , useMemo , ReactNode } from 'react' ;
20+ import { useDispatch , useSelector } from 'react-redux' ;
2021import Split from 'react-split' ;
2122import { t } from '@apache-superset/core/translation' ;
2223import {
@@ -33,6 +34,11 @@ import {
3334import { Alert } from '@apache-superset/core/components' ;
3435import { css , styled , useTheme } from '@apache-superset/core/theme' ;
3536import ChartContainer from 'src/components/Chart/ChartContainer' ;
37+ import { updateExploreChartState } from 'src/explore/actions/exploreActions' ;
38+ import {
39+ convertChartStateToOwnState ,
40+ hasChartStateConverter ,
41+ } from 'src/dashboard/util/chartStateConverter' ;
3642import {
3743 getItem ,
3844 setItem ,
@@ -43,6 +49,7 @@ import { getDatasourceAsSaveableDataset } from 'src/utils/datasourceUtils';
4349import { buildV1ChartDataPayload } from 'src/explore/exploreUtils' ;
4450import { getChartRequiredFieldsMissingMessage } from 'src/utils/getChartRequiredFieldsMissingMessage' ;
4551import type { ChartState , Datasource } from 'src/explore/types' ;
52+ import type { ExploreState } from 'src/explore/reducers/exploreReducer' ;
4653import type { Slice } from 'src/types/Chart' ;
4754import LastQueriedLabel from 'src/components/LastQueriedLabel' ;
4855import { DataTablesPane } from '../DataTablesPane' ;
@@ -126,6 +133,28 @@ const Styles = styled.div<{ showSplite: boolean }>`
126133 }
127134` ;
128135
136+ const EMPTY_OBJECT : Record < string , never > = { } ;
137+
138+ const createOwnStateWithChartState = (
139+ baseOwnState : JsonObject ,
140+ chartState : { state ?: JsonObject } | undefined ,
141+ vizTypeArg : string ,
142+ ) : JsonObject => {
143+ if ( ! hasChartStateConverter ( vizTypeArg ) ) {
144+ return baseOwnState ;
145+ }
146+ const state = chartState ?. state ;
147+ if ( ! state ) {
148+ return baseOwnState ;
149+ }
150+ const convertedState = convertChartStateToOwnState ( vizTypeArg , state ) ;
151+ return {
152+ ...baseOwnState ,
153+ ...convertedState ,
154+ chartState : state ,
155+ } ;
156+ } ;
157+
129158const ExploreChartPanel = ( {
130159 chart,
131160 slice,
@@ -145,8 +174,34 @@ const ExploreChartPanel = ({
145174 can_download : canDownload ,
146175} : ExploreChartPanelProps ) => {
147176 const theme = useTheme ( ) ;
177+ const dispatch = useDispatch ( ) ;
148178 const gutterMargin = theme . sizeUnit * GUTTER_SIZE_FACTOR ;
149179 const gutterHeight = theme . sizeUnit * GUTTER_SIZE_FACTOR ;
180+
181+ const chartState = useSelector (
182+ ( state : { explore ?: ExploreState } ) =>
183+ state . explore ?. chartStates ?. [ chart . id ] ,
184+ ) ;
185+
186+ const handleChartStateChange = useCallback (
187+ ( chartStateArg : JsonObject ) => {
188+ if ( hasChartStateConverter ( vizType ) ) {
189+ dispatch ( updateExploreChartState ( chart . id , chartStateArg ) ) ;
190+ }
191+ } ,
192+ [ dispatch , chart . id , vizType ] ,
193+ ) ;
194+
195+ const mergedOwnState = useMemo (
196+ ( ) =>
197+ createOwnStateWithChartState (
198+ ownState || EMPTY_OBJECT ,
199+ chartState as { state ?: JsonObject } | undefined ,
200+ vizType ,
201+ ) ,
202+ [ ownState , chartState , vizType ] ,
203+ ) ;
204+
150205 const {
151206 ref : chartPanelRef ,
152207 observerRef : resizeObserverRef ,
@@ -259,7 +314,7 @@ const ExploreChartPanel = ({
259314 < ChartContainer
260315 width = { Math . floor ( chartPanelWidth ) }
261316 height = { chartPanelHeight }
262- ownState = { ownState }
317+ ownState = { mergedOwnState }
263318 annotationData = { chart . annotationData }
264319 chartId = { chart . id }
265320 triggerRender = { triggerRender }
@@ -277,6 +332,7 @@ const ExploreChartPanel = ({
277332 timeout = { timeout }
278333 triggerQuery = { chart . triggerQuery }
279334 vizType = { vizType }
335+ onChartStateChange = { handleChartStateChange }
280336 { ...( chart . chartAlert && { chartAlert : chart . chartAlert } ) }
281337 { ...( chart . chartStackTrace && {
282338 chartStackTrace : chart . chartStackTrace ,
@@ -304,8 +360,9 @@ const ExploreChartPanel = ({
304360 errorMessage ,
305361 force ,
306362 formData ,
363+ handleChartStateChange ,
307364 onQuery ,
308- ownState ,
365+ mergedOwnState ,
309366 timeout ,
310367 triggerRender ,
311368 vizType ,
0 commit comments