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:
parent
d5fd433ecc
commit
2420af2093
3 changed files with 9 additions and 9 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue