Files
cloud-host/frontend/src/hooks/useDebounce.ts
T
keyhan cb9320ee15 refactor: add useDebounce hook and replace manual debounce in admin search
- Create reusable useDebounce<T> hook in frontend/src/hooks/useDebounce.ts
- Replace manual setTimeout/clearTimeout + extra state in AdminAppsPage
  with clean useDebounce(search, 400) pattern
- Removes timer state and handleSearch wrapper function
2026-04-08 12:40:25 +03:30

29 lines
832 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use client';
import { useState, useEffect } from 'react';
/**
* Debounce a value by a given delay.
*
* @param value The raw (fast-changing) value, e.g. a search input string.
* @param delay Debounce delay in milliseconds (default 400ms).
* @returns The debounced value only updates after the user stops
* changing `value` for `delay` ms.
*
* @example
* const [search, setSearch] = useState('');
* const debouncedSearch = useDebounce(search, 400);
*
* // use `debouncedSearch` in your query key / API call
*/
export function useDebounce<T>(value: T, delay = 400): T {
const [debounced, setDebounced] = useState<T>(value);
useEffect(() => {
const timer = setTimeout(() => setDebounced(value), delay);
return () => clearTimeout(timer);
}, [value, delay]);
return debounced;
}