From c0ad5bc4a4205ddbc0bda6b987147f17a17ee6d9 Mon Sep 17 00:00:00 2001 From: Mary Hipp Date: Tue, 25 Jun 2024 15:06:19 -0400 Subject: [PATCH] fix when deleting first image in list --- .../listeners/imageDeleted.ts | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/imageDeleted.ts b/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/imageDeleted.ts index a62cf62861..88f7540c66 100644 --- a/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/imageDeleted.ts +++ b/invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/imageDeleted.ts @@ -117,20 +117,7 @@ export const addRequestedSingleImageDeletionListener = (startAppListening: AppSt } dispatch(isModalOpenChanged(false)); - const state = getState(); - const lastSelectedImage = state.gallery.selection[state.gallery.selection.length - 1]?.image_name; - - if (imageDTO && imageDTO?.image_name === lastSelectedImage) { - const baseQueryArgs = selectListImagesQueryArgs(state); - const { data } = imagesApi.endpoints.listImages.select(baseQueryArgs)(state); - - if (data && data.items[0]) { - dispatch(imageSelected(data.items[0])); - } else { - dispatch(imageSelected(null)); - } - } // We need to reset the features where the image is in use - none of these work if their image(s) don't exist if (imageUsage.isCanvasImage) { @@ -155,6 +142,20 @@ export const addRequestedSingleImageDeletionListener = (startAppListening: AppSt if (wasImageDeleted) { dispatch(api.util.invalidateTags([{ type: 'Board', id: imageDTO.board_id ?? 'none' }])); } + + const lastSelectedImage = state.gallery.selection[state.gallery.selection.length - 1]?.image_name; + + if (imageDTO && imageDTO?.image_name === lastSelectedImage) { + const baseQueryArgs = selectListImagesQueryArgs(state); + const { data } = imagesApi.endpoints.listImages.select(baseQueryArgs)(state); + + if (data && data.items) { + const newlySelectedImage = data?.items.find(img => img.image_name !== imageDTO?.image_name) + dispatch(imageSelected(newlySelectedImage || null)); + } else { + dispatch(imageSelected(null)); + } + } }, });