Files
triggerdotdev--trigger.dev/apps/webapp/app/hooks/useDebounce.ts
Matt Aitken a90b73c7ca Filter runs by queue, machine, version (#2277)
* Queue in run table and filtering

* Debounce the filter changes

* Remove console log

* Added machine filtering

* Added version filtering

* Filter by version in the db

* Removed duplicate classes

* Version filtering hasFilters consistency

* Added queues and machines to the bulk action summary

* runs.list filtering for queue and machine

* Fix for machine errors
2025-07-21 16:21:46 +01:00

44 lines
1.2 KiB
TypeScript

import { useEffect, useRef } from "react";
/**
* A function that you call with a debounce delay, the function will only be called after the delay has passed
*
* @param fn The function to debounce
* @param delay In ms
*/
export function useDebounce<T extends (...args: any[]) => any>(fn: T, delay: number) {
const timeout = useRef<ReturnType<typeof setTimeout>>();
return (...args: Parameters<T>) => {
if (timeout.current) {
clearTimeout(timeout.current);
}
timeout.current = setTimeout(() => {
fn(...args);
}, delay);
};
}
/**
* A function that takes in a value, function, and delay.
* It will run the function with the debounced value, only if the value has changed.
* It should deal with the function being passed in not being a useCallback
*/
export function useDebounceEffect<T>(value: T, fn: (value: T) => void, delay: number) {
const fnRef = useRef(fn);
// Update the ref whenever the function changes
fnRef.current = fn;
useEffect(() => {
const timeout = setTimeout(() => {
fnRef.current(value);
}, delay);
return () => {
clearTimeout(timeout);
};
}, [value, delay]); // Only depend on value and delay, not fn
}