index.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import ProgressBar from '../progress-bar'
  6. import { NUM_INFINITE } from '../config'
  7. import Tooltip from '@/app/components/base/tooltip'
  8. import cn from '@/utils/classnames'
  9. type Props = {
  10. className?: string
  11. Icon: any
  12. name: string
  13. tooltip?: string
  14. usage: number
  15. total: number
  16. unit?: string
  17. }
  18. const LOW = 50
  19. const MIDDLE = 80
  20. const UsageInfo: FC<Props> = ({
  21. className,
  22. Icon,
  23. name,
  24. tooltip,
  25. usage,
  26. total,
  27. unit = '',
  28. }) => {
  29. const { t } = useTranslation()
  30. const percent = usage / total * 100
  31. const color = (() => {
  32. if (percent < LOW)
  33. return 'bg-components-progress-bar-progress-solid'
  34. if (percent < MIDDLE)
  35. return 'bg-components-progress-warning-progress'
  36. return 'bg-components-progress-error-progress'
  37. })()
  38. return (
  39. <div className={cn('p-4 flex flex-col gap-2 rounded-xl bg-components-panel-bg', className)}>
  40. <Icon className='w-4 h-4 text-text-tertiary' />
  41. <div className='flex items-center gap-1'>
  42. <div className='system-xs-medium text-text-tertiary'>{name}</div>
  43. {tooltip && (
  44. <Tooltip
  45. popupContent={
  46. <div className='w-[180px]'>
  47. {tooltip}
  48. </div>
  49. }
  50. />
  51. )}
  52. </div>
  53. <div className='flex items-center gap-1 system-md-semibold text-text-primary'>
  54. {usage}
  55. <div className='system-md-regular text-text-quaternary'>/</div>
  56. <div>{total === NUM_INFINITE ? t('billing.plansCommon.unlimited') : `${total}${unit}`}</div>
  57. </div>
  58. <ProgressBar
  59. percent={percent}
  60. color={color}
  61. />
  62. </div>
  63. )
  64. }
  65. export default React.memo(UsageInfo)