import { useState, useEffect, useRef } from "react"; import { motion } from "framer-motion"; import { FadeLoader } from "react-spinners"; import { useClickAway } from "react-use"; import Icon from "@/components/Icon"; import { items } from "./items"; const Search = ({}) => { const [itemStates, setItemStates] = useState< Array<"pending" | "loading" | "active"> >(new Array(items.length).fill("pending")); const [visible, setVisible] = useState(false); const ref = useRef(null); useClickAway(ref, () => { setVisible(false); }); useEffect(() => { if (!visible) { setItemStates(new Array(items.length).fill("pending")); return; } const timeouts: NodeJS.Timeout[] = []; const animateItems = () => { items.forEach((_, index) => { const loadingTimeout = setTimeout(() => { setItemStates((prev) => { const newStates = [...prev]; newStates[index] = "loading"; return newStates; }); }, 3000 * index); const activeTimeout = setTimeout(() => { setItemStates((prev) => { const newStates = [...prev]; newStates[index] = "active"; return newStates; }); }, 3000 * index + 3000); timeouts.push(loadingTimeout, activeTimeout); }); }; animateItems(); // Cleanup function - очищаємо всі таймери при зміні visible або unmount return () => { timeouts.forEach((timeout) => clearTimeout(timeout)); }; }, [visible]); return (