From fe6d5445116fcb1cb01fa880dd7edef9c3b48550 Mon Sep 17 00:00:00 2001 From: g Date: Tue, 13 Jan 2026 08:55:03 +0000 Subject: [PATCH] Refactor AdminAnalytics helpers and formatting Refactored helper functions (transformChartData, combineOrdersAndRevenue, CustomTooltip, formatCurrency, calculateBestMonth) to be defined outside the main component for improved readability and maintainability. Updated code formatting and indentation for consistency, with no changes to business logic. --- components/admin/AdminAnalytics.tsx | 77 +++++++++++++++++++---------- public/git-info.json | 4 +- 2 files changed, 53 insertions(+), 28 deletions(-) diff --git a/components/admin/AdminAnalytics.tsx b/components/admin/AdminAnalytics.tsx index dd1426e..daaae29 100644 --- a/components/admin/AdminAnalytics.tsx +++ b/components/admin/AdminAnalytics.tsx @@ -58,6 +58,15 @@ import { TableSkeleton } from "../analytics/SkeletonLoaders"; +// Format currency for admin analytics +const formatAdminCurrency = (value: number) => { + return new Intl.NumberFormat("en-GB", { + style: "currency", + currency: "GBP", + maximumFractionDigits: 0, + }).format(value); +}; + const renderActiveShape = (props: any) => { const RADIAN = Math.PI / 180; const { @@ -86,7 +95,7 @@ const renderActiveShape = (props: any) => { return ( - {payload.name.split(" ")[0]} + {payload.name ? payload.name.split(" ")[0] : "Product"} 0) { insights.push({ type: 'positive', - message: `AI Forecast: Projected ${formatCurrency(data.predictions.predicted)} revenue over the next 7 days.`, + message: `AI Forecast: Projected ${formatAdminCurrency(data.predictions.predicted)} revenue over the next 7 days.`, icon: Zap, color: 'text-purple-500', bg: 'bg-purple-500/10' @@ -185,7 +194,7 @@ const getSmartInsights = (data?: AnalyticsData | null, growth?: GrowthData | nul rawInsights.push({ score: Math.abs(revPercent) * (label === '1w' ? 1.5 : 1), // Weight recent changes higher type: revPercent > 0 ? 'positive' : 'neutral', - message: `Revenue is ${revPercent > 0 ? 'up' : 'down'} ${Math.abs(revPercent).toFixed(1)}% vs ${timeframeName} (${formatCurrency(Math.abs(revDiff))} difference).`, + message: `Revenue is ${revPercent > 0 ? 'up' : 'down'} ${Math.abs(revPercent).toFixed(1)}% vs ${timeframeName} (${formatAdminCurrency(Math.abs(revDiff))} difference).`, icon: revPercent > 0 ? TrendingUp : TrendingDown, color: revPercent > 0 ? 'text-green-500' : 'text-orange-500', bg: revPercent > 0 ? 'bg-green-500/10' : 'bg-orange-500/10' @@ -544,7 +553,6 @@ export default function AdminAnalytics() { ); } - // Helper to transform data for recharts const transformChartData = ( data: Array<{ date: string;[key: string]: any }>, @@ -554,6 +562,16 @@ export default function AdminAnalytics() { return data.map((item) => { const dateStr = item.date; + + if (!dateStr) { + return { + date: "Unknown", + formattedDate: "Unknown", + value: Number(item[valueKey]) || 0, + [valueKey]: Number(item[valueKey]) || 0, + }; + } + // Parse YYYY-MM-DD format const parts = dateStr.split("-"); const date = @@ -600,15 +618,28 @@ export default function AdminAnalytics() { >(); if (revenue && revenue.length > 0) { revenue.forEach((r) => { - revenueMap.set(r.date, { - amount: r.amount || 0, - avgOrderValue: r.avgOrderValue || 0, - }); + if (r.date) { + revenueMap.set(r.date, { + amount: r.amount || 0, + avgOrderValue: r.avgOrderValue || 0, + }); + } }); } return orders.map((order) => { const dateStr = order.date; + + if (!dateStr) { + return { + date: "Unknown", + formattedDate: "Unknown", + orders: order.count || 0, + revenue: 0, + avgOrderValue: 0 + }; + } + const parts = dateStr.split("-"); const date = parts.length === 3 @@ -641,6 +672,7 @@ export default function AdminAnalytics() { }); }; + // Custom tooltip for charts const CustomTooltip = ({ active, payload, label }: any) => { if (active && payload && payload.length) { @@ -701,14 +733,7 @@ export default function AdminAnalytics() { - // Format currency - const formatCurrency = (value: number) => { - return new Intl.NumberFormat("en-GB", { - style: "currency", - currency: "GBP", - maximumFractionDigits: 0, - }).format(value); - }; + // Calculate best month from daily data (for YTD, full year, or previous years) const calculateBestMonth = (): { month: string; revenue: number; orders: number } | null => { @@ -950,7 +975,7 @@ export default function AdminAnalytics() { Revenue
- {formatCurrency(bestMonth.revenue)} + {formatAdminCurrency(bestMonth.revenue)}
{formatNumber(bestMonth.orders)} orders @@ -993,10 +1018,10 @@ export default function AdminAnalytics() { icon={DollarSign} iconColorClass="text-green-500" iconBgClass="bg-green-500/10" - value={formatCurrency(analyticsData?.revenue?.total || 0)} + value={formatAdminCurrency(analyticsData?.revenue?.total || 0)} subtext={ - Today: {formatCurrency(analyticsData?.revenue?.today || 0)} + Today: {formatAdminCurrency(analyticsData?.revenue?.today || 0)} } trend={{ @@ -1221,7 +1246,7 @@ export default function AdminAnalytics() { Total Revenue
- {formatCurrency( + {formatAdminCurrency( analyticsData.revenue.dailyRevenue.reduce( (sum, day) => sum + (day.amount || 0), 0, @@ -1377,7 +1402,7 @@ export default function AdminAnalytics() {
- {formatCurrency(vendor.totalRevenue)} + {formatAdminCurrency(vendor.totalRevenue)}
@@ -1437,7 +1462,7 @@ export default function AdminAnalytics() {
Total Revenue
-
{formatCurrency(growthData.cumulative.revenue)}
+
{formatAdminCurrency(growthData.cumulative.revenue)}
@@ -1461,7 +1486,7 @@ export default function AdminAnalytics() {
Avg Order Value
-
{formatCurrency(growthData.cumulative.avgOrderValue)}
+
{formatAdminCurrency(growthData.cumulative.avgOrderValue)}
@@ -1542,7 +1567,7 @@ export default function AdminAnalytics() {
Revenue - {formatCurrency(data.revenue)} + {formatAdminCurrency(data.revenue)}
@@ -1747,13 +1772,13 @@ export default function AdminAnalytics() {
- {formatCurrency(month.revenue)} + {formatAdminCurrency(month.revenue)} {month.customers.toLocaleString()} - {formatCurrency(month.avgOrderValue)} + {formatAdminCurrency(month.avgOrderValue)} {month.newVendors} diff --git a/public/git-info.json b/public/git-info.json index 669d233..df84666 100644 --- a/public/git-info.json +++ b/public/git-info.json @@ -1,4 +1,4 @@ { - "commitHash": "600ba1e", - "buildTime": "2026-01-13T06:14:18.909Z" + "commitHash": "38779c2", + "buildTime": "2026-01-13T08:38:56.729Z" } \ No newline at end of file