@@ -11,6 +11,7 @@ import {
1111import { selectCanvasSlice } from 'features/controlLayers/store/selectors' ;
1212import type { CanvasState , RefImagesState } from 'features/controlLayers/store/types' ;
1313import type { ImageUsage } from 'features/deleteImageModal/store/types' ;
14+ import { selectListImageNamesQueryArgs } from 'features/gallery/store/gallerySelectors' ;
1415import { imageSelected } from 'features/gallery/store/gallerySlice' ;
1516import { fieldImageCollectionValueChanged , fieldImageValueChanged } from 'features/nodes/store/nodesSlice' ;
1617import { selectNodesSlice } from 'features/nodes/store/selectors' ;
@@ -79,11 +80,18 @@ const handleDeletions = async (image_names: string[], store: AppStore) => {
7980 try {
8081 const { dispatch, getState } = store ;
8182 const state = getState ( ) ;
82- await dispatch ( imagesApi . endpoints . deleteImages . initiate ( { image_names } , { track : false } ) ) . unwrap ( ) ;
83+ const { data } = imagesApi . endpoints . getImageNames . select ( selectListImageNamesQueryArgs ( state ) ) ( state ) ;
84+ const index = data ?. image_names . findIndex ( ( name ) => name === image_names [ 0 ] ) ;
85+ const { deleted_images } = await dispatch (
86+ imagesApi . endpoints . deleteImages . initiate ( { image_names } , { track : false } )
87+ ) . unwrap ( ) ;
88+
89+ const newImageNames = data ?. image_names . filter ( ( name ) => ! deleted_images . includes ( name ) ) || [ ] ;
90+ const newSelectedImage = newImageNames [ index ?? 0 ] || null ;
8391
8492 if ( intersection ( state . gallery . selection , image_names ) . length > 0 ) {
8593 // Some selected images were deleted, clear selection
86- dispatch ( imageSelected ( null ) ) ;
94+ dispatch ( imageSelected ( newSelectedImage ) ) ;
8795 }
8896
8997 // We need to reset the features where the image is in use - none of these work if their image(s) don't exist
0 commit comments