diff --git a/dicom-viewer/src/App.tsx b/dicom-viewer/src/App.tsx index 11210cf..50fe66a 100644 --- a/dicom-viewer/src/App.tsx +++ b/dicom-viewer/src/App.tsx @@ -3189,29 +3189,37 @@ export default function App({ container_id, imageStacks, autoCacheStack, annotat name: string, requestedZone: ViewportDropZone, ) => { - const imageIds = await expandMultiframeImageIds(rawImageIds.map(normalizeToWadouriOrExternal)); + const normalizedRawImageIds = dedupeImageIds(rawImageIds.map(normalizeToWadouriOrExternal)); + if (normalizedRawImageIds.length === 0) return; + + const existingStackIdx = availableStacks.findIndex((stackObj) => { + const existing = dedupeImageIds((stackObj.imageIds || []).map(normalizeToWadouriOrExternal)); + return ( + existing.length === normalizedRawImageIds.length + && existing.every((id, idx) => id === normalizedRawImageIds[idx]) + ); + }); + + if (existingStackIdx !== -1) { + // Reuse existing stack definitions whenever possible to avoid expensive + // expansion/probing and unnecessary network requests. + await applyStackDrop(viewportIdx, existingStackIdx, requestedZone); + setActiveViewport(viewportIdx); + return; + } + + const imageIds = await expandMultiframeImageIds(normalizedRawImageIds); if (imageIds.length === 0) return; await setLoadedImageIdsAndCacheMeta(imageIds, { force: Boolean(autoCacheStack) }); const seriesGroups = await deriveSeriesGroupsFromImageIds(imageIds); const primarySeries = seriesGroups[0]?.imageIds || imageIds; - - // 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, groupingSource: "derived" }]; }); - const stackIdxToUse = existingStackIdx !== -1 ? existingStackIdx : newStackIdx; + const stackIdxToUse = newStackIdx; const resolvedZone = resolveDropZone(requestedZone); if (resolvedZone === 'center') { @@ -5583,6 +5591,26 @@ export default function App({ container_id, imageStacks, autoCacheStack, annotat )} + {loadingState === "displayset" && ( +