|
| 1 | +/** |
| 2 | + * Custom hook for managing sophisticated list and grid animations |
| 3 | + * Provides optimized animation controls for data-driven components |
| 4 | + * Performance optimized with reduced re-renders and debounced updates |
| 5 | + */ |
| 6 | + |
| 7 | +import { useAnimation } from "framer-motion"; |
| 8 | +import { useEffect, useRef, useState, useCallback, useMemo } from "react"; |
| 9 | + |
| 10 | +export const useListAnimations = ({ |
| 11 | + items = [], |
| 12 | + loading = false, |
| 13 | + error = null, |
| 14 | + animateOnDataChange = true, |
| 15 | +}) => { |
| 16 | + const controls = useAnimation(); |
| 17 | + const containerRef = useRef(null); |
| 18 | + const [isAnimating, setIsAnimating] = useState(false); |
| 19 | + const previousItemsLength = useRef(items.length); |
| 20 | + const animationTimeoutRef = useRef(null); |
| 21 | + |
| 22 | + // Debounced animation trigger to prevent excessive animations |
| 23 | + const triggerAnimation = useCallback( |
| 24 | + (variant) => { |
| 25 | + if (animationTimeoutRef.current) { |
| 26 | + clearTimeout(animationTimeoutRef.current); |
| 27 | + } |
| 28 | + |
| 29 | + animationTimeoutRef.current = setTimeout(() => { |
| 30 | + setIsAnimating(true); |
| 31 | + controls.start(variant).then(() => { |
| 32 | + setIsAnimating(false); |
| 33 | + }); |
| 34 | + }, 50); // Small debounce |
| 35 | + }, |
| 36 | + [controls] |
| 37 | + ); |
| 38 | + |
| 39 | + // Optimized data change detection |
| 40 | + const hasSignificantChange = useMemo(() => { |
| 41 | + const lengthDiff = Math.abs(items.length - previousItemsLength.current); |
| 42 | + return lengthDiff > 0 && lengthDiff < 10; // Only animate for reasonable changes |
| 43 | + }, [items.length]); |
| 44 | + |
| 45 | + // Animate when data changes - optimized |
| 46 | + useEffect(() => { |
| 47 | + if (!animateOnDataChange || !hasSignificantChange) return; |
| 48 | + |
| 49 | + if (!loading && !error) { |
| 50 | + triggerAnimation("visible"); |
| 51 | + } |
| 52 | + |
| 53 | + previousItemsLength.current = items.length; |
| 54 | + }, [ |
| 55 | + hasSignificantChange, |
| 56 | + loading, |
| 57 | + error, |
| 58 | + triggerAnimation, |
| 59 | + animateOnDataChange, |
| 60 | + ]); |
| 61 | + |
| 62 | + // Handle loading state - simplified |
| 63 | + useEffect(() => { |
| 64 | + if (loading) { |
| 65 | + setIsAnimating(true); |
| 66 | + } else if (!error && items.length > 0) { |
| 67 | + triggerAnimation("visible"); |
| 68 | + } |
| 69 | + }, [loading, error, items.length, triggerAnimation]); |
| 70 | + |
| 71 | + // Cleanup timeouts |
| 72 | + useEffect(() => { |
| 73 | + return () => { |
| 74 | + if (animationTimeoutRef.current) { |
| 75 | + clearTimeout(animationTimeoutRef.current); |
| 76 | + } |
| 77 | + }; |
| 78 | + }, []); |
| 79 | + |
| 80 | + // Optimized scroll to top |
| 81 | + const scrollToTop = useCallback((smooth = true) => { |
| 82 | + if (containerRef.current) { |
| 83 | + const behavior = smooth ? "smooth" : "auto"; |
| 84 | + containerRef.current.scrollIntoView({ |
| 85 | + behavior, |
| 86 | + block: "start", |
| 87 | + }); |
| 88 | + } |
| 89 | + }, []); |
| 90 | + |
| 91 | + // Simplified refresh without complex animations |
| 92 | + const triggerRefresh = useCallback(() => { |
| 93 | + triggerAnimation("visible"); |
| 94 | + }, [triggerAnimation]); |
| 95 | + |
| 96 | + return { |
| 97 | + controls, |
| 98 | + containerRef, |
| 99 | + isAnimating, |
| 100 | + scrollToTop, |
| 101 | + triggerRefresh, |
| 102 | + |
| 103 | + // Animation states - memoized for performance |
| 104 | + shouldShowItems: !loading && !error && items.length > 0, |
| 105 | + shouldShowLoading: loading, |
| 106 | + shouldShowEmpty: !loading && !error && items.length === 0, |
| 107 | + shouldShowError: error && !loading, |
| 108 | + }; |
| 109 | +}; |
| 110 | + |
| 111 | +// Simplified pagination animations |
| 112 | +export const usePaginationAnimations = (currentPage, totalPages) => { |
| 113 | + const controls = useAnimation(); |
| 114 | + const [isPageChanging, setIsPageChanging] = useState(false); |
| 115 | + const previousPage = useRef(currentPage); |
| 116 | + |
| 117 | + useEffect(() => { |
| 118 | + if (currentPage !== previousPage.current && currentPage > 0) { |
| 119 | + setIsPageChanging(true); |
| 120 | + |
| 121 | + // Simple fade animation |
| 122 | + controls.start("visible").then(() => { |
| 123 | + setIsPageChanging(false); |
| 124 | + }); |
| 125 | + } |
| 126 | + previousPage.current = currentPage; |
| 127 | + }, [currentPage, controls]); |
| 128 | + |
| 129 | + return { |
| 130 | + controls, |
| 131 | + isPageChanging, |
| 132 | + shouldAnimate: totalPages > 1, |
| 133 | + }; |
| 134 | +}; |
| 135 | + |
| 136 | +// Simplified search animations |
| 137 | +export const useSearchAnimations = (searchQuery, hasResults) => { |
| 138 | + const controls = useAnimation(); |
| 139 | + const [isSearching, setIsSearching] = useState(false); |
| 140 | + const searchTimeoutRef = useRef(null); |
| 141 | + |
| 142 | + const triggerSearchAnimation = useCallback(() => { |
| 143 | + if (searchTimeoutRef.current) { |
| 144 | + clearTimeout(searchTimeoutRef.current); |
| 145 | + } |
| 146 | + |
| 147 | + setIsSearching(true); |
| 148 | + searchTimeoutRef.current = setTimeout(() => { |
| 149 | + controls.start("visible").then(() => { |
| 150 | + setIsSearching(false); |
| 151 | + }); |
| 152 | + }, 100); |
| 153 | + }, [controls]); |
| 154 | + |
| 155 | + useEffect(() => { |
| 156 | + if (searchQuery) { |
| 157 | + triggerSearchAnimation(); |
| 158 | + } |
| 159 | + }, [searchQuery, hasResults, triggerSearchAnimation]); |
| 160 | + |
| 161 | + useEffect(() => { |
| 162 | + return () => { |
| 163 | + if (searchTimeoutRef.current) { |
| 164 | + clearTimeout(searchTimeoutRef.current); |
| 165 | + } |
| 166 | + }; |
| 167 | + }, []); |
| 168 | + |
| 169 | + return { |
| 170 | + controls, |
| 171 | + isSearching, |
| 172 | + triggerSearchAnimation, |
| 173 | + }; |
| 174 | +}; |
0 commit comments