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> & { 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(); const cards = [ ["total", data.total], ["open", data.open], ["converted", data.converted], ["spam", data.spam], ] as const; return (
{t("leads.actions.backToList")}

{t("leads.statistics.title")}

{cards.map(([key, value]) => (

{t(`leads.statistics.card.${key}`)}

{value}

))}

{t("leads.statistics.bySource")}

    {data.byQuelle.map((row) =>
  • {t(`leads.quelle.${row.quelle}`)}: {row.count}
  • )}
); } export function ErrorBoundary() { return ; } export const handle = { moduleKey: LEADS_MODULE_KEY };