This commit is contained in:
NotII
2025-06-30 16:56:57 +01:00
parent 6516c6d2f9
commit 84e4515a26
4 changed files with 122 additions and 218 deletions

View File

@@ -132,6 +132,30 @@ export default function AnalyticsDashboard({ initialData }: AnalyticsDashboardPr
</Card> </Card>
{/* Analytics Tabs */} {/* Analytics Tabs */}
<div className="space-y-6">
{/* Global Date Selector */}
<div className="flex items-center justify-between">
<div>
<h2 className="text-xl font-semibold">Analytics Dashboard</h2>
<p className="text-muted-foreground">
Detailed insights into your business performance
</p>
</div>
<div className="flex items-center gap-2">
<span className="text-sm text-muted-foreground">Time period:</span>
<Select value={timeRange} onValueChange={setTimeRange}>
<SelectTrigger className="w-32">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="7">7 days</SelectItem>
<SelectItem value="30">30 days</SelectItem>
<SelectItem value="90">90 days</SelectItem>
</SelectContent>
</Select>
</div>
</div>
<Tabs defaultValue="revenue" className="space-y-6"> <Tabs defaultValue="revenue" className="space-y-6">
<TabsList className="grid w-full grid-cols-4"> <TabsList className="grid w-full grid-cols-4">
<TabsTrigger value="revenue" className="flex items-center gap-2"> <TabsTrigger value="revenue" className="flex items-center gap-2">
@@ -153,24 +177,12 @@ export default function AnalyticsDashboard({ initialData }: AnalyticsDashboardPr
</TabsList> </TabsList>
<TabsContent value="revenue" className="space-y-6"> <TabsContent value="revenue" className="space-y-6">
<div className="flex items-center justify-between">
<div> <div>
<h3 className="text-lg font-semibold">Revenue Trends</h3> <h3 className="text-lg font-semibold">Revenue Trends</h3>
<p className="text-muted-foreground"> <p className="text-muted-foreground">
Track your revenue performance over time Track your revenue performance over the selected time period
</p> </p>
</div> </div>
<Select value={timeRange} onValueChange={setTimeRange}>
<SelectTrigger className="w-32">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="7">7 days</SelectItem>
<SelectItem value="30">30 days</SelectItem>
<SelectItem value="90">90 days</SelectItem>
</SelectContent>
</Select>
</div>
<RevenueChart timeRange={timeRange} /> <RevenueChart timeRange={timeRange} />
</TabsContent> </TabsContent>
@@ -178,7 +190,7 @@ export default function AnalyticsDashboard({ initialData }: AnalyticsDashboardPr
<div> <div>
<h3 className="text-lg font-semibold">Product Performance</h3> <h3 className="text-lg font-semibold">Product Performance</h3>
<p className="text-muted-foreground"> <p className="text-muted-foreground">
Analyze which products are performing best Analyze which products are performing best (all-time data)
</p> </p>
</div> </div>
<ProductPerformanceChart /> <ProductPerformanceChart />
@@ -188,7 +200,7 @@ export default function AnalyticsDashboard({ initialData }: AnalyticsDashboardPr
<div> <div>
<h3 className="text-lg font-semibold">Customer Insights</h3> <h3 className="text-lg font-semibold">Customer Insights</h3>
<p className="text-muted-foreground"> <p className="text-muted-foreground">
Understand your customer base and behavior Understand your customer base and behavior (all-time data)
</p> </p>
</div> </div>
<CustomerInsightsChart /> <CustomerInsightsChart />
@@ -198,12 +210,13 @@ export default function AnalyticsDashboard({ initialData }: AnalyticsDashboardPr
<div> <div>
<h3 className="text-lg font-semibold">Order Analytics</h3> <h3 className="text-lg font-semibold">Order Analytics</h3>
<p className="text-muted-foreground"> <p className="text-muted-foreground">
Detailed analysis of order patterns and status distribution Order status distribution for the selected time period
</p> </p>
</div> </div>
<OrderAnalyticsChart timeRange={timeRange} /> <OrderAnalyticsChart timeRange={timeRange} />
</TabsContent> </TabsContent>
</Tabs> </Tabs>
</div> </div>
</div>
); );
} }

View File

@@ -145,20 +145,36 @@ export default function OrderAnalyticsChart({ timeRange }: OrderAnalyticsChartPr
} }
const totalOrders = data.statusDistribution.reduce((sum, item) => sum + item.count, 0); const totalOrders = data.statusDistribution.reduce((sum, item) => sum + item.count, 0);
const totalRevenue = data.dailyOrders.reduce((sum, item) => sum + item.revenue, 0);
return ( return (
<div className="space-y-6">
{/* Order Status Distribution */}
<Card> <Card>
<CardHeader> <CardHeader>
<CardTitle>Order Status Distribution</CardTitle> <CardTitle className="flex items-center gap-2">
<BarChart3 className="h-5 w-5" />
Order Analytics
</CardTitle>
<CardDescription> <CardDescription>
Breakdown of orders by current status Order status distribution for the selected time period
</CardDescription> </CardDescription>
</CardHeader> </CardHeader>
<CardContent> <CardContent>
<div className="space-y-4"> <div className="space-y-4">
{data.statusDistribution.length === 0 ? (
<div className="text-center py-8">
<BarChart3 className="h-12 w-12 mx-auto text-muted-foreground mb-4" />
<p className="text-muted-foreground">No order data available for this period</p>
</div>
) : (
<>
{/* Summary */}
<div className="text-center mb-6">
<div className="text-3xl font-bold text-blue-600">
{totalOrders}
</div>
<div className="text-sm text-muted-foreground">Total Orders</div>
</div>
{/* Status Distribution */}
{data.statusDistribution.map((status) => { {data.statusDistribution.map((status) => {
const percentage = totalOrders > 0 ? (status.count / totalOrders * 100).toFixed(1) : '0'; const percentage = totalOrders > 0 ? (status.count / totalOrders * 100).toFixed(1) : '0';
const Icon = getStatusIcon(status._id); const Icon = getStatusIcon(status._id);
@@ -183,98 +199,10 @@ export default function OrderAnalyticsChart({ timeRange }: OrderAnalyticsChartPr
</div> </div>
); );
})} })}
</div> </>
</CardContent>
</Card>
{/* Daily Order Trends */}
<Card>
<CardHeader>
<CardTitle>Daily Order Trends</CardTitle>
<CardDescription>
Orders and revenue over the selected time period
</CardDescription>
</CardHeader>
<CardContent>
{data.dailyOrders.length === 0 ? (
<div className="text-center py-8">
<BarChart3 className="h-12 w-12 mx-auto text-muted-foreground mb-4" />
<p className="text-muted-foreground">No order data available for this period</p>
</div>
) : (
<div className="space-y-4">
{/* Summary Stats */}
<div className="grid grid-cols-3 gap-4 mb-6">
<div className="text-center">
<div className="text-2xl font-bold text-blue-600">
{data.dailyOrders.length}
</div>
<div className="text-sm text-muted-foreground">Days with Orders</div>
</div>
<div className="text-center">
<div className="text-2xl font-bold text-green-600">
{totalOrders}
</div>
<div className="text-sm text-muted-foreground">Total Orders</div>
</div>
<div className="text-center">
<div className="text-2xl font-bold text-purple-600">
{formatGBP(totalRevenue)}
</div>
<div className="text-sm text-muted-foreground">Total Revenue</div>
</div>
</div>
{/* Chart */}
<div className="h-64 flex items-end justify-between gap-1">
{data.dailyOrders.map((item, index) => {
const maxOrders = Math.max(...data.dailyOrders.map(d => d.orders));
const height = maxOrders > 0 ? (item.orders / maxOrders) * 100 : 0;
const date = new Date(item._id.year, item._id.month - 1, item._id.day);
const dateLabel = date.toLocaleDateString('en-GB', {
month: 'short',
day: 'numeric'
});
return (
<div key={index} className="flex-1 flex flex-col items-center">
<div
className="w-full bg-primary/20 hover:bg-primary/30 transition-colors rounded-t"
style={{ height: `${height}%` }}
title={`${dateLabel}: ${item.orders} orders, ${formatGBP(item.revenue)} revenue`}
/>
<div className="text-xs text-muted-foreground mt-1 rotate-45 origin-left">
{dateLabel}
</div>
</div>
);
})}
</div>
</div>
)} )}
</CardContent>
</Card>
{/* Processing Time */}
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Clock className="h-5 w-5" />
Order Processing Time
</CardTitle>
<CardDescription>
Average time to complete orders
</CardDescription>
</CardHeader>
<CardContent>
<div className="text-center">
<div className="text-3xl font-bold text-blue-600">
{data.averageProcessingDays.toFixed(1)}
</div>
<div className="text-sm text-muted-foreground">Average Days to Complete</div>
</div> </div>
</CardContent> </CardContent>
</Card> </Card>
</div>
); );
} }

View File

@@ -39,32 +39,6 @@ export default function ProductPerformanceChart() {
fetchProductData(); fetchProductData();
}, [toast]); }, [toast]);
const getStockStatusColor = (status: string) => {
switch (status) {
case 'in_stock':
return 'bg-green-100 text-green-800';
case 'low_stock':
return 'bg-yellow-100 text-yellow-800';
case 'out_of_stock':
return 'bg-red-100 text-red-800';
default:
return 'bg-gray-100 text-gray-800';
}
};
const getStockStatusText = (status: string) => {
switch (status) {
case 'in_stock':
return 'In Stock';
case 'low_stock':
return 'Low Stock';
case 'out_of_stock':
return 'Out of Stock';
default:
return 'Unknown';
}
};
if (isLoading) { if (isLoading) {
return ( return (
<Card> <Card>
@@ -157,7 +131,6 @@ export default function ProductPerformanceChart() {
<TableHeader> <TableHeader>
<TableRow> <TableRow>
<TableHead>Product</TableHead> <TableHead>Product</TableHead>
<TableHead>Stock</TableHead>
<TableHead className="text-right">Sold</TableHead> <TableHead className="text-right">Sold</TableHead>
<TableHead className="text-right">Revenue</TableHead> <TableHead className="text-right">Revenue</TableHead>
<TableHead className="text-right">Orders</TableHead> <TableHead className="text-right">Orders</TableHead>
@@ -185,19 +158,6 @@ export default function ProductPerformanceChart() {
</div> </div>
</div> </div>
</TableCell> </TableCell>
<TableCell>
<div className="flex flex-col gap-1">
<Badge
variant="secondary"
className={getStockStatusColor(product.stockStatus)}
>
{getStockStatusText(product.stockStatus)}
</Badge>
<span className="text-xs text-muted-foreground">
{product.currentStock} available
</span>
</div>
</TableCell>
<TableCell className="text-right font-medium"> <TableCell className="text-right font-medium">
{product.totalSold.toFixed(2)} {product.totalSold.toFixed(2)}
</TableCell> </TableCell>

View File

@@ -53,14 +53,17 @@ export default function RevenueChart({ timeRange }: RevenueChartProps) {
// Transform data for Recharts // Transform data for Recharts
const chartData: ChartDataPoint[] = data.map(item => { const chartData: ChartDataPoint[] = data.map(item => {
const date = new Date(item._id.year, item._id.month - 1, item._id.day); // Use UTC to avoid timezone issues
const date = new Date(Date.UTC(item._id.year, item._id.month - 1, item._id.day));
return { return {
date: date.toISOString().split('T')[0], // YYYY-MM-DD format date: date.toISOString().split('T')[0], // YYYY-MM-DD format
revenue: item.revenue || 0, revenue: item.revenue || 0,
orders: item.orders || 0, orders: item.orders || 0,
formattedDate: date.toLocaleDateString('en-GB', { formattedDate: date.toLocaleDateString('en-GB', {
weekday: 'short',
month: 'short', month: 'short',
day: 'numeric' day: 'numeric',
timeZone: 'UTC'
}) })
}; };
}); });