Skip to content

Commit a8bad8e

Browse files
committed
app-catalog: Debounce search input by 300ms
Signed-off-by: Evangelos Skopelitis <[email protected]>
1 parent 3e3ff37 commit a8bad8e

File tree

1 file changed

+22
-3
lines changed
  • app-catalog/src/components/charts

1 file changed

+22
-3
lines changed

app-catalog/src/components/charts/List.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
Typography,
2020
} from '@mui/material';
2121
import { Autocomplete, Pagination } from '@mui/material';
22-
import { useEffect, useState } from 'react';
22+
import { useEffect, useRef, useState } from 'react';
2323
//import { jsonToYAML, yamlToJSON } from '../../helpers';
2424
import { fetchChartsFromArtifact } from '../../api/charts';
2525
//import { createRelease } from '../../api/releases';
@@ -47,6 +47,25 @@ function Search({
4747
search,
4848
setSearch
4949
}: SearchProps) {
50+
const [inputValue, setInputValue] = useState(search);
51+
const timeoutRef = useRef<NodeJS.Timeout>();
52+
53+
useEffect(() => {
54+
if (timeoutRef.current) {
55+
clearTimeout(timeoutRef.current);
56+
}
57+
58+
timeoutRef.current = setTimeout(() => {
59+
setSearch(inputValue);
60+
}, 300);
61+
62+
return () => {
63+
if (timeoutRef.current) {
64+
clearTimeout(timeoutRef.current);
65+
}
66+
};
67+
}, [inputValue, setSearch]);
68+
5069
return (
5170
<TextField
5271
sx={{
@@ -55,9 +74,9 @@ function Search({
5574
}}
5675
id="outlined-basic"
5776
label="Search"
58-
value={search}
77+
value={inputValue}
5978
onChange={event => {
60-
setSearch(event.target.value);
79+
setInputValue(event.target.value);
6180
}}
6281
/>
6382
);

0 commit comments

Comments
 (0)