DiskCountdownRadial.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { useParams } from 'common'
  2. import { AnimatePresence, motion } from 'framer-motion'
  3. import { useEffect, useState } from 'react'
  4. import { Card, CardContent } from 'ui'
  5. import FormMessage from './FormMessage'
  6. import CountdownTimerRadial from '@/components/ui/CountdownTimer/CountdownTimerRadial'
  7. import CountdownTimerSpan from '@/components/ui/CountdownTimer/CountdownTimerSpan'
  8. import { useRemainingDurationForDiskAttributeUpdate } from '@/data/config/disk-attributes-query'
  9. import { COOLDOWN_DURATION } from '@/data/config/disk-attributes-update-mutation'
  10. export function DiskCountdownRadial() {
  11. const { ref } = useParams()
  12. const [remainingTime, setRemainingTime] = useState(0)
  13. const {
  14. remainingDuration: initialRemainingTime,
  15. error,
  16. isSuccess,
  17. } = useRemainingDurationForDiskAttributeUpdate({
  18. projectRef: ref,
  19. })
  20. const progressPercentage = (remainingTime / COOLDOWN_DURATION) * 100
  21. useEffect(() => {
  22. if (initialRemainingTime > 0) setRemainingTime(initialRemainingTime)
  23. }, [initialRemainingTime])
  24. useEffect(() => {
  25. if (remainingTime <= 0) return
  26. const timer = setInterval(() => {
  27. setRemainingTime(Math.max(0, remainingTime - 1))
  28. }, 1000)
  29. return () => clearInterval(timer)
  30. }, [remainingTime])
  31. return (
  32. <AnimatePresence>
  33. {remainingTime > 0 && isSuccess && (
  34. <motion.div
  35. initial={{ opacity: 0, height: 0 }}
  36. animate={{ opacity: 1, height: 'auto' }}
  37. exit={{ opacity: 0, height: 0 }}
  38. transition={{ duration: 0.2 }}
  39. >
  40. <Card className="px-2 rounded-sm bg-surface-100">
  41. <CardContent className="py-3 flex gap-3 px-3 items-start">
  42. <CountdownTimerRadial progress={progressPercentage} />
  43. <div className="flex flex-col gap-2">
  44. <div>
  45. <p className="text-foreground text-sm p-0">
  46. 4-hour cooldown period is in progress
  47. </p>
  48. <p className="text-foreground-lighter text-sm p-0">
  49. You can't modify your disk configuration again until the 4-hour cool down period
  50. ends.
  51. </p>
  52. </div>
  53. <CountdownTimerSpan seconds={remainingTime} />
  54. </div>
  55. </CardContent>
  56. </Card>
  57. </motion.div>
  58. )}
  59. {error && <FormMessage message={error.message} type="error" />}
  60. </AnimatePresence>
  61. )
  62. }