Files
explore/ui/blockCountdown/BlockCountdownTimer.tsx
tom goriunovandGitHub f6725649da UI toolkit package (#2691)
* first version of the pack

* single entry point

* add custom components to the package

* move form components to toolkit

* add more utils and hooks to the package

* minor fixes

* add CI workflow

* update readme and package.json

* fix ts

* fix build of env-validator

* fix docker build

* fix icons size

* test fixes

* more test fixes
2025-04-21 19:44:22 +02:00

54 lines
1.4 KiB
TypeScript

import { HStack, StackSeparator } from '@chakra-ui/react';
import React from 'react';
import { SECOND } from 'toolkit/utils/consts';
import BlockCountdownTimerItem from './BlockCountdownTimerItem';
import splitSecondsInPeriods from './splitSecondsInPeriods';
interface Props {
value: number;
onFinish: () => void;
}
const BlockCountdownTimer = ({ value: initialValue, onFinish }: Props) => {
const [ value, setValue ] = React.useState(initialValue);
React.useEffect(() => {
const intervalId = window.setInterval(() => {
setValue((prev) => {
if (prev > 1) {
return prev - 1;
}
onFinish();
return 0;
});
}, SECOND);
return () => {
window.clearInterval(intervalId);
};
}, [ initialValue, onFinish ]);
const periods = splitSecondsInPeriods(value);
return (
<HStack
bgColor={{ _light: 'gray.50', _dark: 'whiteAlpha.100' }}
mt={{ base: 6, lg: 8 }}
p={{ base: 3, lg: 4 }}
borderRadius="base"
separator={ <StackSeparator borderColor="border.divider"/> }
>
<BlockCountdownTimerItem label="Days" value={ periods.days }/>
<BlockCountdownTimerItem label="Hours" value={ periods.hours }/>
<BlockCountdownTimerItem label="Minutes" value={ periods.minutes }/>
<BlockCountdownTimerItem label="Seconds" value={ periods.seconds }/>
</HStack>
);
};
export default React.memo(BlockCountdownTimer);