import { useEffect, useState } from 'react';

import { differenceInSeconds } from 'date-fns';

const useTimer = () => {
  const [startTime, setStartTime] = useState<Date>();

  useEffect(() => {
    setStartTime(new Date());

    return () => {
      setStartTime(undefined);
    };
  }, []);

  return {
    getDuration: () => differenceInSeconds(new Date(), startTime),
  };
};

export default useTimer;
