Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/screens/billing/BillingUsagePage.tsx
T

167 lines
7.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 (
<PageShell uiId="ui.billing.usage.page" className="bg-surface-neutral-primary">
{embedded ? null : (
<PageHeader
icon={<FeatherGauge className={PAGE_HEADER_ICON_CLASS} />}
title={t("billing:usage.title")}
subtitle={t("billing:usage.description")}
/>
)}
<PageBody className="items-start">
{embedded ? <PageIntro uiId="ui.billing.usage.intro">{t("billing:usage.description")}</PageIntro> : null}
{vm.status === "idle" || vm.status === "loading" ? (
<Card>
<div className="flex w-full items-center gap-3" data-ui-id="ui.billing.usage.loading">
<Loader />
<div className="flex flex-col items-start">
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">{t("billing:usage.loading.title")}</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
{t("billing:usage.loading.description")}
</span>
</div>
</div>
</Card>
) : vm.status === "error" ? (
<EmptyState
outlined
variant="error"
title={t("billing:usage.error.title")}
body={t(vm.error.messageKey)}
action={
<Button variant="tertiary" onClick={actions.onRetry} data-ui-id="ui.billing.usage.retry">
{t("billing:states.retry")}
</Button>
}
/>
) : vm.status === "empty" ? (
<EmptyState
outlined
title={t("billing:usage.emptyTitle")}
body={t("billing:usage.empty")}
action={
<Button variant="tertiary" onClick={actions.onRetry}>
{t("billing:states.refresh")}
</Button>
}
/>
) : vm.status === "ready" ? (
<>
{vm.data.unavailableMetrics.includes("storage_bytes") ? (
<div className="flex w-full flex-col items-start gap-3" data-ui-id="ui.billing.usage.storageUnavailable">
<Alert
notDismissible
variant="warning"
title={t("billing:usage.error.title")}
description={t("billing:usage.error.description")}
/>
<Button variant="tertiary" onClick={actions.onRetry} data-ui-id="ui.billing.usage.retryStorage">
{t("billing:states.retry")}
</Button>
</div>
) : null}
<Card variant="neutral">
<div className="mobile:flex-col mobile:items-start flex w-full items-center justify-between gap-4">
<div className="flex flex-col items-start">
<span className="text-body-large font-body-large text-text-neutral-primary">{t("billing:usage.currentPeriod")}</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
{t("billing:usage.periodDescription")}
</span>
</div>
<span className="text-body-small-medium-150 font-body-small-medium-150 text-text-neutral-secondary">
{formatters.date(vm.data.periodStart, { dateStyle: "medium" })} – {formatters.date(vm.data.periodEnd, { dateStyle: "medium" })}
</span>
</div>
</Card>
{vm.data.hasExceeded ? (
<Alert
notDismissible
variant="error"
title={t("billing:usage.alert.exceeded.title")}
description={t("billing:usage.alert.exceeded.description")}
/>
) : vm.data.hasWarning ? (
<Alert
notDismissible
variant="warning"
title={t("billing:usage.alert.warning.title")}
description={t("billing:usage.alert.warning.description")}
/>
) : null}
<div className="mobile:grid-cols-1 grid w-full grid-cols-2 gap-4">
{vm.data.metrics.map((metric) => {
const Icon = metric.metricCode === "storage_bytes" ? FeatherHardDrive : FeatherActivity;
return (
<Card key={metric.metricCode} variant={metric.tone === "danger" ? "negative" : metric.tone === "warning" ? "warning" : "default"}>
<div className="flex w-full items-start justify-between gap-4">
<div className="flex flex-col items-start">
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-secondary">{t(metric.labelKey)}</span>
<span className="text-subtitle-0 font-subtitle-0 text-text-neutral-primary">{formatMetric(metric, formatters.number)}</span>
</div>
<Icon className="text-heading- font-heading- text-icon-neutral-tertiary" />
</div>
{metric.quota === null ? (
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">{t("billing:usage.unlimited")}</span>
) : (
<Progress
className="w-full"
state={metric.exceeded ? "error" : "uploading"}
value={metric.percentUsed ?? 0}
label={t("billing:usage.quota", { quota: formatMetric({ ...metric, quantity: metric.quota }, formatters.number) })}
statusText={t("billing:usage.remaining", {
remaining: formatMetric({ ...metric, quantity: metric.remaining ?? 0 }, formatters.number),
})}
/>
)}
</Card>
);
})}
</div>
</>
) : null}
</PageBody>
</PageShell>
);
}