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

253 lines
13 KiB
TypeScript

import { useState } from "react";
import { useTranslation } from "react-i18next";
import { FeatherCreditCard, FeatherDownload, FeatherEye, FeatherFileText, FeatherReceipt, FeatherX } from "@subframe/core";
import type { BillingPaymentInvoiceVM, BillingPaymentVM } 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 { BadgeFeedback } from "~/ui/components/BadgeFeedback";
import { Button } from "~/ui/components/Button";
import { Card } from "~/ui/components/Card";
import { EmptyState } from "~/ui/components/EmptyState";
import { Loader } from "~/ui/components/Loader";
import { StatsCard } from "~/ui/components/StatsCard";
import { Table } from "~/ui/components/Table";
export type BillingPaymentActions = {
readonly onDownloadInvoice: (href: string) => void;
readonly onRetry: () => void;
};
function invoiceTone(tone: BillingPaymentInvoiceVM["tone"]): React.ComponentProps<typeof BadgeFeedback>["farbe"] {
if (tone === "positive") return "subtle-positive";
if (tone === "negative") return "subtle-negative";
if (tone === "warning") return "subtle-warning";
return "subtle-neutral";
}
/** D-P20 S03 interim: Lago invoices and their Mollie payment result. */
export function BillingPaymentPage({
vm,
actions,
embedded = false,
}: {
vm: BillingPaymentVM;
actions: BillingPaymentActions;
/** 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();
// Which invoice is open in the preview frame below the table. Local view
// state: the frame streams the same route the download button opens, so the
// route has nothing to prepare for it.
const [previewInvoiceId, setPreviewInvoiceId] = useState<string | null>(null);
const previewInvoice = vm.status === "ready" ? (vm.data.invoices.find((invoice) => invoice.id === previewInvoiceId) ?? null) : null;
return (
<PageShell uiId="ui.billing.payment.page" className="bg-surface-neutral-primary">
{embedded ? null : (
<PageHeader
icon={<FeatherReceipt className={PAGE_HEADER_ICON_CLASS} />}
title={t("billing:billingPayment.title")}
subtitle={t("billing:billingPayment.description")}
/>
)}
<PageBody className="items-start">
{embedded ? <PageIntro uiId="ui.billing.payment.intro">{t("billing:billingPayment.description")}</PageIntro> : null}
{vm.status === "idle" || vm.status === "loading" ? (
<Card>
<div className="flex w-full items-center gap-3" data-ui-id="ui.billing.payment.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:billingPayment.loading.title")}
</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
{t("billing:billingPayment.loading.description")}
</span>
</div>
</div>
</Card>
) : vm.status === "error" ? (
<EmptyState
outlined
variant="error"
title={t("billing:billingPayment.error.title")}
body={t(vm.error.messageKey)}
action={
<Button variant="tertiary" onClick={actions.onRetry} data-ui-id="ui.billing.payment.retry">
{t("billing:states.retry")}
</Button>
}
/>
) : vm.status === "empty" ? (
<EmptyState
outlined
title={t("billing:billingPayment.empty.title")}
body={t("billing:billingPayment.empty.description")}
action={
<Button variant="tertiary" onClick={actions.onRetry}>
{t("billing:states.refresh")}
</Button>
}
/>
) : vm.status === "ready" ? (
<>
<div className="mobile:grid-cols-1 grid w-full grid-cols-2 gap-4">
<StatsCard
trend="neutral"
icon={<FeatherFileText />}
label={t("billing:billingPayment.summary.invoices")}
value={formatters.number(vm.data.invoices.length)}
percentage={t("billing:billingPayment.summary.lago")}
/>
<StatsCard
trend={vm.data.latestPaymentStatus === "succeeded" || vm.data.latestPaymentStatus === "paid" ? "positive" : "neutral"}
icon={<FeatherCreditCard />}
label={t("billing:billingPayment.summary.latestPayment")}
value={t(vm.data.latestPaymentStatusLabelKey)}
percentage={t(
vm.data.hasMollieReference ? "billing:billingPayment.summary.mollieConnected" : "billing:billingPayment.summary.noPaymentReference"
)}
/>
</div>
<Alert notDismissible variant="information" title={t(vm.data.helpTitleKey)} description={t(vm.data.helpDescriptionKey)} />
<Card data-ui-id="ui.billing.payment.invoices">
<div className="flex w-full flex-col items-start">
<span className="text-body-large font-body-large text-text-neutral-primary">{t("billing:billingPayment.invoices.title")}</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
{t("billing:billingPayment.invoices.description")}
</span>
</div>
<div className="w-full overflow-x-auto">
<Table
header={
<Table.HeaderRow>
<Table.HeaderCell>{t("billing:billingPayment.invoices.columns.invoice")}</Table.HeaderCell>
<Table.HeaderCell>{t("billing:billingPayment.invoices.columns.date")}</Table.HeaderCell>
<Table.HeaderCell>{t("billing:billingPayment.invoices.columns.amount")}</Table.HeaderCell>
<Table.HeaderCell>{t("billing:billingPayment.invoices.columns.invoiceStatus")}</Table.HeaderCell>
<Table.HeaderCell>{t("billing:billingPayment.invoices.columns.paymentStatus")}</Table.HeaderCell>
<Table.HeaderCell>{t("billing:billingPayment.invoices.columns.download")}</Table.HeaderCell>
</Table.HeaderRow>
}
>
{vm.data.invoices.map((invoice) => (
<Table.Row key={invoice.id}>
<Table.Cell>
<span className="text-body-small-medium-150 font-body-small-medium-150 text-text-neutral-primary">{invoice.number ?? invoice.id}</span>
</Table.Cell>
<Table.Cell>
<span className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-secondary">
{invoice.issuingDate ? formatters.date(invoice.issuingDate, { dateStyle: "medium" }) : t("shared.unknown")}
</span>
</Table.Cell>
<Table.Cell>
<span className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-secondary">
{invoice.amountCents !== null && invoice.currency
? formatters.currencyFromCents(invoice.amountCents, invoice.currency)
: t("shared.unknown")}
</span>
</Table.Cell>
<Table.Cell>
<BadgeFeedback farbe="subtle-neutral" size="small">
{t(invoice.statusLabelKey)}
</BadgeFeedback>
</Table.Cell>
<Table.Cell>
<BadgeFeedback farbe={invoiceTone(invoice.tone)} size="small">
{t(invoice.paymentStatusLabelKey)}
</BadgeFeedback>
</Table.Cell>
<Table.Cell>
{invoice.downloadHref ? (
<div className="flex items-center gap-1">
{/* Preview toggles: the same button closes the frame again. */}
<Button
variant={previewInvoiceId === invoice.id ? "secondary" : "tertiary"}
size="small"
icon={<FeatherEye />}
onClick={() => setPreviewInvoiceId(previewInvoiceId === invoice.id ? null : invoice.id)}
data-ui-id="ui.billing.payment.invoicePreview"
>
{t("billing:billingPayment.invoices.preview")}
</Button>
<Button
variant="tertiary"
size="small"
icon={<FeatherDownload />}
onClick={() => actions.onDownloadInvoice(invoice.downloadHref!)}
data-ui-id="ui.billing.payment.invoiceDownload"
>
{t("billing:billingPayment.invoices.download")}
</Button>
</div>
) : (
<span className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary">
{t("billing:billingPayment.invoices.unavailable")}
</span>
)}
</Table.Cell>
</Table.Row>
))}
</Table>
</div>
{/* Preview opens below the list and closes from its own header or
from the row button that opened it. The frame streams the
invoice route with ?disposition=inline — same permission check
and same bytes as the download. */}
{previewInvoice?.previewHref ? (
<div
className="flex w-full flex-col gap-3 border-t border-solid border-border-default pt-4"
data-ui-id="ui.billing.payment.invoicePreviewPanel"
>
<div className="flex w-full flex-wrap items-center justify-between gap-2">
<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:billingPayment.invoices.previewTitle", { invoice: previewInvoice.number ?? previewInvoice.id })}
</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
{t("billing:billingPayment.invoices.previewHint")}
</span>
</div>
<div className="flex items-center gap-2">
<Button
variant="tertiary"
size="small"
icon={<FeatherDownload />}
onClick={() => actions.onDownloadInvoice(previewInvoice.downloadHref!)}
>
{t("billing:billingPayment.invoices.download")}
</Button>
<Button
variant="tertiary"
size="small"
icon={<FeatherX />}
onClick={() => setPreviewInvoiceId(null)}
data-ui-id="ui.billing.payment.invoicePreviewClose"
>
{t("billing:billingPayment.invoices.previewClose")}
</Button>
</div>
</div>
<iframe
key={previewInvoice.id}
src={previewInvoice.previewHref}
title={t("billing:billingPayment.invoices.previewTitle", { invoice: previewInvoice.number ?? previewInvoice.id })}
className="h-[720px] max-h-[70vh] w-full rounded-md border border-solid border-border-default bg-surface-neutral-secondary"
data-ui-id="ui.billing.payment.invoicePreviewFrame"
/>
</div>
) : null}
</Card>
</>
) : null}
</PageBody>
</PageShell>
);
}