Skip to content

Commit

Permalink
fix: add 400 second tag search debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
prithvidasgupta authored and shuashuai committed Oct 22, 2024
1 parent 9fa6ef6 commit 895c1c4
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions ui/src/components/TagSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
*/

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

import debounce from 'lodash/debounce';
import { marked } from 'marked';
import classNames from 'classnames';

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

const fetchTags = (str) => {
if (!showRequiredTag && !str) {
setTags([]);
return;
}
queryTags(str).then((res) => {
const tagArray: Type.Tag[] = filterTags(res || []);
if (str === '') {
setRequiredTags(res);
const fetchTags = useCallback(
debounce((str) => {
if (!showRequiredTag && !str) {
setTags([]);
return;
}
handleMenuShow(tagArray.length > 0);
setTags(tagArray);
});
};
queryTags(str).then((res) => {
const tagArray: Type.Tag[] = filterTags(res || []);
if (str === '') {
setRequiredTags(res);
}
handleMenuShow(tagArray.length > 0);
setTags(tagArray);
});
}, 400),
[],
);

const resetSearch = () => {
setCurrentIndex(0);
Expand Down

0 comments on commit 895c1c4

Please sign in to comment.