Skip to content
Snippets Groups Projects
Unverified Commit 4162c2f0 authored by Oisin Coveney's avatar Oisin Coveney Committed by GitHub
Browse files

Memoize useChartSettingsSections variables (#49470)

parent de559f49
No related branches found
No related tags found
No related merge requests found
......@@ -35,22 +35,26 @@ export const useChartSettingsSections = ({
return sectionObj;
}, [widgets]);
const sectionNames = Object.keys(sections);
// This sorts the section radio buttons.
const sectionSortOrder = [
"data",
"display",
"axes",
// include all section names so any forgotten sections are sorted to the end
...sectionNames.map(x => x.toLowerCase()),
];
sectionNames.sort((a, b) => {
const [aIdx, bIdx] = [a, b].map(x =>
sectionSortOrder.indexOf(x.toLowerCase()),
);
return aIdx - bIdx;
});
const sectionNames = useMemo(() => {
const names = Object.keys(sections);
const sectionSortOrder = [
"data",
"display",
"axes",
// include all section names so any forgotten sections are sorted to the end
...names.map(x => x.toLowerCase()),
];
names.sort((a, b) => {
const [aIdx, bIdx] = [a, b].map(x =>
sectionSortOrder.indexOf(x.toLowerCase()),
);
return aIdx - bIdx;
});
return names;
}, [sections]);
const chartSettingCurrentSection = useMemo(
() =>
......@@ -61,22 +65,30 @@ export const useChartSettingsSections = ({
[currentSection, sectionNames, sections],
);
const visibleWidgets = sections[chartSettingCurrentSection] || [];
const visibleWidgets = useMemo(
() => sections[chartSettingCurrentSection] || [],
[chartSettingCurrentSection, sections],
);
const currentSectionHasColumnSettings = visibleWidgets.some(
(widget: Widget) => widget.id === "column_settings",
const currentSectionHasColumnSettings = useMemo(
() =>
visibleWidgets.some((widget: Widget) => widget.id === "column_settings"),
[visibleWidgets],
);
const showSectionPicker =
// don't show section tabs for a single section
sectionNames.length > 1 &&
// hide the section picker if the only widget is column_settings
!(
visibleWidgets.length === 1 &&
visibleWidgets[0].id === "column_settings" &&
// and this section doesn't have that as a direct child
!currentSectionHasColumnSettings
);
const showSectionPicker = useMemo(
() =>
// don't show section tabs for a single section
sectionNames.length > 1 &&
// hide the section picker if the only widget is column_settings
!(
visibleWidgets.length === 1 &&
visibleWidgets[0].id === "column_settings" &&
// and this section doesn't have that as a direct child
!currentSectionHasColumnSettings
),
[currentSectionHasColumnSettings, sectionNames.length, visibleWidgets],
);
return {
sectionNames,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment