0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

refactor(console): useDebounce (#3040)

This commit is contained in:
Xiao Yijun 2023-02-01 16:32:50 +08:00 committed by GitHub
parent d5fd433ecc
commit 2420af2093
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 9 deletions

View file

@ -26,7 +26,6 @@ type Props = {
};
const pageSize = defaultPageSize;
const searchDelay = 500;
const SourceUsersBox = ({ roleId, selectedUsers, onChange }: Props) => {
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
@ -52,7 +51,7 @@ const SourceUsersBox = ({ roleId, selectedUsers, onChange }: Props) => {
debounce(() => {
setPage(1);
setKeyword(event.target.value);
}, searchDelay);
});
};
const isUserAdded = (user: User) => selectedUsers.findIndex(({ id }) => id === user.id) >= 0;

View file

@ -26,7 +26,6 @@ type Props = {
};
const pageSize = defaultPageSize;
const searchDelay = 500;
const SourceRolesBox = ({ userId, selectedRoles, onChange }: Props) => {
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
@ -56,7 +55,7 @@ const SourceRolesBox = ({ userId, selectedRoles, onChange }: Props) => {
debounce(() => {
setPage(1);
setKeyword(event.target.value);
}, searchDelay);
});
};
const isEmpty = !isLoading && !error && dataSource.length === 0;

View file

@ -1,7 +1,9 @@
import { useEffect, useRef } from 'react';
const useDebounce = () => {
const timerRef = useRef<NodeJS.Timeout>();
const defaultDelay = 500;
const useDebounce = (delay: number = defaultDelay) => {
const timerRef = useRef<number>();
const clearTimer = () => {
if (timerRef.current) {
@ -15,13 +17,13 @@ const useDebounce = () => {
};
}, []);
return (callback: () => void, wait: number) => {
return (callback: () => void) => {
clearTimer();
// eslint-disable-next-line @silverhand/fp/no-mutation
timerRef.current = setTimeout(() => {
timerRef.current = window.setTimeout(() => {
callback();
clearTimer();
}, wait);
}, delay);
};
};