Skip to content

Commit 895c1c4

Browse files
prithvidasguptashuashuai
authored andcommitted
fix: add 400 second tag search debounce
1 parent 9fa6ef6 commit 895c1c4

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

ui/src/components/TagSelector/index.tsx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
*/
1919

2020
/* eslint-disable no-nested-ternary */
21-
import { FC, useState, useEffect, useRef } from 'react';
21+
import { FC, useState, useEffect, useRef, useCallback } from 'react';
2222
import { Dropdown, Button, Form } from 'react-bootstrap';
2323
import { useTranslation } from 'react-i18next';
2424

25+
import debounce from 'lodash/debounce';
2526
import { marked } from 'marked';
2627
import classNames from 'classnames';
2728

@@ -69,7 +70,7 @@ const TagSelector: FC<IProps> = ({
6970
const [currentIndex, setCurrentIndex] = useState<number>(0);
7071
const [repeatIndex, setRepeatIndex] = useState(-1);
7172
const [searchValue, setSearchValue] = useState<string>('');
72-
const [tags, setTags] = useState<Type.Tag[] | null>(null);
73+
const [tags, setTags] = useState<Type.Tag[] | null>([]);
7374
const [requiredTags, setRequiredTags] = useState<Type.Tag[] | null>(null);
7475
const { t } = useTranslation('translation', { keyPrefix: 'tag_selector' });
7576
const { data: userPermission } = useUserPermission('tag.add');
@@ -146,20 +147,23 @@ const TagSelector: FC<IProps> = ({
146147
handleMenuShow(false);
147148
};
148149

149-
const fetchTags = (str) => {
150-
if (!showRequiredTag && !str) {
151-
setTags([]);
152-
return;
153-
}
154-
queryTags(str).then((res) => {
155-
const tagArray: Type.Tag[] = filterTags(res || []);
156-
if (str === '') {
157-
setRequiredTags(res);
150+
const fetchTags = useCallback(
151+
debounce((str) => {
152+
if (!showRequiredTag && !str) {
153+
setTags([]);
154+
return;
158155
}
159-
handleMenuShow(tagArray.length > 0);
160-
setTags(tagArray);
161-
});
162-
};
156+
queryTags(str).then((res) => {
157+
const tagArray: Type.Tag[] = filterTags(res || []);
158+
if (str === '') {
159+
setRequiredTags(res);
160+
}
161+
handleMenuShow(tagArray.length > 0);
162+
setTags(tagArray);
163+
});
164+
}, 400),
165+
[],
166+
);
163167

164168
const resetSearch = () => {
165169
setCurrentIndex(0);

0 commit comments

Comments
 (0)