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,34 +132,17 @@ export default function AnalyticsDashboard({ initialData }: AnalyticsDashboardPr
</Card> </Card>
{/* Analytics Tabs */} {/* Analytics Tabs */}
<Tabs defaultValue="revenue" className="space-y-6"> <div className="space-y-6">
<TabsList className="grid w-full grid-cols-4"> {/* Global Date Selector */}
<TabsTrigger value="revenue" className="flex items-center gap-2"> <div className="flex items-center justify-between">
<TrendingUp className="h-4 w-4" /> <div>
Revenue <h2 className="text-xl font-semibold">Analytics Dashboard</h2>
</TabsTrigger> <p className="text-muted-foreground">
<TabsTrigger value="products" className="flex items-center gap-2"> Detailed insights into your business performance
<Package className="h-4 w-4" /> </p>
Products </div>
</TabsTrigger> <div className="flex items-center gap-2">
<TabsTrigger value="customers" className="flex items-center gap-2"> <span className="text-sm text-muted-foreground">Time period:</span>
<Users className="h-4 w-4" />
Customers
</TabsTrigger>
<TabsTrigger value="orders" className="flex items-center gap-2">
<BarChart3 className="h-4 w-4" />
Orders
</TabsTrigger>
</TabsList>
<TabsContent value="revenue" className="space-y-6">
<div className="flex items-center justify-between">
<div>
<h3 className="text-lg font-semibold">Revenue Trends</h3>
<p className="text-muted-foreground">
Track your revenue performance over time
</p>
</div>
<Select value={timeRange} onValueChange={setTimeRange}> <Select value={timeRange} onValueChange={setTimeRange}>
<SelectTrigger className="w-32"> <SelectTrigger className="w-32">
<SelectValue /> <SelectValue />
@@ -171,39 +154,69 @@ export default function AnalyticsDashboard({ initialData }: AnalyticsDashboardPr
</SelectContent> </SelectContent>
</Select> </Select>
</div> </div>
<RevenueChart timeRange={timeRange} /> </div>
</TabsContent>
<TabsContent value="products" className="space-y-6"> <Tabs defaultValue="revenue" className="space-y-6">
<div> <TabsList className="grid w-full grid-cols-4">
<h3 className="text-lg font-semibold">Product Performance</h3> <TabsTrigger value="revenue" className="flex items-center gap-2">
<p className="text-muted-foreground"> <TrendingUp className="h-4 w-4" />
Analyze which products are performing best Revenue
</p> </TabsTrigger>
</div> <TabsTrigger value="products" className="flex items-center gap-2">
<ProductPerformanceChart /> <Package className="h-4 w-4" />
</TabsContent> Products
</TabsTrigger>
<TabsTrigger value="customers" className="flex items-center gap-2">
<Users className="h-4 w-4" />
Customers
</TabsTrigger>
<TabsTrigger value="orders" className="flex items-center gap-2">
<BarChart3 className="h-4 w-4" />
Orders
</TabsTrigger>
</TabsList>
<TabsContent value="customers" className="space-y-6"> <TabsContent value="revenue" className="space-y-6">
<div> <div>
<h3 className="text-lg font-semibold">Customer Insights</h3> <h3 className="text-lg font-semibold">Revenue Trends</h3>
<p className="text-muted-foreground"> <p className="text-muted-foreground">
Understand your customer base and behavior Track your revenue performance over the selected time period
</p> </p>
</div> </div>
<CustomerInsightsChart /> <RevenueChart timeRange={timeRange} />
</TabsContent> </TabsContent>
<TabsContent value="orders" className="space-y-6"> <TabsContent value="products" className="space-y-6">
<div> <div>
<h3 className="text-lg font-semibold">Order Analytics</h3> <h3 className="text-lg font-semibold">Product Performance</h3>
<p className="text-muted-foreground"> <p className="text-muted-foreground">
Detailed analysis of order patterns and status distribution Analyze which products are performing best (all-time data)
</p> </p>
</div> </div>
<OrderAnalyticsChart timeRange={timeRange} /> <ProductPerformanceChart />
</TabsContent> </TabsContent>
</Tabs>
<TabsContent value="customers" className="space-y-6">
<div>
<h3 className="text-lg font-semibold">Customer Insights</h3>
<p className="text-muted-foreground">
Understand your customer base and behavior (all-time data)
</p>
</div>
<CustomerInsightsChart />
</TabsContent>
<TabsContent value="orders" className="space-y-6">
<div>
<h3 className="text-lg font-semibold">Order Analytics</h3>
<p className="text-muted-foreground">
Order status distribution for the selected time period
</p>
</div>
<OrderAnalyticsChart timeRange={timeRange} />
</TabsContent>
</Tabs>
</div>
</div> </div>
); );
} }

View File

@@ -145,136 +145,64 @@ 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"> <Card>
{/* Order Status Distribution */} <CardHeader>
<Card> <CardTitle className="flex items-center gap-2">
<CardHeader> <BarChart3 className="h-5 w-5" />
<CardTitle>Order Status Distribution</CardTitle> Order Analytics
<CardDescription> </CardTitle>
Breakdown of orders by current status <CardDescription>
</CardDescription> Order status distribution for the selected time period
</CardHeader> </CardDescription>
<CardContent> </CardHeader>
<div className="space-y-4"> <CardContent>
{data.statusDistribution.map((status) => { <div className="space-y-4">
const percentage = totalOrders > 0 ? (status.count / totalOrders * 100).toFixed(1) : '0'; {data.statusDistribution.length === 0 ? (
const Icon = getStatusIcon(status._id);
return (
<div key={status._id} className="flex items-center justify-between p-3 border rounded-lg">
<div className="flex items-center gap-3">
<div className={`p-2 rounded-full ${getStatusColor(status._id)}`}>
{Icon}
</div>
<div>
<div className="font-medium">{getStatusLabel(status._id)}</div>
<div className="text-sm text-muted-foreground">
{status.count} orders
</div>
</div>
</div>
<div className="text-right">
<div className="text-lg font-bold">{percentage}%</div>
<div className="text-sm text-muted-foreground">of total</div>
</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"> <div className="text-center py-8">
<BarChart3 className="h-12 w-12 mx-auto text-muted-foreground mb-4" /> <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> <p className="text-muted-foreground">No order data available for this period</p>
</div> </div>
) : ( ) : (
<div className="space-y-4"> <>
{/* Summary Stats */} {/* Summary */}
<div className="grid grid-cols-3 gap-4 mb-6"> <div className="text-center mb-6">
<div className="text-center"> <div className="text-3xl font-bold text-blue-600">
<div className="text-2xl font-bold text-blue-600"> {totalOrders}
{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>
<div className="text-sm text-muted-foreground">Total Orders</div>
</div> </div>
{/* Chart */} {/* Status Distribution */}
<div className="h-64 flex items-end justify-between gap-1"> {data.statusDistribution.map((status) => {
{data.dailyOrders.map((item, index) => { const percentage = totalOrders > 0 ? (status.count / totalOrders * 100).toFixed(1) : '0';
const maxOrders = Math.max(...data.dailyOrders.map(d => d.orders)); const Icon = getStatusIcon(status._id);
const height = maxOrders > 0 ? (item.orders / maxOrders) * 100 : 0;
const date = new Date(item._id.year, item._id.month - 1, item._id.day); return (
const dateLabel = date.toLocaleDateString('en-GB', { <div key={status._id} className="flex items-center justify-between p-3 border rounded-lg">
month: 'short', <div className="flex items-center gap-3">
day: 'numeric' <div className={`p-2 rounded-full ${getStatusColor(status._id)}`}>
}); {Icon}
</div>
return ( <div>
<div key={index} className="flex-1 flex flex-col items-center"> <div className="font-medium">{getStatusLabel(status._id)}</div>
<div <div className="text-sm text-muted-foreground">
className="w-full bg-primary/20 hover:bg-primary/30 transition-colors rounded-t" {status.count} orders
style={{ height: `${height}%` }} </div>
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>
); <div className="text-right">
})} <div className="text-lg font-bold">{percentage}%</div>
</div> <div className="text-sm text-muted-foreground">of total</div>
</div> </div>
</div>
);
})}
</>
)} )}
</CardContent> </div>
</Card> </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>
</CardContent>
</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'
}) })
}; };
}); });