2023-12-19 08:52:25 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { useStopwatch } from 'react-timer-hook';
|
|
|
|
|
2024-03-03 05:41:25 +00:00
|
|
|
import FormatMillisecond, { FormatMilliSecondProps } from './FormatMilliSecond';
|
2023-12-19 08:52:25 +00:00
|
|
|
|
|
|
|
const setStopWatchOffset = (time: string) => {
|
|
|
|
const providedTime = new Date(time);
|
|
|
|
const currentTime = new Date();
|
|
|
|
const timeDifference = currentTime.getTime() - providedTime.getTime();
|
|
|
|
currentTime.setMilliseconds(currentTime.getMilliseconds() + timeDifference);
|
|
|
|
return currentTime;
|
|
|
|
};
|
|
|
|
|
2024-03-03 05:41:25 +00:00
|
|
|
interface StopwatchProps extends Omit<FormatMilliSecondProps, 'time'> {
|
|
|
|
offsetTimestamp: Date;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Stopwatch = ({ offsetTimestamp, ...props }: StopwatchProps) => {
|
2023-12-19 08:52:25 +00:00
|
|
|
const { totalSeconds } = useStopwatch({
|
|
|
|
autoStart: true,
|
|
|
|
offsetTimestamp: offsetTimestamp,
|
|
|
|
});
|
|
|
|
|
2024-03-03 05:41:25 +00:00
|
|
|
return <FormatMillisecond time={totalSeconds * 1000} {...props} />;
|
2023-12-19 08:52:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export { Stopwatch, setStopWatchOffset };
|