Skip to content

Commit f099181

Browse files
committed
Add debounce
1 parent cf632fc commit f099181

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

frontend/src/app/(auth)/leetcode-dashboard/LeetcodeDashboardTable.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import { MultiSelect } from "@/components/ui/multiselect";
4040
import { capitalizeWords } from "@/utils/string_utils";
4141
import { topicsList } from "@/utils/constants";
4242

43+
const SEARCH_DEBOUNCE_TIMEOUT = 300;
44+
4345
const Cell = ({
4446
className,
4547
children,
@@ -154,6 +156,16 @@ const ActionsCell: React.FC<ActionCellProps> = ({
154156
);
155157
};
156158

159+
function debounce<T extends (...args: any[]) => void>(func: T, timeout = 300) {
160+
let timer: NodeJS.Timeout | undefined;
161+
return (...args: Parameters<T>) => {
162+
if (timer) clearTimeout(timer);
163+
timer = setTimeout(() => {
164+
func(...args);
165+
}, timeout);
166+
};
167+
}
168+
157169
export function LeetcodeDashboardTable({
158170
refreshKey,
159171
setRefreshKey,
@@ -216,6 +228,10 @@ export function LeetcodeDashboardTable({
216228
const [isFilterOpen, setIsFilterOpen] = React.useState<boolean>(false);
217229
const [isLoading, setIsLoading] = React.useState<boolean>(false);
218230

231+
const debouncedSetSearchTitle = debounce((title: string) => {
232+
setSearchTitle(title);
233+
}, SEARCH_DEBOUNCE_TIMEOUT);
234+
219235
const questionDifficulty = Object.values(QuestionDifficulty).map((q1) => {
220236
return {
221237
label: capitalizeWords(q1),
@@ -265,7 +281,6 @@ export function LeetcodeDashboardTable({
265281
];
266282

267283
useEffect(() => {
268-
setIsLoading(true);
269284
getLeetcodeDashboardData(
270285
pagination.pageIndex + 1,
271286
pagination.pageSize,
@@ -327,7 +342,9 @@ export function LeetcodeDashboardTable({
327342
<Input
328343
className="w-[16rem] pl-10 !placeholder-primary-400"
329344
placeholder="Search Question Name"
330-
onChange={(e) => setSearchTitle(e.target.value)}
345+
onChange={(e) => {
346+
debouncedSetSearchTitle(e.target.value);
347+
}}
331348
/>
332349
</div>
333350
<div className="relative">
@@ -390,7 +407,7 @@ export function LeetcodeDashboardTable({
390407
))}
391408
</TableHeader>
392409
<TableBody className="bg-primary-900 text-primary-300 text-xs">
393-
{table.getRowModel().rows?.length ? (
410+
{table.getRowModel().rows?.length && !isLoading ? (
394411
table.getRowModel().rows.map((row) => (
395412
<TableRow
396413
key={row.id}

0 commit comments

Comments
 (0)