Skip to content

Commit 6099d62

Browse files
fix: serialize spectra object
1 parent 9dd6895 commit 6099d62

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

src/NMRiumWrapper.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useLoadSpectra } from './hooks/useLoadSpectra';
77
import { usePreferences } from './hooks/usePreferences';
88
import { useWhiteList } from './hooks/useWhiteList';
99
import AboutUsModal from './modal/AboutUsModal';
10+
import { mapSpectra } from './utilities/mapSpectra';
1011

1112
const styles: Record<'container' | 'loadingContainer', CSSProperties> = {
1213
container: {
@@ -39,7 +40,13 @@ export default function NMRiumWrapper() {
3940

4041
const { workspace, preferences, defaultEmptyMessage } = usePreferences();
4142
const dataChangeHandler = useCallback<NMRiumChangeCb>((state, source) => {
42-
events.trigger('data-change', { state, source });
43+
//TODO: remove map spectra once this issue resolved
44+
//a temporary fix by remove the `logger` and `keepSource` objects from each spectrum, this should removed once we solve the issue in nmr-load-save
45+
state.data.spectra = mapSpectra(state.data.spectra);
46+
events.trigger('data-change', {
47+
state,
48+
source,
49+
});
4350
}, []);
4451

4552
const { load: loadSpectra, isLoading, data: loadedData } = useLoadSpectra();

src/hooks/useLoadSpectra.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { useCallback, useMemo, useState } from 'react';
1111
import events from '../events';
1212
import { getFileNameFromURL } from '../utilities/getFileNameFromURL';
1313
import { isArrayOfString } from '../utilities/isArrayOfString';
14+
import { mapSpectra } from '../utilities/mapSpectra';
1415

1516
const PARSING_OPTIONS: Partial<ParsingOptions> = {
1617
onLoadProcessing: { autoProcessing: true },
@@ -23,7 +24,10 @@ async function loadSpectraFromFiles(files: File[]) {
2324
const {
2425
nmriumState: { data },
2526
} = await read(fileCollection, PARSING_OPTIONS);
26-
return data;
27+
//TODO: remove map spectra once this issue resolved
28+
//a temporary fix by remove the `logger` and `keepSource` objects from each spectrum, this should removed once we solve the issue in nmr-load-save
29+
const mapData = { ...data, spectra: mapSpectra(data?.spectra) };
30+
return mapData;
2731
}
2832

2933
async function loadSpectraFromURLs(urls: string[]) {
@@ -39,6 +43,11 @@ async function loadSpectraFromURLs(urls: string[]) {
3943
}, []);
4044

4145
const { data } = await readFromWebSource({ entries }, PARSING_OPTIONS);
46+
//TODO: remove map spectra once this issue resolved
47+
//a temporary fix by remove the `logger` and `keepSource` objects from each spectrum, this should removed once we solve the issue in nmr-load-save
48+
const mapData = { ...data, spectra: mapSpectra(data?.spectra) };
49+
return mapData;
50+
4251
return data;
4352
}
4453

src/utilities/mapSpectra.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* eslint-disable @typescript-eslint/dot-notation */
2+
import { Spectrum } from 'nmr-load-save';
3+
4+
export function mapSpectra(spectra?: Spectrum[]) {
5+
if (!spectra) return [];
6+
7+
return spectra.map((spectrum) => {
8+
const cloneSpectrum = { ...spectrum };
9+
if ('logger' in cloneSpectrum || 'keepSource' in cloneSpectrum) {
10+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
11+
delete cloneSpectrum['logger'];
12+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
13+
delete cloneSpectrum['keepSource'];
14+
}
15+
return cloneSpectrum;
16+
}, []);
17+
}

0 commit comments

Comments
 (0)