From e0675cd09f89f50bbb403b580f970eaeac1fe71d Mon Sep 17 00:00:00 2001 From: Ivan Vasilov Date: Mon, 24 Jul 2023 14:10:52 +0200 Subject: [PATCH] Use the new types from the billing metrics to simplify the metric calculation. --- .../Subscription/BillingBreakdown.tsx | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/studio/components/interfaces/BillingV2/Subscription/BillingBreakdown.tsx b/studio/components/interfaces/BillingV2/Subscription/BillingBreakdown.tsx index 0e820f87202..559021c47bd 100644 --- a/studio/components/interfaces/BillingV2/Subscription/BillingBreakdown.tsx +++ b/studio/components/interfaces/BillingV2/Subscription/BillingBreakdown.tsx @@ -1,24 +1,20 @@ +import * as Tooltip from '@radix-ui/react-tooltip' import clsx from 'clsx' import { useParams } from 'common' import ShimmeringLoader from 'components/ui/ShimmeringLoader' import SparkBar from 'components/ui/SparkBar' import { useUpcomingInvoiceQuery } from 'data/invoices/invoice-upcoming-query' import { useProjectSubscriptionV2Query } from 'data/subscriptions/project-subscription-v2-query' -import { - ProjectUsageResponse, - UsageMetric, - useProjectUsageQuery, -} from 'data/usage/project-usage-query' +import { useProjectUsageQuery } from 'data/usage/project-usage-query' import dayjs from 'dayjs' import { USAGE_APPROACHING_THRESHOLD } from 'lib/constants' import { formatBytes } from 'lib/helpers' import { partition } from 'lodash' +import Link from 'next/link' import { useState } from 'react' +import { useSubscriptionPageStateSnapshot } from 'state/subscription-page' import { Alert, Button, Collapsible, IconAlertTriangle, IconChevronRight, IconInfo } from 'ui' import { BILLING_BREAKDOWN_METRICS } from './Subscription.constants' -import { useSubscriptionPageStateSnapshot } from 'state/subscription-page' -import Link from 'next/link' -import * as Tooltip from '@radix-ui/react-tooltip' export interface BillingBreakdownProps {} @@ -127,22 +123,18 @@ const BillingBreakdown = ({}: BillingBreakdownProps) => { ) : (
{BILLING_BREAKDOWN_METRICS.map((metric, i) => { - const usageMeta = - (usage?.[metric.key as keyof ProjectUsageResponse] as UsageMetric) ?? undefined - const usageRatio = - typeof usageMeta !== 'number' - ? (usageMeta?.usage ?? 0) / (usageMeta?.limit ?? 0) - : 0 + const usageMeta = usage?.[metric.key] + const usageRatio = (usageMeta?.usage ?? 0) / (usageMeta?.limit ?? 0) - const hasLimit = usageMeta.limit > 0 + const hasLimit = (usageMeta?.limit ?? 0) > 0 const usageCurrentLabel = metric.units === 'bytes' - ? formatBytes(usageMeta.usage) - : usageMeta.usage?.toLocaleString() + ? formatBytes(usageMeta?.usage) + : usageMeta?.usage?.toLocaleString() const usageLimitLabel = metric.units === 'bytes' - ? formatBytes(usageMeta.limit) - : usageMeta.limit.toLocaleString() + ? formatBytes(usageMeta?.limit) + : usageMeta?.limit.toLocaleString() const usageLabel = `${usageCurrentLabel} ${hasLimit ? `of ${usageLimitLabel}` : ''}` const percentageLabel = `${(usageRatio * 100).toFixed(2)}%` @@ -282,7 +274,7 @@ const BillingBreakdown = ({}: BillingBreakdownProps) => { )}
- {usageMeta.available_in_plan ? ( + {usageMeta?.available_in_plan ? (