mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-10 17:43:51 +00:00
Use test seams instead of DI in useTimeoutToggle
This commit is contained in:
@@ -1,18 +1,22 @@
|
||||
import { parseQuery } from '@shlinkio/shlink-frontend-kit';
|
||||
import { useRef, useState } from 'react';
|
||||
import { useCallback, useRef, useState } from 'react';
|
||||
import { useLocation, useNavigate } from 'react-router-dom';
|
||||
|
||||
const DEFAULT_DELAY = 2000;
|
||||
|
||||
export type TimeoutToggle = (initialValue?: boolean, delay?: number) => [boolean, () => void];
|
||||
export type TimeoutToggle = typeof useTimeoutToggle;
|
||||
|
||||
export const useTimeoutToggle = (
|
||||
setTimeout: (callback: Function, timeout: number) => number,
|
||||
clearTimeout: (timer: number) => void,
|
||||
): TimeoutToggle => (initialValue = false, delay = DEFAULT_DELAY) => {
|
||||
initialValue = false,
|
||||
delay = DEFAULT_DELAY,
|
||||
|
||||
// Test seams
|
||||
setTimeout = window.setTimeout,
|
||||
clearTimeout = window.clearTimeout,
|
||||
): [boolean, () => void] => {
|
||||
const [flag, setFlag] = useState<boolean>(initialValue);
|
||||
const timeout = useRef<number | undefined>(undefined);
|
||||
const callback = () => {
|
||||
const callback = useCallback(() => {
|
||||
setFlag(!initialValue);
|
||||
|
||||
if (timeout.current) {
|
||||
@@ -20,7 +24,7 @@ export const useTimeoutToggle = (
|
||||
}
|
||||
|
||||
timeout.current = setTimeout(() => setFlag(initialValue), delay);
|
||||
};
|
||||
}, [clearTimeout, delay, initialValue, setTimeout]);
|
||||
|
||||
return [flag, callback];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user