import { useTranslation } from "react-i18next"; import { FeatherActivity, FeatherGauge, FeatherHardDrive } from "@subframe/core"; import type { BillingUsageMetricVM, BillingUsageVM } from "@hivegrid/view-models"; import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageIntro, PageShell } from "../shared/PageShell"; import { useLocaleFormatters } from "~/locale/format"; import { Alert } from "~/ui/components/Alert"; import { Card } from "~/ui/components/Card"; import { EmptyState } from "~/ui/components/EmptyState"; import { Button } from "~/ui/components/Button"; import { Loader } from "~/ui/components/Loader"; import { Progress } from "~/ui/components/Progress"; export type BillingUsageActions = { readonly onRetry: () => void }; function formatMetric(metric: BillingUsageMetricVM, number: (value: number, options?: Intl.NumberFormatOptions) => string): string { if (metric.valueFormat === "number") return number(metric.quantity); const units = ["bytes", "kb", "mb", "gb", "tb"] as const; let value = metric.quantity; let unitIndex = 0; while (value >= 1024 && unitIndex < units.length - 1) { value /= 1024; unitIndex += 1; } return `${number(value, { maximumFractionDigits: unitIndex === 0 ? 0 : 1 })} ${units[unitIndex].toUpperCase()}`; } /** D-P20 S02 interim: v1 counters only, with no usage-billing affordance. */ export function BillingUsagePage({ vm, actions, embedded = false, }: { vm: BillingUsageVM; actions: BillingUsageActions; /** Inside the Abrechnung hub (AK-SET-01) the hub owns the header — see PageIntro. */ embedded?: boolean; }): React.JSX.Element { const { t } = useTranslation(); const formatters = useLocaleFormatters(); return ( {embedded ? null : ( } title={t("billing:usage.title")} subtitle={t("billing:usage.description")} /> )} {embedded ? {t("billing:usage.description")} : null} {vm.status === "idle" || vm.status === "loading" ? (
{t("billing:usage.loading.title")} {t("billing:usage.loading.description")}
) : vm.status === "error" ? ( {t("billing:states.retry")} } /> ) : vm.status === "empty" ? ( {t("billing:states.refresh")} } /> ) : vm.status === "ready" ? ( <> {vm.data.unavailableMetrics.includes("storage_bytes") ? (
) : null}
{t("billing:usage.currentPeriod")} {t("billing:usage.periodDescription")}
{formatters.date(vm.data.periodStart, { dateStyle: "medium" })} – {formatters.date(vm.data.periodEnd, { dateStyle: "medium" })}
{vm.data.hasExceeded ? ( ) : vm.data.hasWarning ? ( ) : null}
{vm.data.metrics.map((metric) => { const Icon = metric.metricCode === "storage_bytes" ? FeatherHardDrive : FeatherActivity; return (
{t(metric.labelKey)} {formatMetric(metric, formatters.number)}
{metric.quota === null ? ( {t("billing:usage.unlimited")} ) : ( )}
); })}
) : null}
); }