import { useRef, useCallback } from "react"; const RESET_MS = 1000; const CLICK_THRESHOLD = 5; /** 连续点击 5 次打开小红书主页 */ export function useXhClickOpener(xhUrl: string | undefined) { const countRef = useRef(0); const timerRef = useRef>(); return useCallback( (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); if (!xhUrl) return; clearTimeout(timerRef.current); countRef.current++; if (countRef.current >= CLICK_THRESHOLD) { window.open(xhUrl, "_blank", "noopener,noreferrer"); countRef.current = 0; } else { timerRef.current = setTimeout(() => { countRef.current = 0; }, RESET_MS); } }, [xhUrl] ); }