aboutsummaryrefslogtreecommitdiff
path: root/apps/web/src/hooks
diff options
context:
space:
mode:
authorDhravya <[email protected]>2024-04-13 09:55:29 -0700
committerDhravya <[email protected]>2024-04-13 09:55:29 -0700
commit57e699a6ee35b161cf69aa82bec6c50f114b1055 (patch)
treea40d3c983c4d7661797e1c18591019cc09c3f083 /apps/web/src/hooks
parentmerge (diff)
parentfix edge case for getting metadata (diff)
downloadsupermemory-57e699a6ee35b161cf69aa82bec6c50f114b1055.tar.xz
supermemory-57e699a6ee35b161cf69aa82bec6c50f114b1055.zip
conflicts
Diffstat (limited to 'apps/web/src/hooks')
-rw-r--r--apps/web/src/hooks/useDebounce.ts27
1 files changed, 27 insertions, 0 deletions
diff --git a/apps/web/src/hooks/useDebounce.ts b/apps/web/src/hooks/useDebounce.ts
new file mode 100644
index 00000000..d133b1ae
--- /dev/null
+++ b/apps/web/src/hooks/useDebounce.ts
@@ -0,0 +1,27 @@
+import { useEffect, useState } from "react";
+
+/**
+ * Use this hook when you need to debounce a value.
+ * @param value
+ * @param delay in milliseconds
+ */
+export const useDebounce = <T>(value: T, delay: number) => {
+ // State and setters for debounced value
+ const [debouncedValue, setDebouncedValue] = useState<T>(value);
+
+ useEffect(() => {
+ // Update debounced value after delay
+ const handler = setTimeout(() => {
+ setDebouncedValue(value);
+ }, delay);
+
+ // Cancel the timeout if value changes (also on delay change or unmount)
+ // This is how we prevent debounced value from updating if the value is changed
+ // within the delay period. Timeout gets cleared and restarted.
+ return () => {
+ clearTimeout(handler);
+ };
+ }, [value, delay]);
+
+ return debouncedValue;
+};