From 561e591c3845ca9938831e6536e37541f4ed6ed7 Mon Sep 17 00:00:00 2001 From: hamed musallam Date: Thu, 21 Nov 2024 11:35:46 +0100 Subject: [PATCH] fix: zones preferences --- .../SpectraPanel/SpectraPreferences.tsx | 18 +----- .../panels/ZonesPanel/ZonesPreferences.tsx | 58 +++++++++++-------- .../panels/extra/preferences/NucleusGroup.tsx | 9 +-- .../extra/preferences/NucleusPreferences.tsx | 12 +++- .../utility/replaceNucleiObjectKeys.ts | 11 ++++ 5 files changed, 61 insertions(+), 47 deletions(-) create mode 100644 src/component/utility/replaceNucleiObjectKeys.ts diff --git a/src/component/panels/SpectraPanel/SpectraPreferences.tsx b/src/component/panels/SpectraPanel/SpectraPreferences.tsx index 1166b286b..eebda884b 100644 --- a/src/component/panels/SpectraPanel/SpectraPreferences.tsx +++ b/src/component/panels/SpectraPanel/SpectraPreferences.tsx @@ -18,24 +18,12 @@ import { Scroller } from '../../elements/Scroller.js'; import useNucleus from '../../hooks/useNucleus.js'; import { usePanelPreferencesByNuclei } from '../../hooks/usePanelPreferences.js'; import { getSpectraObjectPaths } from '../../utility/getSpectraObjectPaths.js'; +import { replaceNucleiObjectKeys } from '../../utility/replaceNucleiObjectKeys.js'; import { NucleusGroup } from '../extra/preferences/NucleusGroup.js'; import { PreferencesContainer } from '../extra/preferences/PreferencesContainer.js'; import { SpectraColumnsManager } from './base/SpectraColumnsManager.js'; -function replaceNucleusKeys( - data: PanelsPreferences['spectra'], - searchValue: string, - replaceValue: string, -) { - const nuclei = {}; - - for (const key in data.nuclei) { - nuclei[key.replace(searchValue, replaceValue)] = data.nuclei[key]; - } - return { ...data, nuclei }; -} - const groupPaneStyle: GroupPaneStyle = { header: { color: 'black', @@ -90,7 +78,7 @@ function SpectraPreferences(props, ref: any) { const preferencesByNuclei = usePanelPreferencesByNuclei('spectra', nuclei); const methods = useForm({ - defaultValues: replaceNucleusKeys(preferencesByNuclei, ',', '_'), + defaultValues: replaceNucleiObjectKeys(preferencesByNuclei, ',', '_'), resolver: yupResolver(spectraPreferencesValidation), }); const { @@ -110,7 +98,7 @@ function SpectraPreferences(props, ref: any) { type: 'SET_PANELS_PREFERENCES', payload: { key: 'spectra', - value: replaceNucleusKeys(values, '_', ','), + value: replaceNucleiObjectKeys(values, '_', ','), }, }); return true; diff --git a/src/component/panels/ZonesPanel/ZonesPreferences.tsx b/src/component/panels/ZonesPanel/ZonesPreferences.tsx index 67e852c97..f4ba8eaa4 100644 --- a/src/component/panels/ZonesPanel/ZonesPreferences.tsx +++ b/src/component/panels/ZonesPanel/ZonesPreferences.tsx @@ -6,6 +6,7 @@ import useNucleus from '../../hooks/useNucleus.js'; import { usePanelPreferencesByNuclei } from '../../hooks/usePanelPreferences.js'; import { getUniqueNuclei } from '../../utility/getUniqueNuclei.js'; import { is2DNucleus } from '../../utility/nucleusToString.js'; +import { replaceNucleiObjectKeys } from '../../utility/replaceNucleiObjectKeys.js'; import type { NucleusPreferenceField } from '../extra/preferences/NucleusPreferences.js'; import { NucleusPreferences } from '../extra/preferences/NucleusPreferences.js'; import { PreferencesContainer } from '../extra/preferences/PreferencesContainer.js'; @@ -68,18 +69,21 @@ const preferences2DFields: NucleusPreferenceField[] = [ function ZonesPreferences(props, ref) { const preferences = usePreferences(); const nucleus = useNucleus(); - const nuclei2D = nucleus.filter((n) => is2DNucleus(n)); - const nuclei = useMemo(() => getUniqueNuclei(nucleus), [nucleus]); - const zonesPreferences = usePanelPreferencesByNuclei('zones', [ - ...nuclei, - ...nuclei2D, - ]); + const nuclei = useMemo( + () => + getUniqueNuclei(nucleus).concat(nucleus.filter((n) => is2DNucleus(n))), + [nucleus], + ); + const zonesPreferences = usePanelPreferencesByNuclei('zones', nuclei); const saveHandler = useCallback( (values) => { preferences.dispatch({ type: 'SET_PANELS_PREFERENCES', - payload: { key: 'zones', value: values }, + payload: { + key: 'zones', + value: replaceNucleiObjectKeys(values, '_', ','), + }, }); }, [preferences], @@ -92,27 +96,33 @@ function ZonesPreferences(props, ref) { useSettingImperativeHandle(ref, handleSubmit, saveHandler); useEffect(() => { - reset(zonesPreferences); + reset(replaceNucleiObjectKeys(zonesPreferences, ',', '_')); }, [reset, zonesPreferences]); return ( - {nuclei?.map((n) => ( - - ))} - {nuclei2D?.map((n) => ( - - ))} + {nuclei?.map((n) => { + if (is2DNucleus(n)) { + return ( + + ); + } else { + return ( + + ); + } + })} ); } diff --git a/src/component/panels/extra/preferences/NucleusGroup.tsx b/src/component/panels/extra/preferences/NucleusGroup.tsx index fe13d1613..c1c4faf90 100644 --- a/src/component/panels/extra/preferences/NucleusGroup.tsx +++ b/src/component/panels/extra/preferences/NucleusGroup.tsx @@ -24,17 +24,14 @@ const styles = { header: { borderBottom: '1px solid #e8e8e8', paddingBottom: '5px', + marginBottom: '10px', fontWeight: 'bold', color: '#4a4a4a', }, }; -export function NucleusGroup({ - nucleus, - children, - renderTop, - renderBottom, -}: NucleusGroupWithChildrenProps) { +export function NucleusGroup(props: NucleusGroupWithChildrenProps) { + const { nucleus, children, renderTop, renderBottom } = props; return (
diff --git a/src/component/panels/extra/preferences/NucleusPreferences.tsx b/src/component/panels/extra/preferences/NucleusPreferences.tsx index e2cad22c4..58c3af51d 100644 --- a/src/component/panels/extra/preferences/NucleusPreferences.tsx +++ b/src/component/panels/extra/preferences/NucleusPreferences.tsx @@ -15,13 +15,21 @@ interface NucleusPreferencesProps< > extends NucleusGroupProps, Pick, 'control'> { fields: NucleusPreferenceField[]; + nucleusTitle?: string; } export function NucleusPreferences(props: NucleusPreferencesProps) { - const { nucleus, fields, renderTop, renderBottom, control } = props; + const { + nucleus, + nucleusTitle = nucleus, + fields, + renderTop, + renderBottom, + control, + } = props; return ( diff --git a/src/component/utility/replaceNucleiObjectKeys.ts b/src/component/utility/replaceNucleiObjectKeys.ts new file mode 100644 index 000000000..409b400c0 --- /dev/null +++ b/src/component/utility/replaceNucleiObjectKeys.ts @@ -0,0 +1,11 @@ +export function replaceNucleiObjectKeys< + T extends { nuclei: Record }, +>(data: T, searchValue: string, replaceValue: string): T { + const nuclei: Record = {}; + + for (const key in data.nuclei) { + nuclei[key.replace(searchValue, replaceValue)] = data.nuclei[key]; + } + + return { ...data, nuclei } as T; +}