snowballtools-base/packages/frontend/src/components/StopWatch.tsx
2024-04-11 21:49:14 -05:00

27 lines
825 B
TypeScript

import { useStopwatch } from 'react-timer-hook';
import FormatMillisecond, { FormatMilliSecondProps } from './FormatMilliSecond';
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;
};
interface StopwatchProps extends Omit<FormatMilliSecondProps, 'time'> {
offsetTimestamp: Date;
}
const Stopwatch = ({ offsetTimestamp, ...props }: StopwatchProps) => {
const { totalSeconds } = useStopwatch({
autoStart: true,
offsetTimestamp: offsetTimestamp,
});
return <FormatMillisecond time={totalSeconds * 1000} {...props} />;
};
export { Stopwatch, setStopWatchOffset };