返回 presentation-ai
useDebouncedOnChange.ts
根目录 / src / components / notebook / presentation / editor / hooks / useDebouncedOnChange.ts
1 "use client";
2
3 import debounce from "lodash.debounce";
4 import { useEffect, useMemo } from "react";
5
6 interface UseDebouncedOnChangeArgs<Value> {
7 isGenerating: boolean;
8 onApply: (value: Value) => void;
9 onFontsRefresh?: () => void;
10 }
11
12 export function useDebouncedOnChange<Value>({
13 isGenerating,
14 onApply,
15 onFontsRefresh,
16 }: UseDebouncedOnChangeArgs<Value>) {
17 const debounced = useMemo(
18 () =>
19 debounce(
20 (value: Value) => {
21 if (isGenerating) return;
22 onFontsRefresh?.();
23 onApply(value);
24 },
25 100,
26 { maxWait: 200 },
27 ),
28 [isGenerating, onApply, onFontsRefresh],
29 );
30
31 useEffect(() => () => debounced.cancel(), [debounced]);
32
33 return debounced;
34 }
35
35 lines TYPESCRIPT