Skip to content

Commit 85ede2d

Browse files
authored
fix: ensure active spectrum indexes match internal state indexes (#4095)
Spectra loading is asyncronous which can change the order of spectra in the internal state. This have unexpected behavior if you display 2d spectra but you get the active spectrum by index and it returns a 1d spectrum.
1 parent c81fcba commit 85ede2d

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

src/component/reducer/actions/LoadAction.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,29 @@ function setData(draft: Draft<State>, input: InputProps | InitiateProps) {
179179
}),
180180
);
181181

182+
// Spectra loading is async, it can cause index reordering
183+
// this code section is here to fix the index stored into `activeSpectra`
184+
{
185+
const mapEntries = draft.data.map((spectrum, index) => {
186+
return [spectrum.id, index] as const;
187+
});
188+
const idToIndex = new Map(mapEntries);
189+
190+
const nucleusActiveSpectra = Object.values(
191+
draft.view.spectra.activeSpectra,
192+
);
193+
for (const activeSpectra of nucleusActiveSpectra) {
194+
if (!activeSpectra) continue;
195+
196+
for (const activeSpectrum of activeSpectra) {
197+
const internalIndex = idToIndex.get(activeSpectrum.id);
198+
if (typeof internalIndex !== 'number') continue;
199+
200+
activeSpectrum.index = internalIndex;
201+
}
202+
}
203+
}
204+
182205
draft.view.spectraContourLevels = initializeContours(draft.data);
183206

184207
setCorrelation(draft, correlations);

0 commit comments

Comments
 (0)