@@ -6,7 +6,7 @@ import { getPrefixedId } from 'features/controlLayers/konva/util';
66import { canvasMetadataRecalled } from 'features/controlLayers/store/canvasSlice' ;
77import type { FLUXReduxImageInfluence , RefImagesState } from 'features/controlLayers/store/types' ;
88import { zModelIdentifierField } from 'features/nodes/types/common' ;
9- import { isEqual } from 'lodash-es' ;
9+ import { clamp , isEqual } from 'lodash-es' ;
1010import type { ApiModelConfig , FLUXReduxModelConfig , ImageDTO , IPAdapterModelConfig } from 'services/api/types' ;
1111import { assert } from 'tsafe' ;
1212import type { PartialDeep } from 'type-fest' ;
@@ -188,10 +188,11 @@ export const refImagesSlice = createSlice({
188188 } ,
189189 refImageDeleted : ( state , action : PayloadActionWithId ) => {
190190 const { id } = action . payload ;
191+ const currentIndex = state . entities . findIndex ( ( rg ) => rg . id === id ) ;
191192 state . entities = state . entities . filter ( ( rg ) => rg . id !== id ) ;
192- if ( state . selectedEntityId === id ) {
193- state . selectedEntityId = null ;
194- }
193+ const nextIndex = clamp ( currentIndex , 0 , state . entities . length - 1 ) ;
194+ const nextEntity = state . entities [ nextIndex ] ;
195+ state . selectedEntityId = nextEntity ?. id ?? null ;
195196 } ,
196197 refImageSelected : ( state , action : PayloadAction < { id : string } > ) => {
197198 const { id } = action . payload ;
@@ -238,7 +239,7 @@ export const refImagesPersistConfig: PersistConfig<RefImagesState> = {
238239 name : refImagesSlice . name ,
239240 initialState : getInitialRefImagesState ( ) ,
240241 migrate,
241- persistDenylist : [ 'isPanelOpen' ] ,
242+ persistDenylist : [ 'selectedEntityId' , ' isPanelOpen'] ,
242243} ;
243244
244245export const selectRefImagesSlice = ( state : RootState ) => state . refImages ;
0 commit comments