63 lines
3.1 KiB
TypeScript
63 lines
3.1 KiB
TypeScript
import { LoaderFunctionArgs, redirect, useLoaderData } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import EditPageLayout from "~/components/ui/layouts/EditPageLayout";
|
|
import SettingSection from "~/components/ui/sections/SettingSection";
|
|
import { tenantHasModule } from "~/utils/connector/tenantConnector.server";
|
|
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
|
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
|
import { getUserInfo } from "~/utils/session.server";
|
|
import { appTenantPlanRequiredPath } from "~/utils/app/AppTenantStatusPaths";
|
|
import { BILLING_MODULE_KEY, getBillingUsageSummary } from "~/utils/services/.server/billingStatusService";
|
|
|
|
type LoaderData = Awaited<ReturnType<typeof getBillingUsageSummary>>;
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const tenantId = await getTenantIdFromUrl(params);
|
|
await verifyUserHasPermission(request, "app.settings.subscription.view", tenantId);
|
|
const userInfo = await getUserInfo(request);
|
|
const hasBillingModule = await tenantHasModule({ tenantId, userId: userInfo.userId, moduleKey: BILLING_MODULE_KEY });
|
|
if (!hasBillingModule) {
|
|
throw redirect(appTenantPlanRequiredPath(params));
|
|
}
|
|
return await getBillingUsageSummary(tenantId);
|
|
};
|
|
|
|
export default function BillingUsageRoute() {
|
|
const { t } = useTranslation("billing");
|
|
const data = useLoaderData<LoaderData>();
|
|
|
|
return (
|
|
<EditPageLayout title={t("usage.title")}>
|
|
<SettingSection title={t("usage.currentPeriod")} description={t("usage.description")}>
|
|
<dl className="grid gap-3 sm:grid-cols-2">
|
|
{data.metrics.length === 0 ? (
|
|
<div className="text-muted-foreground rounded-md border p-4 text-sm">{t("usage.empty")}</div>
|
|
) : (
|
|
data.metrics.map((metric) => (
|
|
<div key={metric.metricCode} className="rounded-md border p-4">
|
|
<dt className="text-muted-foreground text-sm">{t(`usage.metrics.${metric.metricCode}`)}</dt>
|
|
<dd className="text-2xl font-semibold">{metric.quantity}</dd>
|
|
{metric.quota === null ? (
|
|
<p className="text-muted-foreground mt-2 text-sm">{t("usage.unlimited")}</p>
|
|
) : (
|
|
<div className="mt-3 space-y-2">
|
|
<div className="text-muted-foreground flex items-center justify-between text-xs">
|
|
<span>{t("usage.quota", { quota: metric.quota })}</span>
|
|
<span>{t("usage.remaining", { remaining: metric.remaining })}</span>
|
|
</div>
|
|
<div className="bg-muted h-2 overflow-hidden rounded">
|
|
<div className={metric.exceeded ? "h-full bg-destructive" : "bg-primary h-full"} style={{ width: `${metric.percentUsed ?? 0}%` }} />
|
|
</div>
|
|
{metric.exceeded ? <p className="text-destructive text-sm">{t("usage.limitExceeded")}</p> : null}
|
|
</div>
|
|
)}
|
|
</div>
|
|
))
|
|
)}
|
|
</dl>
|
|
</SettingSection>
|
|
</EditPageLayout>
|
|
);
|
|
}
|