11import { useAppStore } from 'app/store/storeHooks' ;
2- import { useCallback } from 'react' ;
2+ import { useCallback , useEffect , useRef } from 'react' ;
33import type { ListRange } from 'react-virtuoso' ;
44import { imagesApi , useGetImageDTOsByNamesMutation } from 'services/api/endpoints/images' ;
55import { useThrottledCallback } from 'use-debounce' ;
@@ -53,6 +53,7 @@ export const useRangeBasedImageFetching = ({
5353} : UseRangeBasedImageFetchingArgs ) : UseRangeBasedImageFetchingReturn => {
5454 const store = useAppStore ( ) ;
5555 const [ getImageDTOsByNames ] = useGetImageDTOsByNamesMutation ( ) ;
56+ const lastRangeRef = useRef < ListRange | null > ( null ) ;
5657
5758 const fetchImages = useCallback (
5859 ( visibleRange : ListRange ) => {
@@ -65,6 +66,7 @@ export const useRangeBasedImageFetching = ({
6566 return ;
6667 }
6768 getImageDTOsByNames ( { image_names : uncachedNames } ) ;
69+ lastRangeRef . current = visibleRange ;
6870 } ,
6971 [ enabled , getImageDTOsByNames , imageNames , store ]
7072 ) ;
@@ -78,6 +80,13 @@ export const useRangeBasedImageFetching = ({
7880 [ throttledFetchImages ]
7981 ) ;
8082
83+ useEffect ( ( ) => {
84+ if ( ! lastRangeRef . current ) {
85+ return ;
86+ }
87+ throttledFetchImages ( lastRangeRef . current ) ;
88+ } , [ imageNames , throttledFetchImages ] ) ;
89+
8190 return {
8291 onRangeChanged,
8392 } ;
0 commit comments