11import { useAppDispatch , useAppSelector } from 'app/store/storeHooks' ;
22import { getGalleryImageDataTestId } from 'features/gallery/components/ImageGrid/getGalleryImageDataTestId' ;
3- import { GALLERY_IMAGE_PADDING_PX } from 'features/gallery/components/ImageGrid/ImageGridItemContainer' ;
3+ import { imageItemContainerTestId } from 'features/gallery/components/ImageGrid/ImageGridItemContainer' ;
4+ import { imageListContainerTestId } from 'features/gallery/components/ImageGrid/ImageGridListContainer' ;
45import { virtuosoGridRefs } from 'features/gallery/components/ImageGrid/types' ;
56import { useGalleryImages } from 'features/gallery/hooks/useGalleryImages' ;
67import { selectLastSelectedImage } from 'features/gallery/store/gallerySelectors' ;
@@ -27,23 +28,17 @@ import { imagesSelectors } from 'services/api/util';
2728 * Gets the number of images per row in the gallery by grabbing their DOM elements.
2829 */
2930const getImagesPerRow = ( ) : number => {
30- const imageRect = Object . values (
31- document . getElementsByClassName ( 'gallerygrid-image' )
32- ) [ 0 ] ?. getBoundingClientRect ( ) ;
31+ const widthOfGalleryImage =
32+ document
33+ . querySelector ( `[data-testid="${ imageItemContainerTestId } "]` )
34+ ?. getBoundingClientRect ( ) . width ?? 1 ;
3335
34- // We have to manually take into account the padding of the image container, else
35- // imagesPerRow will be wrong when the gallery is large or images are very small.
36- const widthOfGalleryImage = imageRect
37- ? imageRect . width + GALLERY_IMAGE_PADDING_PX * 2
38- : 0 ;
36+ const widthOfGalleryGrid =
37+ document
38+ . querySelector ( `[data-testid="${ imageListContainerTestId } "]` )
39+ ?. getBoundingClientRect ( ) . width ?? 0 ;
3940
40- const galleryGridRect = document
41- . getElementById ( 'gallery-grid' )
42- ?. getBoundingClientRect ( ) ;
43-
44- const widthOfGalleryGrid = galleryGridRect ?. width ?? 0 ;
45-
46- const imagesPerRow = Math . floor ( widthOfGalleryGrid / widthOfGalleryImage ) ;
41+ const imagesPerRow = Math . round ( widthOfGalleryGrid / widthOfGalleryImage ) ;
4742
4843 return imagesPerRow ;
4944} ;
@@ -104,11 +99,11 @@ const getUpImage = (images: ImageDTO[], currentIndex: number) => {
10499
105100const getDownImage = ( images : ImageDTO [ ] , currentIndex : number ) => {
106101 const imagesPerRow = getImagesPerRow ( ) ;
107- // If we are on the first row , we want to stay on the first row, not go to last image
108- const isOnLastRow = currentIndex >= images . length - imagesPerRow ;
109- const index = isOnLastRow
110- ? currentIndex
111- : clamp ( currentIndex + imagesPerRow , 0 , images . length - 1 ) ;
102+ // If there are no images below the current image , we want to stay where we are
103+ const areImagesBelow = currentIndex < images . length - imagesPerRow ;
104+ const index = areImagesBelow
105+ ? clamp ( currentIndex + imagesPerRow , 0 , images . length - 1 )
106+ : currentIndex ;
112107 const image = images [ index ] ;
113108 return { index, image } ;
114109} ;
@@ -164,7 +159,7 @@ export const useGalleryNavigation = (): UseGalleryNavigationReturn => {
164159 imagesSelectors . selectAll ( data ) ,
165160 lastSelectedImageIndex
166161 ) ;
167- if ( ! image ) {
162+ if ( ! image || index === lastSelectedImageIndex ) {
168163 return ;
169164 }
170165 dispatch ( imageSelected ( image ) ) ;
0 commit comments