@@ -23,8 +23,31 @@ export function useInfiniteScroll({
2323 const stateRef = useRef ( { allImages, displayedImages, hasMore } ) ;
2424 stateRef . current = { allImages, displayedImages, hasMore } ;
2525
26+ // Track document height to detect layout transitions
27+ const lastDocHeightRef = useRef ( 0 ) ;
28+ const layoutTransitionRef = useRef ( false ) ;
29+ const loadBlockedRef = useRef ( false ) ;
30+
31+ // Block loading and reset tracking when data changes (new filter results)
32+ useEffect ( ( ) => {
33+ // Block loading to prevent race conditions during filter change
34+ loadBlockedRef . current = true ;
35+ lastDocHeightRef . current = 0 ;
36+ layoutTransitionRef . current = false ;
37+
38+ // Unblock after state has settled
39+ const timer = setTimeout ( ( ) => {
40+ loadBlockedRef . current = false ;
41+ } , 150 ) ;
42+
43+ return ( ) => clearTimeout ( timer ) ;
44+ } , [ allImages ] ) ;
45+
2646 // Load more images
2747 const loadMore = useCallback ( ( ) => {
48+ // Skip if loading is blocked (filter change in progress)
49+ if ( loadBlockedRef . current ) return ;
50+
2851 const { allImages : all , displayedImages : displayed , hasMore : more } = stateRef . current ;
2952 if ( ! more ) return ;
3053
@@ -41,12 +64,29 @@ export function useInfiniteScroll({
4164
4265 // Check if we need to load more based on scroll position
4366 const checkAndLoad = useCallback ( ( ) => {
67+ // Skip if loading is blocked (filter change in progress)
68+ if ( loadBlockedRef . current ) return ;
69+
4470 const { hasMore : more } = stateRef . current ;
4571 if ( ! more ) return ;
4672
4773 const scrollBottom = window . scrollY + window . innerHeight ;
4874 const docHeight = document . documentElement . scrollHeight ;
4975
76+ // Detect layout transitions (significant height changes)
77+ const heightDiff = Math . abs ( docHeight - lastDocHeightRef . current ) ;
78+ if ( heightDiff > 500 && lastDocHeightRef . current > 0 ) {
79+ // Layout is transitioning (e.g., compact/normal switch), pause loading
80+ layoutTransitionRef . current = true ;
81+ setTimeout ( ( ) => {
82+ layoutTransitionRef . current = false ;
83+ } , 300 ) ;
84+ }
85+ lastDocHeightRef . current = docHeight ;
86+
87+ // Skip loading during layout transitions
88+ if ( layoutTransitionRef . current ) return ;
89+
5090 // If within 2500px of bottom, load more
5191 if ( scrollBottom + 2500 > docHeight ) {
5292 loadMore ( ) ;
0 commit comments