1 import { useEffect, useMemo, useRef, useState } from 'react';
3 import type { Maybe } from '@proton/pass/types';
5 export const useCountdown = (initial?: number) => {
6 const interval = useRef<Maybe<NodeJS.Timeout>>();
7 const [value, setValue] = useState<number>(0);
9 const handle = useRef({
10 start: (value: number) => {
11 clearInterval(interval.current);
13 interval.current = setInterval(
16 if (prev === 0) clearInterval(interval.current);
17 return Math.max(0, prev - 1);
23 clearInterval(interval.current);
29 if (initial) handle.current.start(initial);
30 return handle.current.cancel;
33 return useMemo(() => [value, handle.current] as const, [value]);