veza/apps/web/src/hooks/useThrottledCallback.ts
senke 1a9caf7415 edge-cases: implement Edge 3.2 - handle rapid user interactions
- Created useDebouncedCallback hook for debouncing callbacks
- Created useThrottledCallback hook for throttling callbacks
- Created usePreventDoubleClick hook for preventing double-clicks
- All hooks are properly typed and documented with examples
- Components can use these hooks to prevent rapid interactions
- Existing ButtonLoading component already disables buttons during loading
2026-01-16 12:58:21 +01:00

53 lines
1.5 KiB
TypeScript

import { useCallback, useRef } from 'react';
/**
* Edge 3.2: Hook for throttling callback functions
* Limits execution to at most once per specified time period
*
* @param callback - The function to throttle
* @param delay - Minimum time between executions in milliseconds (default: 300ms)
* @returns Throttled callback function
*
* @example
* ```typescript
* const handleScroll = useThrottledCallback(() => {
* updateScrollPosition();
* }, 100);
*
* // Usage in scroll event
* window.addEventListener('scroll', handleScroll);
* ```
*/
export function useThrottledCallback<T extends (...args: any[]) => any>(
callback: T,
delay: number = 300,
): T {
const lastExecTimeRef = useRef<number>(0);
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
const throttledCallback = useCallback(
((...args: Parameters<T>) => {
const currentTime = Date.now();
const timeSinceLastExec = currentTime - lastExecTimeRef.current;
if (timeSinceLastExec >= delay) {
// Execute immediately if enough time has passed
callback(...args);
lastExecTimeRef.current = currentTime;
} else {
// Schedule execution for the remaining time
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
timeoutRef.current = setTimeout(() => {
callback(...args);
lastExecTimeRef.current = Date.now();
}, delay - timeSinceLastExec);
}
}) as T,
[callback, delay],
);
return throttledCallback;
}