'use client';
import { useEffect, useState } from 'react';

/**
 * 防抖 hook：value 变化后延迟 delay 毫秒才更新返回值
 * 用于搜索框：用户输入时不立即过滤，避免 100+ 用户同时输入造成的卡顿
 */
export function useDebouncedValue<T>(value: T, delay = 300): T {
  const [debounced, setDebounced] = useState(value);
  useEffect(() => {
    const t = setTimeout(() => setDebounced(value), delay);
    return () => clearTimeout(t);
  }, [value, delay]);
  return debounced;
}
