334 lines
15 KiB
TypeScript
334 lines
15 KiB
TypeScript
import { useState } from "react";
|
||
import { useTranslation } from "react-i18next";
|
||
import { FeatherClock, FeatherCreditCard, FeatherExternalLink, FeatherPackage, FeatherUsers } from "@subframe/core";
|
||
import type { TFunction } from "i18next";
|
||
import type { SubscriptionBannerTone, SubscriptionOverviewProductVM, SubscriptionOverviewVM } from "@hivegrid/view-models";
|
||
import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell";
|
||
import { useLocaleFormatters } from "~/locale/format";
|
||
import { Alert } from "~/ui/components/Alert";
|
||
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
|
||
import { Button } from "~/ui/components/Button";
|
||
import { Card } from "~/ui/components/Card";
|
||
import { Dialog } from "~/ui/components/Dialog";
|
||
import { EmptyState } from "~/ui/components/EmptyState";
|
||
import { Loader } from "~/ui/components/Loader";
|
||
import { Progress } from "~/ui/components/Progress";
|
||
import { StatsCard } from "~/ui/components/StatsCard";
|
||
import { Table } from "~/ui/components/Table";
|
||
|
||
export type SubscriptionOverviewActions = {
|
||
readonly onCancel: (productId: string) => void | Promise<void>;
|
||
readonly onOpenContact: () => void;
|
||
readonly onRetry: () => void;
|
||
};
|
||
|
||
function productTitle(t: TFunction, value: string): string {
|
||
return value.startsWith("settings.subscription.checkout.plans.packages.") ? t(value) : value;
|
||
}
|
||
|
||
function alertVariant(tone: SubscriptionBannerTone): "success" | "warning" | "error" {
|
||
if (tone === "success") return "success";
|
||
if (tone === "warning") return "warning";
|
||
return "error";
|
||
}
|
||
|
||
function productTone(state: SubscriptionOverviewProductVM["state"]): React.ComponentProps<typeof BadgeFeedback>["farbe"] {
|
||
if (state === "active") return "subtle-positive";
|
||
if (state === "cancelled") return "subtle-warning";
|
||
return "subtle-neutral";
|
||
}
|
||
|
||
/** D-P20 S01 interim: tenant subscription overview, deliberately read-only except cancellation. */
|
||
export function SubscriptionOverviewPage({ vm, actions }: { vm: SubscriptionOverviewVM; actions: SubscriptionOverviewActions }): React.JSX.Element {
|
||
const { t } = useTranslation();
|
||
const formatters = useLocaleFormatters();
|
||
const [cancelProduct, setCancelProduct] = useState<SubscriptionOverviewProductVM | null>(null);
|
||
const [cancelling, setCancelling] = useState(false);
|
||
|
||
if (vm.status === "idle" || vm.status === "loading") {
|
||
return (
|
||
<div className="flex w-full flex-col items-start gap-6 px-6 py-6" data-ui-id="ui.billing.subscriptionOverview.loading">
|
||
<Card>
|
||
<div className="flex w-full items-center gap-3">
|
||
<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:subscriptionOverview.loading.title")}
|
||
</span>
|
||
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
|
||
{t("billing:subscriptionOverview.loading.description")}
|
||
</span>
|
||
</div>
|
||
</div>
|
||
</Card>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
if (vm.status === "error") {
|
||
return (
|
||
<div className="flex w-full flex-col items-start gap-6 px-6 py-6" data-ui-id="ui.billing.subscriptionOverview.error">
|
||
<EmptyState
|
||
outlined
|
||
variant="error"
|
||
title={t("billing:subscriptionOverview.error.title")}
|
||
body={t(vm.error.messageKey)}
|
||
action={
|
||
<Button variant="tertiary" onClick={actions.onRetry} data-ui-id="ui.billing.subscriptionOverview.retry">
|
||
{t("billing:states.retry")}
|
||
</Button>
|
||
}
|
||
/>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
if (vm.status === "empty") {
|
||
return (
|
||
<div className="flex w-full flex-col items-start gap-6 px-6 py-6" data-ui-id="ui.billing.subscriptionOverview.empty">
|
||
<EmptyState
|
||
outlined
|
||
title={t("billing:subscriptionOverview.empty.title")}
|
||
body={t("billing:subscriptionOverview.empty.description")}
|
||
action={
|
||
<Button
|
||
variant="tertiary"
|
||
icon={<FeatherExternalLink />}
|
||
onClick={actions.onOpenContact}
|
||
data-ui-id="ui.billing.subscriptionOverview.contact"
|
||
>
|
||
{t("billing:subscriptionOverview.contact")}
|
||
</Button>
|
||
}
|
||
/>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
const { data } = vm;
|
||
const totalSeats = data.seatUsage.status === "ready" ? data.seatUsage.data.categories.reduce((sum, category) => sum + category.used, 0) : 0;
|
||
|
||
async function confirmCancellation(): Promise<void> {
|
||
if (!cancelProduct) return;
|
||
setCancelling(true);
|
||
try {
|
||
await actions.onCancel(cancelProduct.id);
|
||
setCancelProduct(null);
|
||
} finally {
|
||
setCancelling(false);
|
||
}
|
||
}
|
||
|
||
return (
|
||
<PageShell uiId="ui.billing.subscriptionOverview.page" className="bg-surface-neutral-primary">
|
||
<PageHeader
|
||
icon={<FeatherCreditCard className={PAGE_HEADER_ICON_CLASS} />}
|
||
title={t("billing:subscriptionOverview.title")}
|
||
subtitle={t("billing:subscriptionOverview.description")}
|
||
actions={
|
||
<Button
|
||
variant="secondary"
|
||
icon={<FeatherExternalLink />}
|
||
onClick={actions.onOpenContact}
|
||
data-ui-id="ui.billing.subscriptionOverview.contact"
|
||
>
|
||
{t("billing:subscriptionOverview.contact")}
|
||
</Button>
|
||
}
|
||
/>
|
||
|
||
<PageBody className="items-start">
|
||
{data.banner.status === "ready" ? (
|
||
<Alert
|
||
notDismissible
|
||
variant={alertVariant(data.banner.data.tone)}
|
||
title={t(data.banner.data.titleKey)}
|
||
description={t(data.banner.data.descriptionKey, { count: data.banner.data.trialDaysRemaining ?? undefined })}
|
||
/>
|
||
) : null}
|
||
|
||
<div
|
||
className={data.trialDaysRemaining === null ? "mobile:grid-cols-1 grid w-full grid-cols-3 gap-4" : "mobile:grid-cols-1 grid w-full grid-cols-4 gap-4"}
|
||
>
|
||
<StatsCard
|
||
trend="neutral"
|
||
icon={<FeatherPackage />}
|
||
label={t("billing:subscriptionOverview.summary.plan")}
|
||
value={data.planLabel ? productTitle(t, data.planLabel) : t("billing:subscriptionOverview.summary.noPlan")}
|
||
percentage={t("billing:subscriptionOverview.summary.readOnly")}
|
||
/>
|
||
<StatsCard
|
||
trend={data.status === "active" || data.status === "trial" ? "positive" : "negative"}
|
||
icon={<FeatherCreditCard />}
|
||
label={t("billing:subscriptionOverview.summary.status")}
|
||
value={t(data.statusLabelKey)}
|
||
percentage={t("billing:subscriptionOverview.summary.synced")}
|
||
/>
|
||
<StatsCard
|
||
trend="neutral"
|
||
icon={<FeatherUsers />}
|
||
label={t("billing:subscriptionOverview.summary.seats")}
|
||
value={formatters.number(totalSeats)}
|
||
percentage={t("billing:subscriptionOverview.summary.activeSeats")}
|
||
/>
|
||
{data.trialDaysRemaining !== null && data.trialEndsAt ? (
|
||
<StatsCard
|
||
trend={data.trialDaysRemaining <= 3 ? "negative" : "neutral"}
|
||
icon={<FeatherClock />}
|
||
label={t("billing:subscriptionOverview.summary.trial")}
|
||
value={t("billing:subscriptionOverview.summary.trialDays", { count: data.trialDaysRemaining })}
|
||
percentage={formatters.date(data.trialEndsAt, { dateStyle: "medium" })}
|
||
/>
|
||
) : null}
|
||
</div>
|
||
|
||
<Card data-ui-id="ui.billing.subscriptionOverview.products">
|
||
<div className="flex w-full flex-col items-start">
|
||
<span className="text-body-large font-body-large text-text-neutral-primary">{t("billing:subscriptionOverview.products.title")}</span>
|
||
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
|
||
{t("billing:subscriptionOverview.products.description")}
|
||
</span>
|
||
</div>
|
||
{data.products.length === 0 ? (
|
||
<EmptyState title={t(data.emptyTitleKey)} body={t(data.emptyDescriptionKey)} />
|
||
) : (
|
||
<div className="w-full overflow-x-auto">
|
||
<Table
|
||
header={
|
||
<Table.HeaderRow>
|
||
<Table.HeaderCell>{t("billing:subscriptionOverview.products.columns.position")}</Table.HeaderCell>
|
||
<Table.HeaderCell>{t("billing:subscriptionOverview.products.columns.price")}</Table.HeaderCell>
|
||
<Table.HeaderCell>{t("billing:subscriptionOverview.products.columns.periodEnd")}</Table.HeaderCell>
|
||
<Table.HeaderCell>{t("billing:subscriptionOverview.products.columns.status")}</Table.HeaderCell>
|
||
<Table.HeaderCell>{t("billing:subscriptionOverview.products.columns.action")}</Table.HeaderCell>
|
||
</Table.HeaderRow>
|
||
}
|
||
>
|
||
{data.products.map((product) => (
|
||
<Table.Row key={product.id}>
|
||
<Table.Cell>
|
||
<div className="flex flex-col items-start">
|
||
<span className="text-body-small-medium-150 font-body-small-medium-150 text-text-neutral-primary">
|
||
{product.quantity > 1 ? `${formatters.number(product.quantity)} × ` : ""}
|
||
{productTitle(t, product.titleKey)}
|
||
</span>
|
||
{product.code ? (
|
||
<span className="text-body-extra-small-regular-0 font-body-extra-small-regular-0 text-text-neutral-tertiary">{product.code}</span>
|
||
) : null}
|
||
</div>
|
||
</Table.Cell>
|
||
<Table.Cell>
|
||
<div className="flex flex-col items-start gap-1">
|
||
{product.prices.length === 0 ? (
|
||
<span className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary">
|
||
{t("billing:subscriptionOverview.products.noPrice")}
|
||
</span>
|
||
) : (
|
||
product.prices.map((price, index) => (
|
||
<span key={`${product.id}-${index}`} className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-secondary">
|
||
{price.amount !== null && price.currency
|
||
? `${formatters.currency(price.amount * price.quantity, price.currency)} / ${t(price.periodLabelKey)}`
|
||
: t("billing:subscriptionOverview.products.noPrice")}
|
||
</span>
|
||
))
|
||
)}
|
||
</div>
|
||
</Table.Cell>
|
||
<Table.Cell>
|
||
<span className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-secondary">
|
||
{product.currentPeriodEnd
|
||
? formatters.date(product.currentPeriodEnd, { dateStyle: "medium" })
|
||
: t("billing:subscriptionOverview.products.openEnded")}
|
||
</span>
|
||
</Table.Cell>
|
||
<Table.Cell>
|
||
<BadgeFeedback farbe={productTone(product.state)} size="small">
|
||
{t(product.stateLabelKey)}
|
||
</BadgeFeedback>
|
||
</Table.Cell>
|
||
<Table.Cell>
|
||
{product.canCancel ? (
|
||
<Button variant="tertiary" size="small" onClick={() => setCancelProduct(product)} data-ui-id="ui.billing.subscriptionOverview.cancel">
|
||
{t("billing:subscriptionOverview.cancel.action")}
|
||
</Button>
|
||
) : null}
|
||
</Table.Cell>
|
||
</Table.Row>
|
||
))}
|
||
</Table>
|
||
</div>
|
||
)}
|
||
</Card>
|
||
|
||
{data.seatUsage.status === "ready" ? (
|
||
<Card data-ui-id="ui.billing.subscriptionOverview.seats">
|
||
<div className="flex w-full flex-col items-start">
|
||
<span className="text-body-large font-body-large text-text-neutral-primary">{t(data.seatUsage.data.titleKey)}</span>
|
||
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
|
||
{t(data.seatUsage.data.descriptionKey)}
|
||
</span>
|
||
</div>
|
||
<div className="mobile:grid-cols-1 grid w-full grid-cols-2 gap-4">
|
||
{data.seatUsage.data.categories.map((category) => {
|
||
const percent = category.included ? Math.min(100, Math.round((category.used / category.included) * 100)) : 0;
|
||
return (
|
||
<Card key={category.seatType} variant={category.overLimit ? "warning" : "neutral"}>
|
||
<Progress
|
||
className="w-full"
|
||
state={category.overLimit ? "error" : "uploading"}
|
||
value={percent}
|
||
label={t(category.labelKey)}
|
||
statusText={
|
||
category.included === null
|
||
? formatters.number(category.used)
|
||
: t("billing:subscriptionOverview.seats.used", { used: category.used, included: category.included })
|
||
}
|
||
/>
|
||
</Card>
|
||
);
|
||
})}
|
||
</div>
|
||
</Card>
|
||
) : null}
|
||
|
||
<Alert
|
||
notDismissible
|
||
variant="information"
|
||
title={t("billing:subscriptionOverview.externalManagement.title")}
|
||
description={t("billing:subscriptionOverview.externalManagement.description")}
|
||
/>
|
||
</PageBody>
|
||
|
||
<Dialog open={cancelProduct !== null} onOpenChange={(open: boolean) => (open ? undefined : setCancelProduct(null))}>
|
||
<Dialog.Content
|
||
className="w-[420px] max-w-[90vw] p-4"
|
||
aria-label={t("billing:subscriptionOverview.cancel.title")}
|
||
data-ui-id="ui.billing.subscriptionOverview.cancelDialog"
|
||
>
|
||
<div className="flex w-full flex-col items-start gap-2">
|
||
<span className="text-heading-0 font-heading-0 text-text-neutral-primary">{t("billing:subscriptionOverview.cancel.title")}</span>
|
||
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
|
||
{t("billing:subscriptionOverview.cancel.description", {
|
||
product: cancelProduct ? productTitle(t, cancelProduct.titleKey) : "",
|
||
})}
|
||
</span>
|
||
</div>
|
||
<div className="flex w-full items-center justify-end gap-2 pt-4">
|
||
<Button variant="tertiary" disabled={cancelling} onClick={() => setCancelProduct(null)}>
|
||
{t("shared.cancel")}
|
||
</Button>
|
||
<Button
|
||
variant="destructive"
|
||
loading={cancelling}
|
||
onClick={() => void confirmCancellation()}
|
||
data-ui-id="ui.billing.subscriptionOverview.cancelConfirm"
|
||
>
|
||
{t("billing:subscriptionOverview.cancel.confirm")}
|
||
</Button>
|
||
</div>
|
||
</Dialog.Content>
|
||
</Dialog>
|
||
</PageShell>
|
||
);
|
||
}
|