Search files as the user types
The Files page and the conversation's files modal searched only on Enter. Both now search by name or path a moment after the last keystroke (useDebouncedValue, AppData.SearchDebounceMs), clearing shows the whole list at once, and the modal drops an answer a newer request overtook. The Files search box no longer locks while a folder loads. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
|
||||
type Params<T> = {
|
||||
value: T;
|
||||
delay_ms?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* `value` once it has stopped changing for `delay_ms`
|
||||
* (`AppData.SearchDebounceMs` by default), so a search box searches as the
|
||||
* user types without a request per keystroke. An emptied value comes
|
||||
* through at once.
|
||||
*/
|
||||
export default function useDebouncedValue<T>({ value, delay_ms = AppData.SearchDebounceMs }: Params<T>) {
|
||||
const [debounced, setDebounced] = useState(value);
|
||||
|
||||
useEffect(() => {
|
||||
if (!value) {
|
||||
setDebounced(value);
|
||||
return;
|
||||
}
|
||||
|
||||
const timer = setTimeout(() => setDebounced(value), delay_ms);
|
||||
|
||||
return () => clearTimeout(timer);
|
||||
}, [value, delay_ms]);
|
||||
|
||||
return debounced;
|
||||
}
|
||||
Reference in New Issue
Block a user