import { useRef, useEffect } from "react";

export const useInterval = async (
  callback: () => Promise<void>,
  interval: number
) => {
  const callbackRef = useRef<() => Promise<void>>(callback);
  useEffect(() => {
    callbackRef.current = callback;
  }, [callback]);

  useEffect(() => {
    const updateToken = async () => {
      await callbackRef.current();
    };
    const id = setInterval(updateToken, interval);
    return () => {
      clearInterval(id);
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
};
