Download frontend/src/hooks/useDebouncedPredict.ts from devrup404/SignalMod: direct link, hf CLI and curl.
- Browser
- Download file 1.03 kB
-
https://huggingface.co/spaces/devrup404/SignalMod/resolve/c2dca5134d67b02cdcdc22bb21994f31996a1715/frontend/src/hooks/useDebouncedPredict.ts
- Command line
-
hf download hf://spaces/devrup404/SignalMod@c2dca5134d67b02cdcdc22bb21994f31996a1715/frontend/src/hooks/useDebouncedPredict.ts
-
curl -L -o useDebouncedPredict.ts https://huggingface.co/spaces/devrup404/SignalMod/resolve/c2dca5134d67b02cdcdc22bb21994f31996a1715/frontend/src/hooks/useDebouncedPredict.ts
1.03 kB
| import { useEffect, useRef, useState } from "react"; | |
| import { predict } from "../api/client"; | |
| import type { PredictResponse } from "../types/api"; | |
| export function useDebouncedPredict(text: string, threshold: number, delayMs = 400) { | |
| const [result, setResult] = useState<PredictResponse | null>(null); | |
| const [loading, setLoading] = useState(false); | |
| const [error, setError] = useState<string | null>(null); | |
| const abortRef = useRef<AbortController | null>(null); | |
| useEffect(() => { | |
| const trimmed = text.trim(); | |
| if (!trimmed) { | |
| setResult(null); | |
| setError(null); | |
| setLoading(false); | |
| return; | |
| } | |
| const timer = setTimeout(() => { | |
| abortRef.current?.abort(); | |
| setLoading(true); | |
| setError(null); | |
| predict(trimmed, threshold) | |
| .then(setResult) | |
| .catch((e: Error) => setError(e.message)) | |
| .finally(() => setLoading(false)); | |
| }, delayMs); | |
| return () => clearTimeout(timer); | |
| }, [text, threshold, delayMs]); | |
| return { result, loading, error }; | |
| } | |