Skip to content

Commit 0a87912

Browse files
committed
refactor(chart): refactor chart dill option ref
1 parent e43573e commit 0a87912

File tree

5 files changed

+76
-75
lines changed

5 files changed

+76
-75
lines changed

frontend/src/app/components/ChartEditor.tsx

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { DownloadFileType } from 'app/constants';
2222
import useI18NPrefix from 'app/hooks/useI18NPrefix';
2323
import useMount from 'app/hooks/useMount';
2424
import { ChartDataRequestBuilder } from 'app/models/ChartDataRequestBuilder';
25-
import { ChartDrillOption } from 'app/models/ChartDrillOption';
2625
import ChartManager from 'app/models/ChartManager';
2726
import workbenchSlice, {
2827
useWorkbenchSlice,
@@ -52,9 +51,11 @@ import {
5251
import { IChart } from 'app/types/Chart';
5352
import { IChartDrillOption } from 'app/types/ChartDrillOption';
5453
import { ChartDTO } from 'app/types/ChartDTO';
55-
import { getDrillPaths } from 'app/utils/chartHelper';
5654
import { makeDownloadDataTask } from 'app/utils/fetch';
57-
import { transferChartConfigs } from 'app/utils/internalChartHelper';
55+
import {
56+
getChartDrillOption,
57+
transferChartConfigs,
58+
} from 'app/utils/internalChartHelper';
5859
import { CommonFormTypes } from 'globalConstants';
5960
import { FC, useCallback, useEffect, useMemo, useRef, useState } from 'react';
6061
import { useDispatch, useSelector } from 'react-redux';
@@ -200,8 +201,8 @@ export const ChartEditor: FC<ChartEditorProps> = ({
200201
}, [backendChart?.config?.chartGraphId]);
201202

202203
useEffect(() => {
203-
if (!drillOptionRef.current) {
204-
initChartDrillOption(chartConfig?.datas);
204+
if (!isEmptyArray(chartConfig?.datas) && !drillOptionRef.current) {
205+
drillOptionRef.current = getChartDrillOption(chartConfig?.datas);
205206
}
206207
}, [chartConfig?.datas, drillOptionRef]);
207208

@@ -269,7 +270,10 @@ export const ChartEditor: FC<ChartEditorProps> = ({
269270
},
270271
}),
271272
);
272-
initChartDrillOption(finalChartConfig?.datas);
273+
drillOptionRef.current = getChartDrillOption(
274+
chartConfig?.datas,
275+
drillOptionRef.current,
276+
);
273277
}, [dispatch, chart?.meta?.id, registerChartEvents]);
274278

275279
const handleChartChange = (c: IChart) => {
@@ -289,9 +293,12 @@ export const ChartEditor: FC<ChartEditorProps> = ({
289293
},
290294
}),
291295
);
292-
const drillOpt = initChartDrillOption(finalChartConfig?.datas);
296+
drillOptionRef.current = getChartDrillOption(
297+
chartConfig?.datas,
298+
drillOptionRef.current,
299+
);
293300
if (!expensiveQuery) {
294-
dispatch(refreshDatasetAction({ drillOption: drillOpt }));
301+
dispatch(refreshDatasetAction({ drillOption: drillOptionRef?.current }));
295302
} else {
296303
setAllowQuery(true);
297304
}
@@ -316,7 +323,13 @@ export const ChartEditor: FC<ChartEditorProps> = ({
316323
type,
317324
payload,
318325
needRefresh: payload.needRefresh,
319-
updateDrillOption: config => initChartDrillOption(config?.datas),
326+
updateDrillOption: config => {
327+
drillOptionRef.current = getChartDrillOption(
328+
config?.datas,
329+
drillOptionRef.current,
330+
);
331+
return drillOptionRef.current;
332+
},
320333
}),
321334
);
322335
},
@@ -529,25 +542,6 @@ export const ChartEditor: FC<ChartEditorProps> = ({
529542
dispatch(refreshDatasetAction({ drillOption: option }));
530543
};
531544

532-
const initChartDrillOption = (
533-
datas?: any[],
534-
): IChartDrillOption | undefined => {
535-
const drillPaths = getDrillPaths(datas);
536-
if (isEmptyArray(drillPaths)) {
537-
drillOptionRef.current = undefined;
538-
}
539-
if (
540-
!isEmptyArray(drillPaths) &&
541-
drillOptionRef.current
542-
?.getAllFields()
543-
?.map(p => p.uid)
544-
.join('-') !== drillPaths.map(p => p.uid).join('-')
545-
) {
546-
drillOptionRef.current = new ChartDrillOption(drillPaths);
547-
}
548-
return drillOptionRef.current;
549-
};
550-
551545
return (
552546
<StyledChartWorkbenchPage>
553547
<SaveFormContext.Provider value={saveFormContextValue}>

frontend/src/app/pages/MainPage/pages/VizPage/ChartPreview/ChartPreview.tsx

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,18 @@ import { VizHeader } from 'app/components/VizHeader';
2424
import { useCacheWidthHeight } from 'app/hooks/useCacheWidthHeight';
2525
import useI18NPrefix from 'app/hooks/useI18NPrefix';
2626
import { ChartDataRequestBuilder } from 'app/models/ChartDataRequestBuilder';
27-
import { ChartDrillOption } from 'app/models/ChartDrillOption';
2827
import ChartManager from 'app/models/ChartManager';
2928
import ChartDrillContext from 'app/pages/ChartWorkbenchPage/contexts/ChartDrillContext';
3029
import { useMainSlice } from 'app/pages/MainPage/slice';
3130
import { IChart } from 'app/types/Chart';
3231
import { IChartDrillOption } from 'app/types/ChartDrillOption';
33-
import { getDrillPaths } from 'app/utils/chartHelper';
3432
import { generateShareLinkAsync, makeDownloadDataTask } from 'app/utils/fetch';
33+
import { getChartDrillOption } from 'app/utils/internalChartHelper';
3534
import { FC, memo, useCallback, useEffect, useRef, useState } from 'react';
3635
import { useDispatch, useSelector } from 'react-redux';
3736
import { useHistory } from 'react-router-dom';
3837
import styled from 'styled-components/macro';
3938
import { BORDER_RADIUS, SPACE_LG } from 'styles/StyleConstants';
40-
import { isEmptyArray } from 'utils/object';
4139
import { useSaveAsViz } from '../hooks/useSaveAsViz';
4240
import { useVizSlice } from '../slice';
4341
import {
@@ -118,19 +116,10 @@ const ChartPreviewBoard: FC<{
118116
setVersion(newChartPreview.version);
119117
setChartPreview(newChartPreview);
120118

121-
const drillPaths = getDrillPaths(newChartPreview?.chartConfig?.datas);
122-
if (isEmptyArray(drillPaths)) {
123-
drillOptionRef.current = undefined;
124-
}
125-
if (
126-
!isEmptyArray(drillPaths) &&
127-
drillOptionRef.current
128-
?.getAllFields()
129-
?.map(p => p.uid)
130-
.join('-') !== drillPaths.map(p => p.uid).join('-')
131-
) {
132-
drillOptionRef.current = new ChartDrillOption(drillPaths);
133-
}
119+
drillOptionRef.current = getChartDrillOption(
120+
newChartPreview?.chartConfig?.datas,
121+
drillOptionRef?.current,
122+
);
134123

135124
if (
136125
!chart ||

frontend/src/app/pages/SharePage/ChartForShare.tsx

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@ import ChartDrillPaths from 'app/components/ChartDrill/ChartDrillPaths';
2121
import { ChartIFrameContainer } from 'app/components/ChartIFrameContainer';
2222
import useMount from 'app/hooks/useMount';
2323
import useResizeObserver from 'app/hooks/useResizeObserver';
24-
import { ChartDrillOption } from 'app/models/ChartDrillOption';
2524
import ChartManager from 'app/models/ChartManager';
2625
import { IChart } from 'app/types/Chart';
2726
import { IChartDrillOption } from 'app/types/ChartDrillOption';
28-
import { getDrillPaths } from 'app/utils/chartHelper';
27+
import { getChartDrillOption } from 'app/utils/internalChartHelper';
2928
import { FC, memo, useRef, useState } from 'react';
3029
import { useDispatch, useSelector } from 'react-redux';
3130
import styled from 'styled-components/macro';
32-
import { isEmptyArray } from 'utils/object';
3331
import ChartDrillContext from '../ChartWorkbenchPage/contexts/ChartDrillContext';
3432
import ControllerPanel from '../MainPage/pages/VizPage/ChartPreview/components/ControllerPanel';
3533
import {
@@ -77,19 +75,10 @@ const ChartForShare: FC<{
7775
if (!chartPreview) {
7876
return;
7977
}
80-
const drillPaths = getDrillPaths(chartPreview?.chartConfig?.datas);
81-
if (isEmptyArray(drillPaths)) {
82-
drillOptionRef.current = undefined;
83-
}
84-
if (
85-
!isEmptyArray(drillPaths) &&
86-
drillOptionRef.current
87-
?.getAllFields()
88-
?.map(p => p.uid)
89-
.join('-') !== drillPaths.map(p => p.uid).join('-')
90-
) {
91-
drillOptionRef.current = new ChartDrillOption(drillPaths);
92-
}
78+
drillOptionRef.current = getChartDrillOption(
79+
chartPreview?.chartConfig?.datas,
80+
drillOptionRef?.current,
81+
);
9382
dispatch(fetchShareDataSetByPreviewChartAction({ preview: chartPreview }));
9483
registerChartEvents(chart);
9584
});

frontend/src/app/utils/chartHelper.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,18 +1564,3 @@ export const getDrillableRows = (
15641564
return config.rows || [];
15651565
});
15661566
};
1567-
1568-
/**
1569-
* Get all Drill Paths
1570-
*
1571-
* @param {ChartDataConfig[]} configs
1572-
* @return {*} {ChartDataSectionField[]}
1573-
*/
1574-
export const getDrillPaths = (
1575-
configs?: ChartDataConfig[],
1576-
): ChartDataSectionField[] => {
1577-
return (configs || [])
1578-
?.filter(c => c.type === ChartDataSectionType.GROUP)
1579-
?.filter(d => Boolean(d.drillable))
1580-
?.flatMap(r => r.rows || []);
1581-
};

frontend/src/app/utils/internalChartHelper.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
ChartDataViewFieldCategory,
2323
DataViewFieldType,
2424
} from 'app/constants';
25+
import { ChartDrillOption } from 'app/models/ChartDrillOption';
2526
import {
2627
ChartConfig,
2728
ChartDataConfig,
@@ -33,6 +34,7 @@ import {
3334
ChartStyleConfigDTO,
3435
} from 'app/types/ChartConfigDTO';
3536
import { ChartDataViewMeta } from 'app/types/ChartDataViewMeta';
37+
import { IChartDrillOption } from 'app/types/ChartDrillOption';
3638
import {
3739
cond,
3840
curry,
@@ -602,3 +604,45 @@ export const buildDragItem = (item, children: any[] = []) => {
602604
children: children.map(c => buildDragItem(c)),
603605
};
604606
};
607+
608+
/**
609+
* Get all Drill Paths
610+
*
611+
* @param {ChartDataConfig[]} configs
612+
* @return {*} {ChartDataSectionField[]}
613+
*/
614+
const getDrillPaths = (
615+
configs?: ChartDataConfig[],
616+
): ChartDataSectionField[] => {
617+
return (configs || [])
618+
?.filter(c => c.type === ChartDataSectionType.GROUP)
619+
?.filter(d => Boolean(d.drillable))
620+
?.flatMap(r => r.rows || []);
621+
};
622+
623+
/**
624+
* Create or Update Chart Drill Option
625+
*
626+
* @param {ChartDataConfig[]} [datas]
627+
* @param {IChartDrillOption} [drillOption]
628+
* @return {*} {(IChartDrillOption | undefined)}
629+
*/
630+
export const getChartDrillOption = (
631+
datas?: ChartDataConfig[],
632+
drillOption?: IChartDrillOption,
633+
): IChartDrillOption | undefined => {
634+
const newDrillPaths = getDrillPaths(datas);
635+
if (isEmptyArray(newDrillPaths)) {
636+
return undefined;
637+
}
638+
if (
639+
!isEmptyArray(newDrillPaths) &&
640+
drillOption
641+
?.getAllFields()
642+
?.map(p => p.uid)
643+
.join('-') !== newDrillPaths.map(p => p.uid).join('-')
644+
) {
645+
return new ChartDrillOption(newDrillPaths);
646+
}
647+
return drillOption;
648+
};

0 commit comments

Comments
 (0)