55 lines
2.3 KiB
TypeScript
55 lines
2.3 KiB
TypeScript
import { type LoaderFunctionArgs, useLoaderData } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import { loadLeadContext, notFoundResponse, requireLeadPermission } from "~/utils/integrations/leads/leadAccess.server";
|
|
import { leadsPath, LEADS_MODULE_KEY } from "~/utils/integrations/leads/leadRoutePaths";
|
|
import { getLeadStatistics } from "~/utils/integrations/leads/leadService.server";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
|
|
type LoaderData = Awaited<ReturnType<typeof getLeadStatistics>> & { listHref: string };
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const ctx = await loadLeadContext({ request, params, requireModule: true });
|
|
try {
|
|
requireLeadPermission(ctx, "leads.read_own");
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
const stats = await getLeadStatistics({ tenantId: ctx.tenantId, ctx });
|
|
return Response.json({ ...stats, listHref: leadsPath(params.tenant ?? "") } satisfies LoaderData);
|
|
};
|
|
|
|
export default function LeadStatisticsRoute() {
|
|
const { t } = useTranslation();
|
|
const data = useLoaderData<LoaderData>();
|
|
const cards = [
|
|
["total", data.total],
|
|
["open", data.open],
|
|
["converted", data.converted],
|
|
["spam", data.spam],
|
|
] as const;
|
|
return (
|
|
<main className="mx-auto max-w-5xl px-4 py-10" data-ui-id="ui.leads.statistics">
|
|
<a href={data.listHref} className="text-theme-600 text-sm font-medium">{t("leads.actions.backToList")}</a>
|
|
<h1 className="text-foreground mt-4 text-xl font-semibold">{t("leads.statistics.title")}</h1>
|
|
<div className="mt-6 grid gap-4 sm:grid-cols-4">
|
|
{cards.map(([key, value]) => (
|
|
<section key={key} className="rounded-md border p-4">
|
|
<p className="text-muted-foreground text-sm">{t(`leads.statistics.card.${key}`)}</p>
|
|
<p className="mt-2 text-2xl font-semibold">{value}</p>
|
|
</section>
|
|
))}
|
|
</div>
|
|
<section className="mt-6 rounded-md border p-4">
|
|
<h2 className="font-medium">{t("leads.statistics.bySource")}</h2>
|
|
<ul className="mt-3 grid gap-2 text-sm">{data.byQuelle.map((row) => <li key={row.quelle}>{t(`leads.quelle.${row.quelle}`)}: {row.count}</li>)}</ul>
|
|
</section>
|
|
</main>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: LEADS_MODULE_KEY };
|