203 lines
7.4 KiB
TypeScript
203 lines
7.4 KiB
TypeScript
// R-P7 (P7 Lead-Inbox) Interim-UI (AD-28) — lead statistics (D-P7
|
|
// "Lead-Statistiken", archetype A2 + KPI-Kit), composed from ~/ui. Pure: renders
|
|
// LeadStatisticsVM (KPI tiles + by-source/by-status breakdowns) inside the shared
|
|
// PageShell/PageHeader/PageBody chrome, the same page frame the dashboard uses.
|
|
// Shares are derived from the counts the VM already carries — no invented
|
|
// aggregates. The brief's richer breakdowns (reaction time, per-owner, weekday,
|
|
// trend charts) are not yet exposed by the service and stay a tracked
|
|
// design-nachzug. No useLoaderData. Final Subframe optics = tracked design-nachzug.
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
import { FeatherBarChart2, FeatherCheckCircle, FeatherInbox, FeatherShieldOff, FeatherTrendingUp } from "@subframe/core";
|
|
import type { LeadStatBreakdownRow, LeadStatKpi, LeadStatisticsVM } from "@hivegrid/view-models";
|
|
import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell";
|
|
import { Alert } from "~/ui/components/Alert";
|
|
import { Card } from "~/ui/components/Card";
|
|
import { EmptyState } from "~/ui/components/EmptyState";
|
|
import { Loader } from "~/ui/components/Loader";
|
|
import { Progress } from "~/ui/components/Progress";
|
|
import { StatsCard } from "~/ui/components/StatsCard";
|
|
import { useLocaleFormatters } from "~/locale/format";
|
|
|
|
export type LeadStatisticsActions = {
|
|
/** Open the inbox filtered to what the tile counts. */
|
|
onOpenLeads: (kpi: LeadStatKpi) => void;
|
|
};
|
|
|
|
export type LeadStatisticsPageProps = {
|
|
vm: LeadStatisticsVM;
|
|
actions: LeadStatisticsActions;
|
|
};
|
|
|
|
const sectionHeading = "text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary";
|
|
const mutedText = "text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary";
|
|
|
|
const kpiIcons: Record<LeadStatKpi, React.ReactNode> = {
|
|
total: <FeatherInbox />,
|
|
new: <FeatherTrendingUp />,
|
|
converted: <FeatherCheckCircle />,
|
|
spam: <FeatherShieldOff />,
|
|
};
|
|
|
|
export function LeadStatisticsPage(props: LeadStatisticsPageProps) {
|
|
const { t } = useTranslation();
|
|
const formatters = useLocaleFormatters();
|
|
|
|
const header = (
|
|
<PageHeader
|
|
icon={<FeatherBarChart2 className={PAGE_HEADER_ICON_CLASS} />}
|
|
title={t("leads.statistics.title")}
|
|
subtitle={t("leads.statistics.subtitle")}
|
|
/>
|
|
);
|
|
|
|
if (props.vm.status === "loading" || props.vm.status === "idle") {
|
|
return (
|
|
<PageShell uiId="ui.leads.statistics.loading">
|
|
{header}
|
|
<PageBody className="items-center gap-4">
|
|
<Loader size="medium" />
|
|
<span className={mutedText}>{t("leads.statistics.loading")}</span>
|
|
</PageBody>
|
|
</PageShell>
|
|
);
|
|
}
|
|
|
|
if (props.vm.status === "error") {
|
|
return (
|
|
<PageShell uiId="ui.leads.statistics.error">
|
|
{header}
|
|
<PageBody>
|
|
<Alert variant="error" title={t(props.vm.error.messageKey)} />
|
|
</PageBody>
|
|
</PageShell>
|
|
);
|
|
}
|
|
|
|
const data = props.vm.status === "ready" ? props.vm.data : null;
|
|
const total = data?.kpis.find((kpi) => kpi.key === "total")?.value ?? 0;
|
|
|
|
if (!data || total === 0) {
|
|
return (
|
|
<PageShell uiId="ui.leads.statistics.empty">
|
|
{header}
|
|
<PageBody>
|
|
<EmptyState outlined title={t("leads.statistics.empty.headline")} body={t("leads.statistics.empty.body")} />
|
|
</PageBody>
|
|
</PageShell>
|
|
);
|
|
}
|
|
|
|
/** Share of all leads — the only derivation, straight from the VM counts. */
|
|
const share = (count: number) => (total > 0 ? count / total : 0);
|
|
|
|
return (
|
|
<PageShell uiId="ui.leads.statistics">
|
|
{header}
|
|
<PageBody>
|
|
<div className="grid w-full gap-4 sm:grid-cols-2 lg:grid-cols-4" data-ui-id="ui.leads.statistics.kpis">
|
|
{data.kpis.map((kpi) => (
|
|
// Each tile is a shortcut into the inbox, filtered to exactly what it
|
|
// counts — a button, so it is reachable by keyboard like any control.
|
|
<button
|
|
key={kpi.key}
|
|
type="button"
|
|
onClick={() => props.actions.onOpenLeads(kpi.key)}
|
|
className="focus-visible:shadow-focus h-full rounded-md text-left outline-none"
|
|
aria-label={t("leads.statistics.card.openInInbox", { label: t(kpi.labelKey) })}
|
|
data-ui-id={`ui.leads.statistics.kpi.${kpi.key}.link`}
|
|
>
|
|
<StatsCard
|
|
trend="neutral"
|
|
label={t(kpi.labelKey)}
|
|
value={formatters.number(kpi.value)}
|
|
comparisonText={
|
|
kpi.key === "total"
|
|
? t("leads.statistics.card.totalHint")
|
|
: t("leads.statistics.shareOfTotal", { percent: formatters.percent(share(kpi.value), { maximumFractionDigits: 0 }) })
|
|
}
|
|
icon={kpiIcons[kpi.key]}
|
|
className="hover:border-brand-brand-border-solid-hover h-full"
|
|
data-ui-id={`ui.leads.statistics.kpi.${kpi.key}`}
|
|
/>
|
|
</button>
|
|
))}
|
|
</div>
|
|
|
|
<div className="grid w-full gap-4 lg:grid-cols-2">
|
|
<BreakdownCard
|
|
uiId="ui.leads.statistics.bySource"
|
|
heading={t("leads.statistics.bySource")}
|
|
description={t("leads.statistics.bySourceDescription")}
|
|
rows={data.byQuelle}
|
|
total={total}
|
|
/>
|
|
<BreakdownCard
|
|
uiId="ui.leads.statistics.byStatus"
|
|
heading={t("leads.statistics.byStatus")}
|
|
description={t("leads.statistics.byStatusDescription")}
|
|
rows={data.byStatus}
|
|
total={total}
|
|
/>
|
|
</div>
|
|
|
|
{data.canReadAll ? null : (
|
|
<span className={mutedText} data-ui-id="ui.leads.statistics.scopeHint">
|
|
{t("leads.statistics.scopeOwnOnly")}
|
|
</span>
|
|
)}
|
|
</PageBody>
|
|
</PageShell>
|
|
);
|
|
}
|
|
|
|
function BreakdownCard({
|
|
uiId,
|
|
heading,
|
|
description,
|
|
rows,
|
|
total,
|
|
}: {
|
|
uiId: string;
|
|
heading: string;
|
|
description: string;
|
|
rows: readonly LeadStatBreakdownRow[];
|
|
total: number;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const formatters = useLocaleFormatters();
|
|
|
|
return (
|
|
<Card className="h-full" data-ui-id={uiId}>
|
|
<div className="flex w-full flex-col gap-1">
|
|
<span className={sectionHeading}>{heading}</span>
|
|
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">{description}</span>
|
|
</div>
|
|
{rows.length === 0 ? (
|
|
<span className={mutedText}>{t("leads.statistics.breakdown.empty")}</span>
|
|
) : (
|
|
<div className="flex w-full flex-col gap-4">
|
|
{rows.map((row) => (
|
|
<Progress
|
|
key={row.key}
|
|
// The shared Progress track has no own height and its indicator is
|
|
// forced to `height: 100%` by the vendor stylesheet, so the bar
|
|
// collapses to 0px unless the track gets one here.
|
|
className="w-full gap-2 [&>div:nth-of-type(2)]:h-1.5"
|
|
value={total > 0 ? Math.round((row.count / total) * 100) : 0}
|
|
label={t(row.labelKey)}
|
|
statusText={t("leads.statistics.breakdown.count", {
|
|
value: formatters.number(row.count),
|
|
percent: formatters.percent(total > 0 ? row.count / total : 0, { maximumFractionDigits: 0 }),
|
|
})}
|
|
data-ui-id={`${uiId}.row.${row.key}`}
|
|
/>
|
|
))}
|
|
</div>
|
|
)}
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
export default LeadStatisticsPage;
|