2020-05-18 11:03:29 -05:00
|
|
|
import React, { FC, useState, useEffect } from 'react';
|
|
|
|
import { useInterval } from 'react-use';
|
|
|
|
import { Time, TimeProps } from './Time';
|
2018-04-26 04:58:42 -05:00
|
|
|
|
|
|
|
const INTERVAL = 150;
|
|
|
|
|
2020-05-18 11:03:29 -05:00
|
|
|
export interface ElapsedTimeProps extends Omit<TimeProps, 'timeInMs'> {
|
2019-08-29 06:41:45 -05:00
|
|
|
// Use this to reset the timer. Any value is allowed just need to be !== from the previous.
|
|
|
|
// Keep in mind things like [] !== [] or {} !== {}.
|
|
|
|
resetKey?: any;
|
2019-05-20 06:28:23 -05:00
|
|
|
}
|
|
|
|
|
2020-05-18 11:03:29 -05:00
|
|
|
export const ElapsedTime: FC<ElapsedTimeProps> = ({ resetKey, humanize, className }) => {
|
|
|
|
const [elapsed, setElapsed] = useState(0); // the current value of elapsed
|
2018-04-26 04:58:42 -05:00
|
|
|
|
2020-05-18 11:03:29 -05:00
|
|
|
// hook that will schedule a interval and then update the elapsed value on every tick.
|
|
|
|
useInterval(() => setElapsed(elapsed + INTERVAL), INTERVAL);
|
|
|
|
// this effect will only be run when resetKey changes. This will reset the elapsed to 0.
|
|
|
|
useEffect(() => setElapsed(0), [resetKey]);
|
2019-05-20 06:28:23 -05:00
|
|
|
|
2020-05-18 11:03:29 -05:00
|
|
|
return <Time timeInMs={elapsed} className={className} humanize={humanize} />;
|
|
|
|
};
|