diff options
Diffstat (limited to 'src/utils/react.tsx')
-rw-r--r-- | src/utils/react.tsx | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/utils/react.tsx b/src/utils/react.tsx index a4c7152..e8c1081 100644 --- a/src/utils/react.tsx +++ b/src/utils/react.tsx @@ -16,7 +16,7 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import { React, useEffect, useReducer, useState } from "@webpack/common"; +import { React, useEffect, useMemo, useReducer, useState } from "@webpack/common"; import { makeLazy } from "./lazy"; import { checkIntersecting } from "./misc"; @@ -135,3 +135,24 @@ export function LazyComponent<T extends object = any>(factory: () => React.Compo return <Component {...props} />; }; } + +interface TimerOpts { + interval?: number; + deps?: unknown[]; +} + +export function useTimer({ interval = 1000, deps = [] }: TimerOpts) { + const [time, setTime] = useState(0); + const start = useMemo(() => Date.now(), deps); + + useEffect(() => { + const intervalId = setInterval(() => setTime(Date.now() - start), interval); + + return () => { + setTime(0); + clearInterval(intervalId); + }; + }, deps); + + return time; +} |