30 lines
803 B
TypeScript
30 lines
803 B
TypeScript
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<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
|
|
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]
|
|
);
|
|
}
|