diff --git a/dicom-viewer/src/App.tsx b/dicom-viewer/src/App.tsx index 25fe084..3fedfbf 100644 --- a/dicom-viewer/src/App.tsx +++ b/dicom-viewer/src/App.tsx @@ -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[]);