@@ -16,16 +16,22 @@ import { firefoxDndFix } from 'features/dnd/util';
16
16
import { useImageContextMenu } from 'features/gallery/components/ImageContextMenu/ImageContextMenu' ;
17
17
import { GalleryImageHoverIcons } from 'features/gallery/components/ImageGrid/GalleryImageHoverIcons' ;
18
18
import { getGalleryImageDataTestId } from 'features/gallery/components/ImageGrid/getGalleryImageDataTestId' ;
19
- import { selectListImageNamesQueryArgs } from 'features/gallery/store/gallerySelectors' ;
19
+ import {
20
+ selectListImageNamesQueryArgs ,
21
+ selectSelectedBoardId ,
22
+ selectSelection ,
23
+ } from 'features/gallery/store/gallerySelectors' ;
20
24
import { imageToCompareChanged , selectGallerySlice , selectionChanged } from 'features/gallery/store/gallerySlice' ;
21
25
import { useAutoLayoutContext } from 'features/ui/layouts/auto-layout-context' ;
22
26
import { VIEWER_PANEL_ID } from 'features/ui/layouts/shared' ;
23
27
import type { MouseEvent , MouseEventHandler } from 'react' ;
24
- import { memo , useCallback , useEffect , useId , useMemo , useRef , useState } from 'react' ;
28
+ import { memo , useCallback , useEffect , useMemo , useState } from 'react' ;
25
29
import { PiImageBold } from 'react-icons/pi' ;
26
30
import { imagesApi } from 'services/api/endpoints/images' ;
27
31
import type { ImageDTO } from 'services/api/types' ;
28
32
33
+ const GALLERY_IMAGE_CLASS = 'gallery-image' ;
34
+
29
35
const galleryImageContainerSX = {
30
36
containerType : 'inline-size' ,
31
37
w : 'full' ,
@@ -38,7 +44,7 @@ const galleryImageContainerSX = {
38
44
'&[data-is-dragging=true]' : {
39
45
opacity : 0.3 ,
40
46
} ,
41
- '.gallery-image' : {
47
+ [ `. ${ GALLERY_IMAGE_CLASS } ` ] : {
42
48
touchAction : 'none' ,
43
49
userSelect : 'none' ,
44
50
webkitUserSelect : 'none' ,
@@ -139,8 +145,8 @@ export const GalleryImage = memo(({ imageDTO }: Props) => {
139
145
const [ dragPreviewState , setDragPreviewState ] = useState <
140
146
DndDragPreviewSingleImageState | DndDragPreviewMultipleImageState | null
141
147
> ( null ) ;
142
- const ref = useRef < HTMLImageElement > ( null ) ;
143
- const dndId = useId ( ) ;
148
+ // Must use callback ref - else chakra's Image fallback prop will break the ref & dnd
149
+ const [ element , ref ] = useState < HTMLImageElement | null > ( null ) ;
144
150
const selectIsSelectedForCompare = useMemo (
145
151
( ) => createSelector ( selectGallerySlice , ( gallery ) => gallery . imageToCompare === imageDTO . image_name ) ,
146
152
[ imageDTO . image_name ]
@@ -153,7 +159,6 @@ export const GalleryImage = memo(({ imageDTO }: Props) => {
153
159
const isSelected = useAppSelector ( selectIsSelected ) ;
154
160
155
161
useEffect ( ( ) => {
156
- const element = ref . current ;
157
162
if ( ! element ) {
158
163
return ;
159
164
}
@@ -162,16 +167,14 @@ export const GalleryImage = memo(({ imageDTO }: Props) => {
162
167
draggable ( {
163
168
element,
164
169
getInitialData : ( ) => {
165
- const { gallery } = store . getState ( ) ;
170
+ const selection = selectSelection ( store . getState ( ) ) ;
171
+ const boardId = selectSelectedBoardId ( store . getState ( ) ) ;
166
172
// When we have multiple images selected, and the dragged image is part of the selection, initiate a
167
173
// multi-image drag.
168
- if (
169
- gallery . selection . length > 1 &&
170
- gallery . selection . find ( ( image_name ) => image_name === imageDTO . image_name ) !== undefined
171
- ) {
174
+ if ( selection . length > 1 && selection . includes ( imageDTO . image_name ) ) {
172
175
return multipleImageDndSource . getData ( {
173
- image_names : gallery . selection ,
174
- board_id : gallery . selectedBoardId ,
176
+ image_names : selection ,
177
+ board_id : boardId ,
175
178
} ) ;
176
179
}
177
180
@@ -221,7 +224,7 @@ export const GalleryImage = memo(({ imageDTO }: Props) => {
221
224
} ,
222
225
} )
223
226
) ;
224
- } , [ imageDTO , store , dndId ] ) ;
227
+ } , [ element , imageDTO , store ] ) ;
225
228
226
229
const [ isHovered , setIsHovered ] = useState ( false ) ;
227
230
@@ -242,14 +245,14 @@ export const GalleryImage = memo(({ imageDTO }: Props) => {
242
245
243
246
const dataTestId = useMemo ( ( ) => getGalleryImageDataTestId ( imageDTO . image_name ) , [ imageDTO . image_name ] ) ;
244
247
245
- useImageContextMenu ( imageDTO , ref ) ;
248
+ useImageContextMenu ( imageDTO , element ) ;
246
249
247
250
return (
248
251
< >
249
252
< Box sx = { galleryImageContainerSX } data-testid = { dataTestId } data-is-dragging = { isDragging } >
250
253
< Flex
251
254
role = "button"
252
- className = "gallery-image"
255
+ className = { GALLERY_IMAGE_CLASS }
253
256
onMouseOver = { onMouseOver }
254
257
onMouseOut = { onMouseOut }
255
258
onClick = { onClick }
0 commit comments