Skip to content

Commit c1ab839

Browse files
authored
Add debounce to filter text on process list page (#8)
* install use-debounce * use debounce on filter text
1 parent 06ca67c commit c1ab839

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

package-lock.json

+13-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"next": "latest",
1919
"react": "latest",
2020
"react-chartjs-2": "^5.2.0",
21-
"react-dom": "latest"
21+
"react-dom": "latest",
22+
"use-debounce": "^9.0.4"
2223
},
2324
"devDependencies": {
2425
"@types/node": "latest",

src/app/process-list/hooks.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import { SortDescriptor } from "@nextui-org/react";
1010
import { invoke } from "@tauri-apps/api/tauri";
1111
import { useEffect, useState } from "react";
12+
import { useDebounce } from "use-debounce";
1213

1314
type FetchProcessesParams = {
1415
sortDescriptor: SortDescriptor,
@@ -20,6 +21,8 @@ export const useProcesses = () => {
2021
const [ filterText, setFilterText ] = useState<string>("");
2122
const [ sortDescriptor, setSortDescriptor ] = useState<SortDescriptor>({ column: "memoryUsage", direction: "descending" });
2223

24+
const [ filterTextVal ] = useDebounce(filterText, 1000);
25+
2326
const fetchProcesses = async ({ filterText, sortDescriptor }: FetchProcessesParams) => {
2427
console.log("Fetcing processes...");
2528
const processes: Process[] = await invoke('get_processes', { sortDescriptor, filterText });
@@ -34,12 +37,12 @@ export const useProcesses = () => {
3437
useEffect(() => {
3538
const interval = setInterval(() => {
3639
console.log("Fetching processes...");
37-
fetchProcesses({ sortDescriptor, filterText: filterText.trim().length === 0 ? null : filterText })
40+
fetchProcesses({ sortDescriptor, filterText: filterTextVal.trim().length === 0 ? null : filterTextVal })
3841
.then(setProcesses);
3942
}, 1000);
4043

4144
return () => clearInterval(interval)
42-
}, [filterText, sortDescriptor]);
45+
}, [filterTextVal, sortDescriptor]);
4346

44-
return { processes, filterText, setFilterText, sortDescriptor, setSortDescriptor, killProcess };
47+
return { processes, filterText: filterTextVal, setFilterText, sortDescriptor, setSortDescriptor, killProcess };
4548
}

0 commit comments

Comments
 (0)