Files
twenty_crm/front/src/modules/ui/feedback/snack-bar/hooks/usePausableTimeout.ts
gitstart-twenty 0c79217ba0 Add an ESLint rule to prevent the usage of useRef other than for HTML elements. (#2014)
* Add an ESLint rule to prevent the usage of useRef other than for HTML elements

Co-authored-by: v1b3m <vibenjamin6@gmail.com>

* Bump eslint version and rewrite rule

* Fix

---------

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Charles Bochet <charles@twenty.com>
2023-10-14 11:32:46 +02:00

55 lines
1.5 KiB
TypeScript

import { useCallback, useEffect, useRef } from 'react';
export const usePausableTimeout = (callback: () => void, delay: number) => {
// eslint-disable-next-line twenty/no-state-useref
const savedCallback = useRef<() => void>(callback);
// eslint-disable-next-line twenty/no-state-useref
const remainingTime = useRef<number>(delay);
// eslint-disable-next-line twenty/no-state-useref
const startTime = useRef<number>(Date.now());
// eslint-disable-next-line twenty/no-state-useref
const timeoutId = useRef<ReturnType<typeof setTimeout> | null>(null);
const tick = () => {
if (savedCallback.current) {
savedCallback.current();
}
};
const startTimeout = useCallback(() => {
startTime.current = Date.now();
timeoutId.current = setTimeout(tick, remainingTime.current);
}, []);
// Remember the latest callback
useEffect(() => {
savedCallback.current = callback;
}, [callback]);
// Set up the timeout loop
useEffect(() => {
if (delay !== null) {
startTimeout();
return () => {
if (timeoutId.current) {
clearTimeout(timeoutId.current);
}
};
}
}, [delay, startTimeout]);
const pauseTimeout = () => {
if (timeoutId.current) {
clearTimeout(timeoutId.current);
}
const elapsedTime = Date.now() - startTime.current;
remainingTime.current = remainingTime.current - elapsedTime;
};
const resumeTimeout = () => {
startTimeout();
};
return { pauseTimeout, resumeTimeout };
};