90 lines
3.6 KiB
TypeScript
90 lines
3.6 KiB
TypeScript
// AK-SET-01 Hub D — "Abrechnung" bundles the former subscription, usage and
|
|
// billing rail entries into one hub with a tab bar. Layout route only: the
|
|
// loader decides which tabs the user may see, every tab keeps its own URL and
|
|
// its own loader.
|
|
import { Outlet, useLoaderData, useLocation, type LoaderFunctionArgs, type MetaFunction } from "react-router";
|
|
import { FeatherCreditCard } from "@subframe/core";
|
|
|
|
import { buildSettingsHubVM, type SettingsHubTabInput } from "@hivegrid/view-models";
|
|
|
|
import { SettingsHubShell } from "~/components/screens/settings/SettingsHubShell";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import UrlUtils from "~/utils/app/UrlUtils";
|
|
import { useAppData } from "~/utils/data/useAppData";
|
|
import { useRootData } from "~/utils/data/useRootData";
|
|
import { requiresTenantSubscriptionActivation } from "~/utils/app/SubscriptionAccess";
|
|
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
|
import { hasSettingsPermission, settingsHubBaseRedirect, settingsHubNotFound } from "./hub-access.server";
|
|
|
|
type LoaderData = {
|
|
title: string;
|
|
tabs: SettingsHubTabInput[];
|
|
};
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const tenantId = await getTenantIdFromUrl(params);
|
|
const [{ t }, canViewSubscription, canViewInvoices] = await Promise.all([
|
|
getTranslations(request),
|
|
hasSettingsPermission(request, "app.settings.subscription.view", tenantId),
|
|
hasSettingsPermission(request, "app.settings.subscription.invoices.view", tenantId),
|
|
]);
|
|
|
|
const tabs: SettingsHubTabInput[] = [];
|
|
if (canViewSubscription) {
|
|
tabs.push({
|
|
id: "plan",
|
|
labelKey: "billing:selfServiceSubscription.management.tabs.overview",
|
|
href: UrlUtils.currentTenantUrl(params, "settings/billing"),
|
|
// The hub index href is a prefix of every sibling — without this the plan
|
|
// tab would stay lit on the usage and invoices routes.
|
|
exact: true,
|
|
});
|
|
tabs.push({ id: "usage", labelKey: "billing:usage.title", href: UrlUtils.currentTenantUrl(params, "settings/billing/usage") });
|
|
}
|
|
if (canViewInvoices) {
|
|
tabs.push({
|
|
id: "invoices",
|
|
labelKey: "app.subscription.invoices.title",
|
|
href: UrlUtils.currentTenantUrl(params, "settings/billing/invoices"),
|
|
});
|
|
}
|
|
|
|
// A hub without a single reachable tab must not render an empty frame —
|
|
// permission denied shows 404, never 403 (Bauprinzip A.5.3).
|
|
if (tabs.length === 0) throw settingsHubNotFound();
|
|
const baseRedirect = settingsHubBaseRedirect(request, UrlUtils.currentTenantUrl(params, "settings/billing"), tabs);
|
|
if (baseRedirect) throw baseRedirect;
|
|
|
|
return { title: `${t("settings.hubs.billing.title")} | ${process.env.APP_NAME}`, tabs } satisfies LoaderData;
|
|
};
|
|
|
|
export const meta: MetaFunction<typeof loader> = ({ data }) => [{ title: data?.title }];
|
|
|
|
export default function BillingHubRoute() {
|
|
const { tabs } = useLoaderData<LoaderData>();
|
|
const location = useLocation();
|
|
const appData = useAppData();
|
|
const { appConfiguration } = useRootData();
|
|
const subscriptionActivationMode =
|
|
appData.subscriptionActivationRequired ??
|
|
requiresTenantSubscriptionActivation(appConfiguration.subscription.required, appData.mySubscription);
|
|
|
|
if (subscriptionActivationMode) {
|
|
return <Outlet />;
|
|
}
|
|
|
|
const vm = buildSettingsHubVM({
|
|
hubId: "billing",
|
|
titleKey: "settings.hubs.billing.title",
|
|
subtitleKey: "settings.hubs.billing.subtitle",
|
|
tabs,
|
|
currentPath: location.pathname,
|
|
});
|
|
|
|
return (
|
|
<SettingsHubShell vm={vm} icon={<FeatherCreditCard />}>
|
|
<Outlet />
|
|
</SettingsHubShell>
|
|
);
|
|
}
|