Skip to content

Commit 1f80b2b

Browse files
committed
refactor(chart): move get chart icons into chart manager
1 parent 5aded4d commit 1f80b2b

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

frontend/src/app/hooks/useGetVizIcon.tsx

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,31 @@ import {
55
FundFilled,
66
} from '@ant-design/icons';
77
import ChartManager from 'app/pages/ChartWorkbenchPage/models/ChartManager';
8-
import { IChart } from 'app/types/Chart';
9-
import React, { useCallback, useMemo, useState } from 'react';
8+
import React, { useCallback } from 'react';
109
import styled from 'styled-components/macro';
1110
import { FONT_SIZE_TITLE } from 'styles/StyleConstants';
1211

1312
function useGetVizIcon() {
1413
const chartManager = ChartManager.instance();
15-
const [allCharts] = useState<IChart[]>(chartManager.getAllCharts());
16-
const chartsIcon = useMemo(() => {
17-
let iconObj = {};
18-
allCharts.forEach(v => {
19-
iconObj[v.meta.id] = v.meta.icon;
20-
});
21-
return iconObj;
22-
}, [allCharts]);
14+
const chartIcons = chartManager.getAllChartIcons();
2315

24-
return useCallback(({ relType, avatar, subType }) => {
25-
switch (relType) {
26-
case 'DASHBOARD':
27-
return subType !== null ? (
28-
renderIcon(subType === 'free' ? 'CombinedShape' : 'kanban')
29-
) : (
30-
<FundFilled />
31-
);
32-
case 'DATACHART':
33-
return avatar ? renderIcon(chartsIcon[avatar]) : <BarChartOutlined />;
34-
default:
35-
return p => (p.expanded ? <FolderOpenFilled /> : <FolderFilled />);
36-
}
37-
}, []);
16+
return useCallback(
17+
({ relType, avatar, subType }) => {
18+
switch (relType) {
19+
case 'DASHBOARD':
20+
return subType !== null ? (
21+
renderIcon(subType === 'free' ? 'CombinedShape' : 'kanban')
22+
) : (
23+
<FundFilled />
24+
);
25+
case 'DATACHART':
26+
return avatar ? renderIcon(chartIcons[avatar]) : <BarChartOutlined />;
27+
default:
28+
return p => (p.expanded ? <FolderOpenFilled /> : <FolderFilled />);
29+
}
30+
},
31+
[chartIcons],
32+
);
3833
}
3934

4035
export default useGetVizIcon;

frontend/src/app/pages/ChartWorkbenchPage/models/ChartManager.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ class ChartManager {
7676
return this._charts || [];
7777
}
7878

79+
public getAllChartIcons() {
80+
return this._charts.reduce((acc, cur) => {
81+
acc[cur.meta.id] = cur.meta.icon;
82+
return acc;
83+
}, {});
84+
}
85+
7986
public getById(id?: string) {
8087
if (id === null || id === undefined) {
8188
return;

0 commit comments

Comments
 (0)