changed dashboard
This commit is contained in:
@@ -4,76 +4,87 @@ import { useMemo, useState, type ElementType } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import { AlertCircle, ArrowDownRight, ArrowUpRight, CreditCard, DollarSign, FileText, RefreshCw, Users } from "lucide-react";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { Skeleton } from "@/components/ui/skeleton";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { Skeleton } from "@/components/ui/skeleton";
|
||||
import { cn } from "@/lib/utils";
|
||||
import type { StatItem } from "@/features/dashboard/api/dashboardApi";
|
||||
import { DASHBOARD_OVERVIEW_KEY, useDashboardOverview } from "@/features/dashboard/hooks/useDashboard";
|
||||
import { ANALYTICS_KEY, useAnalyticsCustomers } from "@/features/dashboard/hooks/useAnalytics";
|
||||
import { ANALYTICS_KEY, useAnalyticsCustomers, useAnalyticsProducts } from "@/features/dashboard/hooks/useAnalytics";
|
||||
import { CustomersSection } from "@/features/dashboard/components/productsCustomersSection";
|
||||
import { formatNumber } from "@/features/dashboard/components/analyticsFormat";
|
||||
|
||||
// ─── Stat card ────────────────────────────────────────────────────────────────
|
||||
/**
|
||||
* KPI register cell dividers: a 2×2 grid on small screens, one row of four on lg.
|
||||
* Each cell owns its own rules, so the panel never needs a negative-margin trick.
|
||||
*/
|
||||
const KPI_CELL_BORDERS = [
|
||||
"border-b border-r border-border lg:border-b-0",
|
||||
"border-b border-border lg:border-b-0 lg:border-r",
|
||||
"border-r border-border",
|
||||
"",
|
||||
];
|
||||
|
||||
interface StatCardProps {
|
||||
// ─── Stat cell ────────────────────────────────────────────────────────────────
|
||||
|
||||
interface StatCellProps {
|
||||
title: string;
|
||||
stat?: StatItem;
|
||||
icon: ElementType;
|
||||
accent: string;
|
||||
footnote: string;
|
||||
comparedTo: string;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
function StatCard({ title, stat, icon: Icon, accent, footnote, comparedTo }: StatCardProps) {
|
||||
function StatCell({ title, stat, icon: Icon, footnote, comparedTo, className }: StatCellProps) {
|
||||
const change = stat?.changePercent;
|
||||
const hasChange = change !== null && change !== undefined;
|
||||
const positive = (change ?? 0) >= 0;
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<CardTitle className="text-sm font-medium text-muted-foreground">{title}</CardTitle>
|
||||
<div className={cn("flex h-9 w-9 items-center justify-center rounded-lg", accent)}>
|
||||
<Icon size={16} />
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<p className="text-3xl font-semibold tracking-tight tabular-nums">{formatNumber(stat?.count ?? 0)}</p>
|
||||
{hasChange ? (
|
||||
<p
|
||||
className={cn(
|
||||
"mt-1 flex items-center gap-1 text-xs font-medium",
|
||||
positive ? "text-emerald-600 dark:text-emerald-400" : "text-destructive",
|
||||
)}
|
||||
>
|
||||
{positive ? <ArrowUpRight size={13} /> : <ArrowDownRight size={13} />}
|
||||
{formatNumber(Math.abs(change), 1)}% {comparedTo}
|
||||
</p>
|
||||
) : (
|
||||
<p className="mt-1 flex items-center gap-1.5 text-xs text-muted-foreground">
|
||||
<span className="inline-block h-1.5 w-1.5 rounded-full bg-muted-foreground/40" />
|
||||
{footnote}
|
||||
</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
<div className={cn("px-5 py-5 lg:px-7 lg:py-6", className)}>
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<p className="text-[13px] font-medium text-muted-foreground">{title}</p>
|
||||
<Icon className="size-[18px] shrink-0 text-muted-foreground/60" aria-hidden="true" />
|
||||
</div>
|
||||
|
||||
<p className="mt-3.5 text-[34px] font-semibold leading-none tracking-tight tabular-nums">{formatNumber(stat?.count ?? 0)}</p>
|
||||
|
||||
{hasChange ? (
|
||||
<p
|
||||
className={cn(
|
||||
"mt-3 flex flex-wrap items-center gap-x-1 text-[12.5px] font-medium",
|
||||
positive ? "text-positive" : "text-destructive",
|
||||
)}
|
||||
>
|
||||
{positive ? (
|
||||
<ArrowUpRight className="size-3 shrink-0" aria-hidden="true" />
|
||||
) : (
|
||||
<ArrowDownRight className="size-3 shrink-0" aria-hidden="true" />
|
||||
)}
|
||||
<span className="tabular-nums">{formatNumber(Math.abs(change), 1)}%</span>
|
||||
<span className="font-normal text-muted-foreground">{comparedTo}</span>
|
||||
</p>
|
||||
) : (
|
||||
<p className="mt-3 flex items-center gap-2 text-[12.5px] text-muted-foreground">
|
||||
<span className="inline-block size-2 shrink-0 rounded-full bg-muted-foreground/40" aria-hidden="true" />
|
||||
{footnote}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function StatCardSkeleton() {
|
||||
function StatCellSkeleton({ className }: { className?: string }) {
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
|
||||
<Skeleton className="h-4 w-28" />
|
||||
<Skeleton className="h-9 w-9 rounded-lg" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Skeleton className="h-8 w-24" />
|
||||
<Skeleton className="mt-2 h-3 w-36" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
<div className={cn("px-5 py-5 lg:px-7 lg:py-6", className)}>
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<Skeleton className="h-3.5 w-24" />
|
||||
<Skeleton className="size-[18px] rounded-sm" />
|
||||
</div>
|
||||
<Skeleton className="mt-3.5 h-7 w-24" />
|
||||
<Skeleton className="mt-2.5 h-3 w-32" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -89,8 +100,9 @@ export default function DashboardPage({ isLoading: propLoading = false }: { isLo
|
||||
|
||||
const overview = useDashboardOverview({ year: selectedYear });
|
||||
const customers = useAnalyticsCustomers(analyticsParams);
|
||||
const products = useAnalyticsProducts(analyticsParams);
|
||||
|
||||
const isFetching = overview.isFetching || customers.isFetching;
|
||||
const isFetching = overview.isFetching || customers.isFetching || products.isFetching;
|
||||
const showStatSkeleton = propLoading || overview.isLoading;
|
||||
|
||||
const handleRefresh = () => {
|
||||
@@ -102,78 +114,83 @@ export default function DashboardPage({ isLoading: propLoading = false }: { isLo
|
||||
const comparedTo = t("dashboard.comparedToLastMonth", "geçen aýa garanyňda");
|
||||
const stats = overview.data?.stats;
|
||||
|
||||
const statCards: Omit<StatCardProps, "comparedTo">[] = [
|
||||
const statCards: Omit<StatCellProps, "comparedTo" | "className">[] = [
|
||||
{
|
||||
title: t("dashboard.loanOrders", "Karz sargytlary"),
|
||||
stat: stats?.loanOrders,
|
||||
icon: FileText,
|
||||
accent: "bg-[var(--chart-1)]/12 text-[var(--chart-1)]",
|
||||
footnote: t("dashboard.totalLoanOrders", "Jemi karz sargytlar"),
|
||||
},
|
||||
{
|
||||
title: t("dashboard.activeCards", "Işjeň kartlar"),
|
||||
stat: stats?.activeCards,
|
||||
icon: CreditCard,
|
||||
accent: "bg-[var(--chart-3)]/12 text-[var(--chart-3)]",
|
||||
footnote: t("dashboard.totalActiveCards", "Işjeň kartlaryň sany"),
|
||||
},
|
||||
{
|
||||
title: t("dashboard.activeClients", "Işjeň müşderiler"),
|
||||
stat: stats?.activeCustomers,
|
||||
icon: Users,
|
||||
accent: "bg-[var(--chart-4)]/12 text-[var(--chart-4)]",
|
||||
footnote: t("dashboard.totalActiveCustomers", "Hasaba alnan müşderiler"),
|
||||
},
|
||||
{
|
||||
title: t("dashboard.onlinePayments", "Onlaýn tölegler"),
|
||||
stat: stats?.onlinePayments,
|
||||
icon: DollarSign,
|
||||
accent: "bg-[var(--chart-2)]/12 text-[var(--chart-2)]",
|
||||
footnote: t("dashboard.totalOnlinePayments", "Amala aşyrylan tölegler"),
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<div className="w-full space-y-6">
|
||||
<div className="w-full space-y-3.5">
|
||||
{/* Header */}
|
||||
<div className="flex flex-col justify-between gap-4 sm:flex-row sm:items-center">
|
||||
<div className="flex flex-col justify-between gap-4 sm:flex-row sm:items-end">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold tracking-tight">{t("dashboard.title", "Baş sahypa")}</h1>
|
||||
<p className="mt-0.5 text-sm text-muted-foreground">{t("dashboard.overviewSubtitle", "Ähli amallaryň gysgaça görünüşi")}</p>
|
||||
<h1 className="text-[28px] font-semibold tracking-tight">{t("dashboard.title", "Baş sahypa")}</h1>
|
||||
<p className="mt-2 text-[13.5px] text-muted-foreground">{t("dashboard.overviewSubtitle", "Ähli amallaryň gysgaça görünüşi")}</p>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2 self-start sm:self-auto">
|
||||
<div className="flex items-center self-start rounded-md border border-border bg-card p-0.5 sm:self-auto">
|
||||
<Select value={String(selectedYear)} onValueChange={(val) => setSelectedYear(Number(val))}>
|
||||
<SelectTrigger className="h-9 w-[120px] cursor-pointer">
|
||||
<SelectValue placeholder={String(selectedYear)} />
|
||||
<SelectTrigger
|
||||
aria-label={t("common.year", "ýyl")}
|
||||
className="h-9 w-[110px] cursor-pointer rounded-[5px] border-0 bg-transparent px-2.5 text-[13.5px] shadow-none focus-visible:border-transparent focus-visible:ring-0"
|
||||
>
|
||||
<SelectValue />
|
||||
<span className="text-[11.5px] text-muted-foreground" aria-hidden="true">
|
||||
{t("common.year", "ýyl")}
|
||||
</span>
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectContent align="end">
|
||||
{yearOptions.map((y) => (
|
||||
<SelectItem key={y} value={String(y)} className="cursor-pointer">
|
||||
{y} {t("common.year", "ýyl")}
|
||||
<SelectItem key={y} value={String(y)} className="cursor-pointer tabular-nums">
|
||||
{y}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
|
||||
<span className="h-4 w-px bg-border" aria-hidden="true" />
|
||||
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
size="icon-sm"
|
||||
onClick={handleRefresh}
|
||||
disabled={isFetching}
|
||||
aria-label={t("common.refresh", "Täzele")}
|
||||
title={t("common.refresh", "Täzele")}
|
||||
className="h-9 w-9 cursor-pointer"
|
||||
className="rounded-[5px]"
|
||||
>
|
||||
<RefreshCw className={cn("h-4 w-4", isFetching && "animate-spin")} />
|
||||
<RefreshCw className={cn("size-4", isFetching && "animate-spin")} />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Stat cards */}
|
||||
{/* KPI register */}
|
||||
{overview.isError && !showStatSkeleton ? (
|
||||
<div className="flex w-full flex-col items-center justify-center space-y-4 rounded-xl border bg-card p-12 text-center">
|
||||
<div className="flex h-12 w-12 items-center justify-center rounded-full bg-destructive/10 text-destructive">
|
||||
<AlertCircle className="h-6 w-6" />
|
||||
<div className="flex w-full flex-col items-center justify-center space-y-4 rounded-panel border border-border bg-card p-12 text-center">
|
||||
<div className="flex size-12 items-center justify-center rounded-full bg-destructive/10 text-destructive">
|
||||
<AlertCircle className="size-6" aria-hidden="true" />
|
||||
</div>
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold">{t("dashboard.errorTitle", "Maglumatlary ýükläp bolmady")}</h3>
|
||||
@@ -184,15 +201,15 @@ export default function DashboardPage({ isLoading: propLoading = false }: { isLo
|
||||
</p>
|
||||
</div>
|
||||
<Button onClick={() => overview.refetch()} variant="outline" className="gap-2">
|
||||
<RefreshCw className="h-4 w-4" />
|
||||
<RefreshCw className="size-4" aria-hidden="true" />
|
||||
{t("common.retry", "Täzeden synanyş")}
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid grid-cols-2 gap-4 lg:grid-cols-4">
|
||||
<div className="grid grid-cols-2 overflow-hidden rounded-panel border border-border bg-card lg:grid-cols-4">
|
||||
{showStatSkeleton
|
||||
? Array.from({ length: 4 }, (_, i) => <StatCardSkeleton key={i} />)
|
||||
: statCards.map((card) => <StatCard key={card.title} {...card} comparedTo={comparedTo} />)}
|
||||
? Array.from({ length: 4 }, (_, i) => <StatCellSkeleton key={i} className={KPI_CELL_BORDERS[i]} />)
|
||||
: statCards.map((card, i) => <StatCell key={card.title} {...card} comparedTo={comparedTo} className={KPI_CELL_BORDERS[i]} />)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -202,6 +219,12 @@ export default function DashboardPage({ isLoading: propLoading = false }: { isLo
|
||||
isLoading={customers.isLoading}
|
||||
isError={customers.isError}
|
||||
onRetry={() => customers.refetch()}
|
||||
products={{
|
||||
data: products.data,
|
||||
isLoading: products.isLoading,
|
||||
isError: products.isError,
|
||||
onRetry: () => products.refetch(),
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user