UsageSection.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import SectionHeader from '../SectionHeader'
  2. import { CategoryMetaKey, USAGE_CATEGORIES } from '../Usage.constants'
  3. import AttributeUsage from './AttributeUsage'
  4. import DatabaseSizeUsage from './DatabaseSizeUsage'
  5. import { DiskUsage } from './DiskUsage'
  6. import { ScaffoldContainer } from '@/components/layouts/Scaffold'
  7. import { DataPoint } from '@/data/analytics/constants'
  8. import { PricingMetric } from '@/data/analytics/org-daily-stats-query'
  9. import type { OrgSubscription } from '@/data/subscriptions/types'
  10. import { useOrgUsageQuery } from '@/data/usage/org-usage-query'
  11. export interface ChartMeta {
  12. [key: string]: { data: DataPoint[]; margin: number; isLoading: boolean }
  13. }
  14. export interface UsageSectionProps {
  15. orgSlug: string
  16. projectRef?: string | null
  17. categoryKey: CategoryMetaKey
  18. subscription?: OrgSubscription
  19. chartMeta: ChartMeta
  20. currentBillingCycleSelected: boolean
  21. startDate?: string
  22. endDate?: string
  23. }
  24. const UsageSection = ({
  25. orgSlug,
  26. projectRef,
  27. categoryKey,
  28. chartMeta,
  29. subscription,
  30. currentBillingCycleSelected,
  31. startDate,
  32. endDate,
  33. }: UsageSectionProps) => {
  34. const {
  35. data: usage,
  36. error: usageError,
  37. isPending: isLoadingUsage,
  38. isError: isErrorUsage,
  39. isSuccess: isSuccessUsage,
  40. } = useOrgUsageQuery({
  41. orgSlug,
  42. projectRef,
  43. start: !currentBillingCycleSelected && startDate ? new Date(startDate) : undefined,
  44. end: !currentBillingCycleSelected && endDate ? new Date(endDate) : undefined,
  45. })
  46. const categoryMeta = USAGE_CATEGORIES(subscription).find(
  47. (category) => category.key === categoryKey
  48. )
  49. if (!categoryMeta) return null
  50. return (
  51. <>
  52. <ScaffoldContainer>
  53. <SectionHeader
  54. title={categoryMeta.name}
  55. description={categoryMeta.description}
  56. className="pb-0"
  57. />
  58. </ScaffoldContainer>
  59. {categoryMeta.attributes.map((attribute) =>
  60. attribute.key === 'diskSize' ? (
  61. <DiskUsage
  62. key={attribute.name}
  63. slug={orgSlug}
  64. projectRef={projectRef}
  65. attribute={attribute}
  66. subscription={subscription}
  67. currentBillingCycleSelected={currentBillingCycleSelected}
  68. usage={usage}
  69. />
  70. ) : attribute.key === PricingMetric.DATABASE_SIZE && subscription?.plan.id === 'free' ? (
  71. <DatabaseSizeUsage
  72. key={attribute.name}
  73. slug={orgSlug}
  74. projectRef={projectRef}
  75. attribute={attribute}
  76. subscription={subscription}
  77. currentBillingCycleSelected={currentBillingCycleSelected}
  78. usage={usage}
  79. />
  80. ) : (
  81. <AttributeUsage
  82. key={attribute.name}
  83. slug={orgSlug}
  84. projectRef={projectRef}
  85. attribute={attribute}
  86. usage={usage}
  87. usageMeta={usage?.usages.find((x) => x.metric === attribute.key)}
  88. chartMeta={chartMeta}
  89. subscription={subscription}
  90. error={usageError}
  91. isLoading={isLoadingUsage}
  92. isError={isErrorUsage}
  93. isSuccess={isSuccessUsage}
  94. currentBillingCycleSelected={currentBillingCycleSelected}
  95. />
  96. )
  97. )}
  98. </>
  99. )
  100. }
  101. export default UsageSection