's'
This commit is contained in:
29
app/hooks/useXhClickOpener.ts
Normal file
29
app/hooks/useXhClickOpener.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
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>>();
|
||||
|
||||
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]
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user