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 pageSize = defaultPageSize;
|
||||||
const searchDelay = 500;
|
|
||||||
|
|
||||||
const SourceUsersBox = ({ roleId, selectedUsers, onChange }: Props) => {
|
const SourceUsersBox = ({ roleId, selectedUsers, onChange }: Props) => {
|
||||||
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
|
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
|
||||||
|
@ -52,7 +51,7 @@ const SourceUsersBox = ({ roleId, selectedUsers, onChange }: Props) => {
|
||||||
debounce(() => {
|
debounce(() => {
|
||||||
setPage(1);
|
setPage(1);
|
||||||
setKeyword(event.target.value);
|
setKeyword(event.target.value);
|
||||||
}, searchDelay);
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const isUserAdded = (user: User) => selectedUsers.findIndex(({ id }) => id === user.id) >= 0;
|
const isUserAdded = (user: User) => selectedUsers.findIndex(({ id }) => id === user.id) >= 0;
|
||||||
|
|
|
@ -26,7 +26,6 @@ type Props = {
|
||||||
};
|
};
|
||||||
|
|
||||||
const pageSize = defaultPageSize;
|
const pageSize = defaultPageSize;
|
||||||
const searchDelay = 500;
|
|
||||||
|
|
||||||
const SourceRolesBox = ({ userId, selectedRoles, onChange }: Props) => {
|
const SourceRolesBox = ({ userId, selectedRoles, onChange }: Props) => {
|
||||||
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
|
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
|
||||||
|
@ -56,7 +55,7 @@ const SourceRolesBox = ({ userId, selectedRoles, onChange }: Props) => {
|
||||||
debounce(() => {
|
debounce(() => {
|
||||||
setPage(1);
|
setPage(1);
|
||||||
setKeyword(event.target.value);
|
setKeyword(event.target.value);
|
||||||
}, searchDelay);
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const isEmpty = !isLoading && !error && dataSource.length === 0;
|
const isEmpty = !isLoading && !error && dataSource.length === 0;
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import { useEffect, useRef } from 'react';
|
import { useEffect, useRef } from 'react';
|
||||||
|
|
||||||
const useDebounce = () => {
|
const defaultDelay = 500;
|
||||||
const timerRef = useRef<NodeJS.Timeout>();
|
|
||||||
|
const useDebounce = (delay: number = defaultDelay) => {
|
||||||
|
const timerRef = useRef<number>();
|
||||||
|
|
||||||
const clearTimer = () => {
|
const clearTimer = () => {
|
||||||
if (timerRef.current) {
|
if (timerRef.current) {
|
||||||
|
@ -15,13 +17,13 @@ const useDebounce = () => {
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (callback: () => void, wait: number) => {
|
return (callback: () => void) => {
|
||||||
clearTimer();
|
clearTimer();
|
||||||
// eslint-disable-next-line @silverhand/fp/no-mutation
|
// eslint-disable-next-line @silverhand/fp/no-mutation
|
||||||
timerRef.current = setTimeout(() => {
|
timerRef.current = window.setTimeout(() => {
|
||||||
callback();
|
callback();
|
||||||
clearTimer();
|
clearTimer();
|
||||||
}, wait);
|
}, delay);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue