|
| 1 | +import { logger } from 'app/logging/logger'; |
1 | 2 | import type { AppDispatch, RootState } from 'app/store/store'; |
2 | 3 | import { deepClone } from 'common/util/deepClone'; |
3 | 4 | import { selectDefaultIPAdapter } from 'features/controlLayers/hooks/addLayerHooks'; |
@@ -30,14 +31,18 @@ import { calculateNewSize } from 'features/controlLayers/util/getScaledBoundingB |
30 | 31 | import { imageToCompareChanged, selectionChanged } from 'features/gallery/store/gallerySlice'; |
31 | 32 | import type { BoardId } from 'features/gallery/store/types'; |
32 | 33 | import { fieldImageCollectionValueChanged, fieldImageValueChanged } from 'features/nodes/store/nodesSlice'; |
33 | | -import type { FieldIdentifier } from 'features/nodes/types/field'; |
| 34 | +import { selectFieldInputInstance, selectNodesSlice } from 'features/nodes/store/selectors'; |
| 35 | +import { type FieldIdentifier, isImageFieldCollectionInputInstance } from 'features/nodes/types/field'; |
34 | 36 | import { upscaleInitialImageChanged } from 'features/parameters/store/upscaleSlice'; |
35 | 37 | import { getOptimalDimension } from 'features/parameters/util/optimalDimension'; |
| 38 | +import { uniqBy } from 'lodash-es'; |
36 | 39 | import { imagesApi } from 'services/api/endpoints/images'; |
37 | 40 | import type { ImageDTO } from 'services/api/types'; |
38 | 41 | import type { Equals } from 'tsafe'; |
39 | 42 | import { assert } from 'tsafe'; |
40 | 43 |
|
| 44 | +const log = logger('system'); |
| 45 | + |
41 | 46 | export const setGlobalReferenceImage = (arg: { |
42 | 47 | imageDTO: ImageDTO; |
43 | 48 | entityIdentifier: CanvasEntityIdentifier<'reference_image'>; |
@@ -75,9 +80,24 @@ export const addImagesToNodeImageFieldCollectionAction = (arg: { |
75 | 80 | imageDTOs: ImageDTO[]; |
76 | 81 | fieldIdentifer: FieldIdentifier; |
77 | 82 | dispatch: AppDispatch; |
| 83 | + getState: () => RootState; |
78 | 84 | }) => { |
79 | | - const { imageDTOs, fieldIdentifer, dispatch } = arg; |
80 | | - dispatch(fieldImageCollectionValueChanged({ ...fieldIdentifer, value: imageDTOs })); |
| 85 | + const { imageDTOs, fieldIdentifer, dispatch, getState } = arg; |
| 86 | + const fieldInputInstance = selectFieldInputInstance( |
| 87 | + selectNodesSlice(getState()), |
| 88 | + fieldIdentifer.nodeId, |
| 89 | + fieldIdentifer.fieldName |
| 90 | + ); |
| 91 | + |
| 92 | + if (!isImageFieldCollectionInputInstance(fieldInputInstance)) { |
| 93 | + log.warn({ fieldIdentifer }, 'Attempted to add images to a non-image field collection'); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + const images = fieldInputInstance.value ? [...fieldInputInstance.value] : []; |
| 98 | + images.push(...imageDTOs.map(({ image_name }) => ({ image_name }))); |
| 99 | + const uniqueImages = uniqBy(images, 'image_name'); |
| 100 | + dispatch(fieldImageCollectionValueChanged({ ...fieldIdentifer, value: uniqueImages })); |
81 | 101 | }; |
82 | 102 |
|
83 | 103 | export const setComparisonImage = (arg: { imageDTO: ImageDTO; dispatch: AppDispatch }) => { |
|
0 commit comments