changed dashboard
This commit is contained in:
1 parent
1ebc133d1c
commit
cbd17608c1
13 files changed
+1125
-547
No files matched your search
@@ -0,0 +1,338 @@
|
||||
import type { ReactNode } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import type { TFunction } from "i18next";
|
||||
import { Info } from "lucide-react";
|
||||
import { Cell, Pie, PieChart } from "recharts";
|
||||
import { Card, CardContent } from "@/components/ui/card";
|
||||
import { Skeleton } from "@/components/ui/skeleton";
|
||||
import { ChartContainer, ChartTooltip, ChartTooltipContent } from "@/components/ui/chart";
|
||||
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
||||
import type { ApprovalCount, CountItem, CustomersData, ProductsData } from "../api/analyticsApi";
|
||||
import { BarList, EmptyState, SectionCard, type BarListRow } from "./analyticsShared";
|
||||
import { formatNumber, formatPercent, useLocalizedName } from "./analyticsFormat";
|
||||
|
||||
interface QueryProps<T> {
|
||||
data?: T;
|
||||
isLoading: boolean;
|
||||
isError: boolean;
|
||||
onRetry: () => void;
|
||||
}
|
||||
|
||||
// One meaning per color across the dashboard: blue = loans, green = cards / good, amber = waiting, red = rejected.
|
||||
const LOAN_COLOR = "var(--chart-1)";
|
||||
const CARD_COLOR = "var(--chart-2)";
|
||||
const GOOD_COLOR = "var(--chart-2)";
|
||||
const WAIT_COLOR = "var(--chart-4)";
|
||||
const BAD_COLOR = "var(--chart-5)";
|
||||
|
||||
/** Below this many decided orders an approval rate is shown but flagged as unreliable. */
|
||||
const MIN_DECIDED = 5;
|
||||
|
||||
/** "3 sargyt · 60%" — count first, share muted. */
|
||||
function CountShare({ count, percent }: { count: number; percent: number }) {
|
||||
const { t } = useTranslation();
|
||||
return (
|
||||
<>
|
||||
<span className="font-semibold">
|
||||
{formatNumber(count)} {t("analytics.kpi.orders", "sargyt")}
|
||||
</span>
|
||||
<span className="text-muted-foreground"> · {formatNumber(percent, 1)}%</span>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function InfoHint({ children }: { children: ReactNode }) {
|
||||
return (
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<button type="button" className="inline-flex cursor-help text-muted-foreground hover:text-foreground">
|
||||
<Info className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent className="max-w-xs leading-relaxed">{children}</TooltipContent>
|
||||
</Tooltip>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── Donut with the total in the middle ──────────────────────────────────────
|
||||
|
||||
interface DonutRow {
|
||||
key: string;
|
||||
label: string;
|
||||
color: string;
|
||||
}
|
||||
|
||||
function TotalDonut({ items, rows: rowDefs }: { items: CountItem[]; rows: DonutRow[] }) {
|
||||
const { t } = useTranslation();
|
||||
const byKey = new Map(items.map((it) => [it.key, it]));
|
||||
// Fixed row order and colors, so "unpaid" is always amber and never swaps with "paid".
|
||||
const known = new Set(rowDefs.map((r) => r.key));
|
||||
const extra = items.filter((it) => !known.has(it.key)).map((it) => ({ key: it.key, label: it.key, color: "var(--muted-foreground)" }));
|
||||
const rows = [...rowDefs, ...extra].map((r) => ({
|
||||
...r,
|
||||
count: byKey.get(r.key)?.count ?? 0,
|
||||
percent: byKey.get(r.key)?.percent ?? 0,
|
||||
}));
|
||||
const total = rows.reduce((s, r) => s + r.count, 0);
|
||||
if (total === 0) return <EmptyState height={140} />;
|
||||
const config = Object.fromEntries(rows.map((r) => [r.label, { label: r.label, color: r.color }]));
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-5">
|
||||
<div className="relative h-[132px] w-[132px] shrink-0">
|
||||
<ChartContainer config={config} className="h-full w-full">
|
||||
<PieChart>
|
||||
<Pie data={rows} dataKey="count" nameKey="label" innerRadius={44} outerRadius={64} strokeWidth={2} stroke="var(--card)">
|
||||
{rows.map((r) => (
|
||||
<Cell key={r.key} fill={r.color} />
|
||||
))}
|
||||
</Pie>
|
||||
<ChartTooltip content={<ChartTooltipContent nameKey="label" hideLabel />} />
|
||||
</PieChart>
|
||||
</ChartContainer>
|
||||
<div className="pointer-events-none absolute inset-0 flex flex-col items-center justify-center">
|
||||
<span className="text-2xl font-semibold tabular-nums leading-none">{formatNumber(total)}</span>
|
||||
<span className="mt-1 text-[11px] text-muted-foreground">{t("analytics.kpi.orders", "sargyt")}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="min-w-0 flex-1 space-y-3">
|
||||
{rows.map((r) => (
|
||||
<div key={r.key} className="space-y-0.5">
|
||||
<div className="flex items-center gap-2 text-sm">
|
||||
<span className="h-2.5 w-2.5 shrink-0 rounded-full" style={{ background: r.color }} />
|
||||
<span className="truncate">{r.label}</span>
|
||||
</div>
|
||||
<div className="pl-[18px] text-sm tabular-nums">
|
||||
<CountShare count={r.count} percent={r.percent} />
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── Products ─────────────────────────────────────────────────────────────────
|
||||
|
||||
export function ProductsSection({ data, isLoading, isError, onRetry }: QueryProps<ProductsData>) {
|
||||
const { t } = useTranslation();
|
||||
const localized = useLocalizedName();
|
||||
const common = { isLoading, isError, onRetry, skeletonHeight: 150 };
|
||||
|
||||
const toRows = (items: ProductsData["loanTypes"]): BarListRow[] =>
|
||||
items.map((it) => ({
|
||||
key: String(it.id),
|
||||
label: localized(it.name),
|
||||
value: it.count,
|
||||
share: it.percent,
|
||||
valueText: <CountShare count={it.count} percent={it.percent} />,
|
||||
}));
|
||||
|
||||
return (
|
||||
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-4">
|
||||
<SectionCard
|
||||
title={t("analytics.products.sources", "Karz: Web we mobil")}
|
||||
description={t("analytics.products.sourcesDesc", "Karz sargytlary nireden iberildi")}
|
||||
{...common}
|
||||
>
|
||||
<TotalDonut
|
||||
items={data?.loanSources ?? []}
|
||||
rows={[
|
||||
{ key: "mobile", label: t("analytics.source.mobile", "Mobil"), color: LOAN_COLOR },
|
||||
{ key: "web", label: t("analytics.source.web", "Web"), color: "var(--chart-3)" },
|
||||
]}
|
||||
/>
|
||||
</SectionCard>
|
||||
<SectionCard
|
||||
title={t("analytics.products.cardPaid", "Kart sargytlary: töleg")}
|
||||
description={t("analytics.products.cardPaidDesc", "Kart sargytlarynyň näçesi tölendi")}
|
||||
{...common}
|
||||
>
|
||||
<TotalDonut
|
||||
items={data?.cardOrderPay ?? []}
|
||||
rows={[
|
||||
{ key: "paid", label: t("analytics.paid", "Tölenen"), color: GOOD_COLOR },
|
||||
{ key: "unpaid", label: t("analytics.unpaid", "Tölenmedik"), color: WAIT_COLOR },
|
||||
]}
|
||||
/>
|
||||
</SectionCard>
|
||||
<SectionCard
|
||||
title={t("analytics.products.loanTypes", "Karz görnüşleri")}
|
||||
description={t("analytics.products.loanTypesDesc", "Her görnüşiň ähli karz sargytlaryndaky paýy")}
|
||||
{...common}
|
||||
>
|
||||
<BarList rows={toRows(data?.loanTypes ?? [])} color={LOAN_COLOR} hideEmpty />
|
||||
</SectionCard>
|
||||
<SectionCard
|
||||
title={t("analytics.products.cardTypes", "Kart görnüşleri")}
|
||||
description={t("analytics.products.cardTypesDesc", "Her görnüşiň ähli kart sargytlaryndaky paýy")}
|
||||
{...common}
|
||||
>
|
||||
<BarList rows={toRows(data?.cardTypes ?? [])} color={CARD_COLOR} hideEmpty />
|
||||
</SectionCard>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ─── Customers ────────────────────────────────────────────────────────────────
|
||||
|
||||
/** Approved / rejected / waiting counts as colored dots, then the approval rate. */
|
||||
function Outcome({ item, t }: { item: ApprovalCount; t: TFunction }) {
|
||||
const decided = item.approved + item.rejected;
|
||||
const parts = [
|
||||
{ n: item.approved, color: GOOD_COLOR, label: t("analytics.outcome.approved", "tassyklandy") },
|
||||
{ n: item.rejected, color: BAD_COLOR, label: t("analytics.outcome.rejected", "ret edildi") },
|
||||
{ n: item.open, color: WAIT_COLOR, label: t("analytics.outcome.open", "garaşýar") },
|
||||
].filter((p) => p.n > 0);
|
||||
|
||||
const fewData = decided > 0 && decided < MIN_DECIDED;
|
||||
|
||||
// Two fixed columns: counts on the left, approval rate pinned right — so every row lines up the same way.
|
||||
return (
|
||||
<div className="grid grid-cols-[minmax(0,1fr)_auto] items-center gap-3">
|
||||
<span className="flex min-w-0 flex-wrap items-center gap-x-2.5 gap-y-0.5">
|
||||
{parts.map((p) => (
|
||||
<span key={p.label} className="inline-flex items-center gap-1 whitespace-nowrap">
|
||||
<span className="h-1.5 w-1.5 rounded-full" style={{ background: p.color }} />
|
||||
{formatNumber(p.n)} {p.label}
|
||||
</span>
|
||||
))}
|
||||
</span>
|
||||
{decided === 0 ? (
|
||||
<span className="whitespace-nowrap">{t("analytics.outcome.noDecision", "Karar entek ýok")}</span>
|
||||
) : (
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<span className="inline-flex cursor-help items-baseline gap-1 whitespace-nowrap tabular-nums">
|
||||
<span
|
||||
className={
|
||||
fewData
|
||||
? "font-medium text-muted-foreground underline decoration-dotted underline-offset-2"
|
||||
: "font-semibold text-foreground"
|
||||
}
|
||||
>
|
||||
{formatPercent(item.approvalRate)}
|
||||
</span>
|
||||
<span className="text-muted-foreground">
|
||||
{formatNumber(item.approved)}/{formatNumber(decided)}
|
||||
</span>
|
||||
</span>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent className="max-w-xs leading-relaxed">
|
||||
{fewData
|
||||
? t("analytics.outcome.fewDataHint", "Az maglumat: diňe {{count}} karar bar, göterim takyk däl", { count: decided })
|
||||
: t("analytics.outcome.rateHint", "Tassyklama derejesi: tassyklanan / karar berlen")}
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function approvalRows(items: ApprovalCount[], labelFor: (key: string) => string, t: TFunction): BarListRow[] {
|
||||
return items.map((it) => ({
|
||||
key: it.key,
|
||||
label: labelFor(it.key),
|
||||
value: it.count,
|
||||
share: it.percent,
|
||||
valueText: <CountShare count={it.count} percent={it.percent} />,
|
||||
sub: <Outcome item={it} t={t} />,
|
||||
}));
|
||||
}
|
||||
|
||||
function SummaryStat({ label, value, color, hint }: { label: string; value: string; color?: string; hint?: ReactNode }) {
|
||||
return (
|
||||
<div className="min-w-0 space-y-1">
|
||||
<p className="flex items-center gap-1.5 text-xs text-muted-foreground">
|
||||
{color && <span className="h-2 w-2 shrink-0 rounded-full" style={{ background: color }} />}
|
||||
<span className="truncate">{label}</span>
|
||||
{hint && <InfoHint>{hint}</InfoHint>}
|
||||
</p>
|
||||
<p className="text-2xl font-semibold tabular-nums tracking-tight">{value}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function CustomersSummary({ data, isLoading }: { data?: CustomersData; isLoading: boolean }) {
|
||||
const { t } = useTranslation();
|
||||
const rateHint = t(
|
||||
"analytics.customers.approvalHint",
|
||||
"Tassyklama derejesi = tassyklanan ÷ (tassyklanan + ret edilen). Garaşýan sargytlar hasaba alynmaýar.",
|
||||
);
|
||||
return (
|
||||
<Card>
|
||||
<CardContent className="grid grid-cols-2 gap-x-6 gap-y-4 sm:grid-cols-3 lg:grid-cols-5">
|
||||
{isLoading ? (
|
||||
Array.from({ length: 5 }, (_, i) => <Skeleton key={i} className="h-12 w-full" />)
|
||||
) : (
|
||||
<>
|
||||
<SummaryStat label={t("analytics.customers.totalOrders", "Jemi karz sargytlary")} value={formatNumber(data?.total)} />
|
||||
<SummaryStat
|
||||
label={t("analytics.outcome.approvedTitle", "Tassyklandy")}
|
||||
value={formatNumber(data?.approved)}
|
||||
color={GOOD_COLOR}
|
||||
/>
|
||||
<SummaryStat
|
||||
label={t("analytics.outcome.rejectedTitle", "Ret edildi")}
|
||||
value={formatNumber(data?.rejected)}
|
||||
color={BAD_COLOR}
|
||||
/>
|
||||
<SummaryStat label={t("analytics.outcome.openTitle", "Garaşýar")} value={formatNumber(data?.open)} color={WAIT_COLOR} />
|
||||
<SummaryStat
|
||||
label={t("analytics.customers.approvalRate", "Tassyklama derejesi")}
|
||||
value={formatPercent(data?.approvalRate)}
|
||||
hint={rateHint}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
export function CustomersSection({ data, isLoading, isError, onRetry }: QueryProps<CustomersData>) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
// One error card instead of four identical ones.
|
||||
if (isError) {
|
||||
return <SectionCard title={t("analytics.customers.title", "Müşderiler")} isError onRetry={onRetry} skeletonHeight={160} />;
|
||||
}
|
||||
|
||||
const unknown = t("analytics.unknown", "Näbelli");
|
||||
const humanize = (k: string) => k.charAt(0).toUpperCase() + k.slice(1).toLowerCase().replace(/_/g, " ");
|
||||
const ageLabel = (k: string) =>
|
||||
k === "under25" ? t("analytics.age.under25", "25 ýaşdan kiçi") : k === "unknown" ? unknown : `${k} ${t("analytics.age.years", "ýaş")}`;
|
||||
const educationLabel = (k: string) => (k === "unknown" ? unknown : t(`options.education.${k}`, humanize(k)));
|
||||
const marriageLabel = (k: string) => (k === "unknown" ? unknown : t(`options.marriage.${k}`, humanize(k)));
|
||||
const guarantorLabel = (k: string) =>
|
||||
({
|
||||
none: t("analytics.guarantors.none", "Zamunsyz"),
|
||||
one: t("analytics.guarantors.one", "1 zamun"),
|
||||
two: t("analytics.guarantors.two", "2 zamun"),
|
||||
})[k] ?? k;
|
||||
|
||||
const cards: { title: string; items?: ApprovalCount[]; label: (k: string) => string }[] = [
|
||||
{ title: t("analytics.customers.age", "Ýaş toparlary"), items: data?.ageGroups, label: ageLabel },
|
||||
{ title: t("analytics.customers.education", "Bilimi"), items: data?.education, label: educationLabel },
|
||||
{ title: t("analytics.customers.marriage", "Maşgala ýagdaýy"), items: data?.marriageStatus, label: marriageLabel },
|
||||
{ title: t("analytics.customers.guarantors", "Zamunlar"), items: data?.guarantors, label: guarantorLabel },
|
||||
];
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{t("analytics.customers.desc", "Saýlanan döwürde karz sargydyny berenleriň profili")}
|
||||
</p>
|
||||
<CustomersSummary data={data} isLoading={isLoading} />
|
||||
</div>
|
||||
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 2xl:grid-cols-4">
|
||||
{cards.map((c) => (
|
||||
<SectionCard key={c.title} title={c.title} isLoading={isLoading} skeletonHeight={180}>
|
||||
<BarList rows={approvalRows(c.items ?? [], c.label, t)} color={LOAN_COLOR} />
|
||||
</SectionCard>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user