Enhance stack management by preventing duplicate stacks and optimizing stack index usage

This commit is contained in:
Ross
2026-05-18 22:31:28 +01:00
parent 7c17a27ec3
commit 77bbbd548e
+16 -4
View File
@@ -2459,12 +2459,23 @@ function App({ container_id, imageStacks, autoCacheStack, annotationJson, viewer
const seriesGroups = await deriveSeriesGroupsFromImageIds(imageIds);
const primarySeries = seriesGroups[0]?.imageIds || imageIds;
// Add the incoming stack to the panel and capture its index.
let newStackIdx = 0;
// Check if a stack with the same imageIds already exists
let existingStackIdx = -1;
let newStackIdx = -1;
setAvailableStacks(prev => {
existingStackIdx = prev.findIndex(
s =>
s.imageIds.length === imageIds.length &&
s.imageIds.every((id, idx) => id === imageIds[idx])
);
if (existingStackIdx !== -1) {
return prev;
}
newStackIdx = prev.length;
return [...prev, { imageIds, name, seriesGroups }];
});
const stackIdxToUse = existingStackIdx !== -1 ? existingStackIdx : newStackIdx;
const resolvedZone = resolveDropZone(requestedZone);
if (resolvedZone === 'center') {
@@ -2472,7 +2483,7 @@ function App({ container_id, imageStacks, autoCacheStack, annotationJson, viewer
try {
setViewportModes(prev => { const n = [...prev]; n[viewportIdx] = 'stack'; return n; });
setViewportImageIds(prev => { const n = [...prev]; n[viewportIdx] = primarySeries; return n; });
setSelectedStackIdx(prev => { const n = [...prev]; n[viewportIdx] = newStackIdx; return n; });
setSelectedStackIdx(prev => { const n = [...prev]; n[viewportIdx] = stackIdxToUse; return n; });
setSeriesGroupsByViewport(prev => {
const n = [...prev];
n[viewportIdx] = seriesGroups;
@@ -2504,7 +2515,7 @@ function App({ container_id, imageStacks, autoCacheStack, annotationJson, viewer
try {
setViewportModes(prev => { const n = [...prev]; n[viewportIdx] = 'stack'; return n; });
setViewportImageIds(prev => { const n = [...prev]; n[viewportIdx] = imageIds; return n; });
setSelectedStackIdx(prev => { const n = [...prev]; n[viewportIdx] = newStackIdx; return n; });
setSelectedStackIdx(prev => { const n = [...prev]; n[viewportIdx] = stackIdxToUse; return n; });
setActiveViewport(viewportIdx);
await setLoadedImageIdsAndCacheMeta(imageIds);
} finally {
@@ -2518,6 +2529,7 @@ function App({ container_id, imageStacks, autoCacheStack, annotationJson, viewer
const insertedViewportIndex = targetNewRow * newCols + targetNewCol;
const nextImageIds = Array(MAX_VIEWPORTS).fill([] as string[]);
// If you use newStackIdx below, ensure it is only used if a new stack was added
const nextModes = Array(MAX_VIEWPORTS).fill('stack') as Array<'stack' | 'volume'>;
const nextSelected = Array(MAX_VIEWPORTS).fill(0);
const nextSeriesGroups = Array(MAX_VIEWPORTS).fill(null).map(() => [] as StackSeriesGroup[]);