robosats/frontend/src/components/OrderDetails/LinearDeterminate.tsx

44 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-09-09 17:18:04 +00:00
import React, { useState, useEffect } from 'react';
import { Box, LinearProgress } from '@mui/material';
import { calcTimeDelta } from 'react-countdown';
interface Props {
expiresAt: string;
totalSecsExp: number;
}
2022-09-09 17:18:04 +00:00
const LinearDeterminate = ({ expiresAt, totalSecsExp }: Props): JSX.Element => {
2022-12-01 10:18:42 +00:00
const timePercentLeft = function () {
if (expiresAt && totalSecsExp) {
const lapseTime = calcTimeDelta(new Date(expiresAt)).total / 1000;
return (lapseTime / totalSecsExp) * 100;
} else {
return 100;
}
2022-12-01 10:18:42 +00:00
};
const [progress, setProgress] = useState<number>(timePercentLeft);
2022-12-01 10:18:42 +00:00
useEffect(() => {
const timer = setInterval(() => {
setProgress(timePercentLeft);
}, 1000);
return () => {
clearInterval(timer);
};
}, [expiresAt, totalSecsExp]);
return (
<Box sx={{ width: '100%' }}>
<LinearProgress
sx={{ height: '0.4em' }}
variant='determinate'
value={progress}
color={progress < 25 ? 'secondary' : 'primary'}
/>
</Box>
);
};
export default LinearDeterminate;