// R-P7 (P7 Lead-Inbox) — lead statistics route (`/leads-statistics`, reached via // the leads sub-sidenav). Loader builds the LeadStatisticsVM from // getLeadStatistics (permission-aware). Missing leads.read_own -> 404. UI is the // AD-28 interim LeadStatisticsPage. import { type LoaderFunctionArgs, useLoaderData, useNavigate, useParams } from "react-router"; import { buildLeadStatisticsVM, type LeadStatisticsVM } from "@hivegrid/view-models"; import { loadLeadContext, notFoundResponse, requireLeadPermission } from "~/utils/integrations/leads/leadAccess.server"; import { leadsPath, LEADS_MODULE_KEY } from "~/utils/integrations/leads/leadRoutePaths"; import { leadStatisticsKpiParams } from "~/utils/integrations/leads/leadListQuery"; import { getLeadStatistics } from "~/utils/integrations/leads/leadService.server"; import { LeadStatisticsPage } from "~/components/screens/leads/LeadStatisticsPage"; import ServerError from "~/components/ui/errors/ServerError"; import { getTranslations } from "~/locale/i18next.server"; type LoaderData = { title: string; vm: LeadStatisticsVM }; 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 }); const listHref = leadsPath(params.tenant ?? ""); const vm = buildLeadStatisticsVM({ stats, canReadAll: ctx.permissions.includes("leads.read_all"), listHref }); const { t } = await getTranslations(request); return Response.json({ title: t("leads.statistics.title"), vm } satisfies LoaderData); }; export default function LeadStatisticsRoute() { const { vm } = useLoaderData(); const navigate = useNavigate(); const params = useParams(); return ( inbox, pre-filtered. The inbox reads the query back into its // dropdowns, so the filter that got applied stays visible there. onOpenLeads: (kpi) => navigate(`${leadsPath(params.tenant ?? "")}?${leadStatisticsKpiParams(kpi).toString()}`), }} /> ); } export function ErrorBoundary() { return ; } export const handle = { moduleKey: LEADS_MODULE_KEY };