474 lines
21 KiB
TypeScript
474 lines
21 KiB
TypeScript
'use client';
|
|
|
|
import {
|
|
ArrowRight,
|
|
TrendingUp,
|
|
Filter,
|
|
Users,
|
|
Activity,
|
|
PackageCheck,
|
|
ShoppingBag,
|
|
} from 'lucide-react';
|
|
import {
|
|
ComposedChart,
|
|
Bar,
|
|
Area,
|
|
XAxis,
|
|
YAxis,
|
|
Tooltip,
|
|
ResponsiveContainer,
|
|
CartesianGrid,
|
|
} from 'recharts';
|
|
import {
|
|
DashboardRevenuePoint,
|
|
DashboardMonthlyPoint,
|
|
} from '@/services/api/adminService';
|
|
import { cn } from '@/lib/utils';
|
|
import Link from 'next/link';
|
|
import { DashboardHeaderNav } from '@/components/dashboard/DashboardHeaderNav';
|
|
import { TopSellingProductsCard } from '@/components/dashboard/TopSellingProductsCard';
|
|
import { useDashboardData } from '@/components/dashboard/useDashboardData';
|
|
import { isRouteAllowed } from '@/lib/rbac';
|
|
import {
|
|
CARD,
|
|
PIPELINE,
|
|
OVERVIEW,
|
|
DONUT_COLORS,
|
|
PAYMENT_TAG,
|
|
PRODUCT_TONES,
|
|
formatCompact,
|
|
formatInr,
|
|
initials,
|
|
samplePoints,
|
|
CardTitle,
|
|
PeriodPills,
|
|
GhostSelect,
|
|
DeltaPill,
|
|
StatusDonut,
|
|
} from '@/components/dashboard/DashboardShared';
|
|
|
|
export default function MainDashboardPage() {
|
|
const {
|
|
stats: displayStats,
|
|
sessions,
|
|
loading,
|
|
refreshing,
|
|
userRole,
|
|
chartMode,
|
|
setChartMode,
|
|
fetchData,
|
|
activity,
|
|
} = useDashboardData();
|
|
|
|
const byStatus = displayStats.orders_by_status ?? {};
|
|
const countOf = (key: string) => byStatus[key] ?? 0;
|
|
const totalOrders = displayStats.total_orders ?? 0;
|
|
const delivered = countOf('delivered');
|
|
const conversion = totalOrders > 0 ? (delivered / totalOrders) * 100 : 0;
|
|
|
|
const daily: DashboardRevenuePoint[] = displayStats.revenue_chart_daily ?? [];
|
|
const monthly: DashboardMonthlyPoint[] = displayStats.revenue_chart_monthly ?? [];
|
|
const sourceChart: Array<DashboardRevenuePoint | DashboardMonthlyPoint> =
|
|
chartMode === 'weekly' ? samplePoints(daily, 12) : monthly;
|
|
|
|
const rawPoints = sourceChart.map((row) => ({
|
|
name:
|
|
'date' in row
|
|
? row.date?.replace('08-', '') ?? ''
|
|
: row.label?.slice(0, 3) ?? '',
|
|
revenue: row.revenue,
|
|
orders: row.orders,
|
|
}));
|
|
const maxRev = Math.max(...rawPoints.map((row) => row.revenue), 1);
|
|
const maxOrd = Math.max(...rawPoints.map((row) => row.orders), 1);
|
|
const salesScale = (maxRev * 0.72) / maxOrd;
|
|
const chartPoints = rawPoints.map((row) => ({
|
|
...row,
|
|
sales: row.orders * salesScale,
|
|
}));
|
|
|
|
const pipelineRows = PIPELINE.map((stage) => ({
|
|
...stage,
|
|
count: countOf(stage.key),
|
|
value: countOf(stage.key) * (displayStats.revenue_all_time / Math.max(totalOrders, 1)),
|
|
}));
|
|
const maxPipeline = Math.max(...pipelineRows.map((row) => row.count), 1);
|
|
|
|
const overviewRows = OVERVIEW.map((row) => ({
|
|
...row,
|
|
count: countOf(row.key),
|
|
}));
|
|
const overviewTotal = overviewRows.reduce((sum, row) => sum + row.count, 0) || 1;
|
|
|
|
const donutSlices = [
|
|
{ label: 'Delivered', value: countOf('delivered'), color: DONUT_COLORS.delivered },
|
|
{ label: 'Shipped', value: countOf('shipped'), color: DONUT_COLORS.shipped },
|
|
{ label: 'Processing', value: countOf('processing') + countOf('confirmed'), color: DONUT_COLORS.processing },
|
|
{ label: 'Pending', value: countOf('pending') + countOf('cancelled') + countOf('returned'), color: DONUT_COLORS.pending },
|
|
].filter((slice) => slice.value > 0);
|
|
|
|
const spark = daily.slice(-14).map((row) => row.orders);
|
|
const sparkMax = Math.max(...spark, 1);
|
|
const customerAvatars = (displayStats.recent_orders ?? []).slice(0, 4);
|
|
|
|
if (loading) {
|
|
return (
|
|
<div className="flex flex-col gap-4 animate-pulse">
|
|
<div className="h-8 w-48 bg-muted crm-radius-section" />
|
|
<div className="grid grid-cols-1 lg:grid-cols-3 gap-4">
|
|
<div className="lg:col-span-2 h-72 bg-muted crm-radius-section" />
|
|
<div className="h-72 bg-muted crm-radius-section" />
|
|
</div>
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-4">
|
|
{[...Array(4)].map((_, i) => (
|
|
<div key={i} className="h-28 bg-muted crm-radius-section" />
|
|
))}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="flex flex-col gap-4">
|
|
<DashboardHeaderNav
|
|
title="Main Overview Dashboard"
|
|
refreshing={refreshing}
|
|
onRefresh={() => fetchData(true)}
|
|
/>
|
|
|
|
{/* Top Analytics Row */}
|
|
<div className="grid grid-cols-1 lg:grid-cols-3 gap-4">
|
|
{/* Revenue Analytics Chart */}
|
|
<div className={cn(CARD, 'lg:col-span-2 p-5 flex flex-col')}>
|
|
<div className="flex flex-wrap items-start justify-between gap-3">
|
|
<CardTitle>Revenue Analytics</CardTitle>
|
|
<PeriodPills value={chartMode} onChange={setChartMode} />
|
|
</div>
|
|
<div className="mt-4 flex flex-wrap items-end justify-between gap-3">
|
|
<div className="flex flex-wrap items-baseline gap-2">
|
|
<p className="text-[28px] font-bold text-foreground tracking-tight leading-none">
|
|
{formatCompact(displayStats.revenue_last_30_days).replace('₹', '')}
|
|
</p>
|
|
<p className="text-[13px] text-muted-foreground">Revenue with orders (INR)</p>
|
|
</div>
|
|
<div className="flex items-center gap-4 text-[12px] text-muted-foreground">
|
|
<span className="inline-flex items-center gap-1.5">
|
|
<span className="w-2 h-2 rounded-full bg-primary" /> Revenue
|
|
</span>
|
|
<span className="inline-flex items-center gap-1.5">
|
|
<span className="w-2 h-2 rounded-full bg-[#d9dee8]" /> Orders
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<div className="h-[220px] w-full mt-4">
|
|
<ResponsiveContainer width="100%" height="100%">
|
|
<ComposedChart data={chartPoints} margin={{ top: 8, right: 8, left: 0, bottom: 0 }}>
|
|
<CartesianGrid vertical stroke="var(--border)" strokeDasharray="4 4" horizontal={false} />
|
|
<XAxis dataKey="name" tick={{ fontSize: 11, fill: 'var(--muted-foreground)' }} axisLine={false} tickLine={false} />
|
|
<YAxis tick={{ fontSize: 11, fill: 'var(--muted-foreground)' }} axisLine={false} tickLine={false} tickFormatter={(v: number) => (v >= 1000 ? `₹${(v / 1000).toFixed(0)}k` : `₹${v}`)} />
|
|
<Tooltip content={({ active, payload, label }) => {
|
|
if (!active || !payload?.length) return null;
|
|
const row = payload[0]?.payload as { revenue: number; orders: number };
|
|
return (
|
|
<div className="bg-card border border-border crm-radius-control px-3 py-2 shadow-sm text-xs">
|
|
<p className="text-muted-foreground mb-0.5">{label}</p>
|
|
<p className="font-semibold text-foreground">{formatInr(row.revenue)}</p>
|
|
<p className="text-muted-foreground">{row.orders} orders</p>
|
|
</div>
|
|
);
|
|
}} />
|
|
<Area type="monotone" dataKey="sales" fill="#EEF0F4" stroke="none" />
|
|
<Bar dataKey="revenue" fill="var(--color-primary, #1B2559)" radius={[2, 2, 0, 0]} />
|
|
</ComposedChart>
|
|
</ResponsiveContainer>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Order Status Pipeline */}
|
|
<div className={cn(CARD, 'p-5 flex flex-col justify-between')}>
|
|
<div>
|
|
<div className="flex items-center justify-between gap-2">
|
|
<CardTitle>Order Status Pipeline</CardTitle>
|
|
<GhostSelect label="Stages" />
|
|
</div>
|
|
<div className="grid grid-cols-2 gap-2 mt-4">
|
|
{pipelineRows.map((row) => (
|
|
<div key={row.key} className="p-2 crm-radius-control bg-muted/40 border border-border/50 text-center">
|
|
<p className="text-[11px] text-muted-foreground">{row.label}</p>
|
|
<p className="text-[13px] font-bold text-foreground mt-0.5">{row.count}</p>
|
|
<p className="text-[10px] text-muted-foreground mt-0.5">{formatCompact(row.value)}</p>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
<div className="flex items-end gap-3 h-[130px] pt-4">
|
|
{pipelineRows.map((row) => (
|
|
<div key={row.key} className="flex-1 flex flex-col items-center justify-end h-full gap-2">
|
|
<div
|
|
className="w-full max-w-[40px] rounded-t-[4px] transition-all"
|
|
style={{
|
|
height: `${Math.max(12, (row.count / maxPipeline) * 100)}%`,
|
|
backgroundColor: row.color,
|
|
}}
|
|
/>
|
|
<span className="text-[11px] font-semibold text-foreground">{row.label}</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* KPI Stats Grid */}
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 gap-4">
|
|
{/* Total Revenue */}
|
|
<div className={cn(CARD, 'p-4 flex flex-col justify-between')}>
|
|
<div className="flex items-start justify-between gap-2">
|
|
<div>
|
|
<p className="text-[13px] text-muted-foreground">Total Revenue</p>
|
|
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">
|
|
{formatInr(displayStats.revenue_all_time)}
|
|
</p>
|
|
</div>
|
|
<span className="w-9 h-9 rounded-full bg-primary/10 text-primary flex items-center justify-center shrink-0">
|
|
<TrendingUp className="w-4 h-4" />
|
|
</span>
|
|
</div>
|
|
<div className="mt-3 flex items-center gap-2">
|
|
<DeltaPill value={activity.revenueDelta} />
|
|
<span className="text-[12px] text-muted-foreground">vs prior 7 days</span>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Total Orders */}
|
|
<div className={cn(CARD, 'p-4 flex flex-col justify-between')}>
|
|
<div className="flex items-start justify-between gap-2">
|
|
<div>
|
|
<p className="text-[13px] text-muted-foreground">Total Orders</p>
|
|
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">
|
|
{totalOrders.toLocaleString()}
|
|
</p>
|
|
</div>
|
|
<span className="w-9 h-9 rounded-full bg-warning/10 text-warning flex items-center justify-center shrink-0">
|
|
<ShoppingBag className="w-4 h-4" />
|
|
</span>
|
|
</div>
|
|
<div className="mt-3 flex items-center justify-between gap-2">
|
|
<div className="flex items-center gap-2">
|
|
<DeltaPill value={activity.ordersDelta} />
|
|
<span className="text-[12px] text-muted-foreground">vs prior 7 days</span>
|
|
</div>
|
|
{spark.length > 0 && (
|
|
<div className="flex items-end gap-1 h-5">
|
|
{spark.map((v, i) => (
|
|
<span
|
|
key={i}
|
|
className="w-1 rounded-xs bg-warning/70"
|
|
style={{ height: `${Math.max(20, (v / sparkMax) * 100)}%` }}
|
|
/>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Delivery Success Rate */}
|
|
<div className={cn(CARD, 'p-4 flex flex-col justify-between')}>
|
|
<div className="flex items-start justify-between gap-2">
|
|
<div>
|
|
<p className="text-[13px] text-muted-foreground">Delivery Success Rate</p>
|
|
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">
|
|
{conversion.toFixed(1)}%
|
|
</p>
|
|
</div>
|
|
<span className="w-9 h-9 rounded-full bg-success/10 text-success flex items-center justify-center shrink-0">
|
|
<PackageCheck className="w-4 h-4" />
|
|
</span>
|
|
</div>
|
|
<div className="mt-3 flex items-center gap-2">
|
|
<DeltaPill value={activity.fulfillmentDelta} />
|
|
<span className="text-[12px] text-muted-foreground">vs prior 7 days</span>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Total Customers */}
|
|
<div className={cn(CARD, 'p-4 flex flex-col justify-between')}>
|
|
<div className="flex items-start justify-between gap-2">
|
|
<div>
|
|
<p className="text-[13px] text-muted-foreground">Total Customers</p>
|
|
<p className="text-[22px] font-bold text-foreground mt-2 tracking-tight">
|
|
{displayStats.total_users.toLocaleString()}
|
|
</p>
|
|
</div>
|
|
<span className="w-9 h-9 rounded-full bg-primary/10 text-primary flex items-center justify-center shrink-0">
|
|
<Users className="w-4 h-4" />
|
|
</span>
|
|
</div>
|
|
<div className="mt-3 flex items-center justify-between gap-2">
|
|
<div className="flex items-center gap-2">
|
|
<DeltaPill value={activity.customersDelta} />
|
|
<span className="text-[12px] text-muted-foreground">vs prior 7 days</span>
|
|
</div>
|
|
{customerAvatars.length > 0 && (
|
|
<div className="flex -space-x-1.5 overflow-hidden">
|
|
{customerAvatars.map((c, i) => (
|
|
<span
|
|
key={i}
|
|
className="inline-flex items-center justify-center w-5 h-5 rounded-full ring-2 ring-card bg-muted text-[10px] font-semibold text-foreground"
|
|
title={c.customer_name}
|
|
>
|
|
{initials(c.customer_name)}
|
|
</span>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Middle Grid: Order Status Breakdown & Live Sessions */}
|
|
<div className="grid grid-cols-1 lg:grid-cols-3 gap-4">
|
|
{/* Order Status Breakdown */}
|
|
<div className={cn(CARD, 'p-5 flex flex-col justify-between')}>
|
|
<div className="flex items-center justify-between gap-2">
|
|
<CardTitle>Order Status Breakdown</CardTitle>
|
|
<GhostSelect label="Live" />
|
|
</div>
|
|
<div className="flex items-center justify-center py-4">
|
|
<StatusDonut slices={donutSlices} />
|
|
</div>
|
|
<div className="space-y-2 border-t border-border pt-3">
|
|
{overviewRows.map((row) => {
|
|
const pct = Math.round((row.count / overviewTotal) * 100);
|
|
return (
|
|
<div key={row.key} className="flex items-center justify-between text-[12px]">
|
|
<span className="inline-flex items-center gap-2 text-muted-foreground">
|
|
<span className="w-2.5 h-2.5 rounded-full" style={{ backgroundColor: row.color }} />
|
|
{row.label}
|
|
</span>
|
|
<div className="flex items-center gap-2 font-semibold text-foreground">
|
|
<span>{row.count}</span>
|
|
<span className="text-[11px] text-muted-foreground w-8 text-right">{pct}%</span>
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Live Admin Sessions */}
|
|
<div className={cn(CARD, 'lg:col-span-2 p-5 flex flex-col')}>
|
|
<div className="flex items-center justify-between gap-3 mb-4">
|
|
<div className="flex items-center gap-2">
|
|
<CardTitle>Live System Sessions</CardTitle>
|
|
<span className="px-2 py-0.5 text-xs font-semibold rounded-full bg-emerald-500/10 text-emerald-600 border border-emerald-500/20">
|
|
{sessions.length} Active
|
|
</span>
|
|
</div>
|
|
{isRouteAllowed('/security', userRole) && (
|
|
<Link href="/security" className="inline-flex items-center gap-1 text-[13px] font-medium text-primary hover:underline">
|
|
Manage Security <ArrowRight className="w-3.5 h-3.5" />
|
|
</Link>
|
|
)}
|
|
</div>
|
|
<div className="overflow-x-auto">
|
|
<table className="crm-data-table min-w-[500px]">
|
|
<thead>
|
|
<tr className="border-y border-border">
|
|
<th className="text-left font-medium text-muted-foreground px-4 py-2">User Identifier</th>
|
|
<th className="text-left font-medium text-muted-foreground px-4 py-2">IP Address</th>
|
|
<th className="text-left font-medium text-muted-foreground px-4 py-2">Client Device</th>
|
|
<th className="text-left font-medium text-muted-foreground px-4 py-2">State</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{sessions.length === 0 ? (
|
|
<tr>
|
|
<td colSpan={4} className="py-6 text-center text-xs text-muted-foreground">
|
|
No active sessions recorded
|
|
</td>
|
|
</tr>
|
|
) : (
|
|
sessions.slice(0, 5).map((s) => (
|
|
<tr key={s.session_id} className="border-b border-border last:border-0">
|
|
<td className="px-4 py-2.5 font-medium text-foreground text-xs">{s.user_id}</td>
|
|
<td className="px-4 py-2.5 text-xs text-muted-foreground font-mono">{s.ip_address || '—'}</td>
|
|
<td className="px-4 py-2.5 text-xs text-muted-foreground truncate max-w-[200px]" title={`${s.browser || ''} ${s.operating_system || ''}`}>
|
|
{s.browser || s.device_name || 'Web Client'}
|
|
</td>
|
|
<td className="px-4 py-2.5">
|
|
<span className="inline-flex items-center gap-1.5 px-2 py-0.5 rounded-full text-[11px] font-medium bg-emerald-500/10 text-emerald-600">
|
|
<span className="w-1.5 h-1.5 rounded-full bg-emerald-500" />
|
|
Online
|
|
</span>
|
|
</td>
|
|
</tr>
|
|
))
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Recent Orders & Top Selling Products */}
|
|
<div className="grid grid-cols-1 lg:grid-cols-3 gap-4">
|
|
{/* Recent Orders Table */}
|
|
<div className={cn(CARD, 'lg:col-span-2')}>
|
|
<div className="px-5 py-4 flex items-center justify-between gap-3">
|
|
<CardTitle>Recent Orders</CardTitle>
|
|
<div className="flex items-center gap-2">
|
|
<span className="inline-flex items-center gap-1.5 h-7 px-2.5 crm-radius-control border border-border text-[12px] text-muted-foreground">
|
|
<Filter className="w-3.5 h-3.5" />
|
|
Filter
|
|
</span>
|
|
<Link href="/orders" className="inline-flex items-center gap-1 text-[13px] font-medium text-primary hover:underline">
|
|
See all <ArrowRight className="w-3.5 h-3.5" />
|
|
</Link>
|
|
</div>
|
|
</div>
|
|
<div className="overflow-x-auto">
|
|
<table className="crm-data-table min-w-[560px]">
|
|
<thead>
|
|
<tr className="border-y border-border">
|
|
<th className="text-left font-medium text-muted-foreground px-5 py-2.5">Order</th>
|
|
<th className="text-left font-medium text-muted-foreground px-5 py-2.5">Date</th>
|
|
<th className="text-left font-medium text-muted-foreground px-5 py-2.5">Total</th>
|
|
<th className="text-left font-medium text-muted-foreground px-5 py-2.5">Status</th>
|
|
<th className="text-left font-medium text-muted-foreground px-5 py-2.5">Payment</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{(displayStats.recent_orders ?? []).length === 0 ? (
|
|
<tr>
|
|
<td colSpan={5} className="py-8 text-center text-[13px] text-muted-foreground">
|
|
No recent orders found
|
|
</td>
|
|
</tr>
|
|
) : (
|
|
(displayStats.recent_orders ?? []).map((order) => (
|
|
<tr key={order.order_no} className="border-b border-border last:border-0">
|
|
<td className="px-5 py-3 font-semibold text-foreground">{order.order_no}</td>
|
|
<td className="px-5 py-3 text-muted-foreground">
|
|
{order.created_at ? new Date(order.created_at).toLocaleDateString('en-IN', { day: '2-digit', month: 'short' }) : '—'}
|
|
</td>
|
|
<td className="px-5 py-3 font-medium text-foreground">{formatInr(order.amount)}</td>
|
|
<td className="px-5 py-3 text-muted-foreground capitalize">{order.status}</td>
|
|
<td className="px-5 py-3">
|
|
<span className={cn('inline-flex items-center h-6 px-2 crm-radius-badge text-[11px] font-semibold border capitalize', PAYMENT_TAG[order.payment_status] ?? 'border-border text-muted-foreground')}>
|
|
{order.payment_status || '—'}
|
|
</span>
|
|
</td>
|
|
</tr>
|
|
))
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Top Selling Products with 3D Isometric PillarGrid */}
|
|
<TopSellingProductsCard products={displayStats.top_products ?? []} />
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|