anal
This commit is contained in:
@@ -132,34 +132,17 @@ export default function AnalyticsDashboard({ initialData }: AnalyticsDashboardPr
|
||||
</Card>
|
||||
|
||||
{/* Analytics Tabs */}
|
||||
<Tabs defaultValue="revenue" className="space-y-6">
|
||||
<TabsList className="grid w-full grid-cols-4">
|
||||
<TabsTrigger value="revenue" className="flex items-center gap-2">
|
||||
<TrendingUp className="h-4 w-4" />
|
||||
Revenue
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="products" className="flex items-center gap-2">
|
||||
<Package className="h-4 w-4" />
|
||||
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="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>
|
||||
<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 />
|
||||
@@ -171,39 +154,69 @@ export default function AnalyticsDashboard({ initialData }: AnalyticsDashboardPr
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<RevenueChart timeRange={timeRange} />
|
||||
</TabsContent>
|
||||
</div>
|
||||
|
||||
<TabsContent value="products" className="space-y-6">
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold">Product Performance</h3>
|
||||
<p className="text-muted-foreground">
|
||||
Analyze which products are performing best
|
||||
</p>
|
||||
</div>
|
||||
<ProductPerformanceChart />
|
||||
</TabsContent>
|
||||
<Tabs defaultValue="revenue" className="space-y-6">
|
||||
<TabsList className="grid w-full grid-cols-4">
|
||||
<TabsTrigger value="revenue" className="flex items-center gap-2">
|
||||
<TrendingUp className="h-4 w-4" />
|
||||
Revenue
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="products" className="flex items-center gap-2">
|
||||
<Package className="h-4 w-4" />
|
||||
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">
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold">Customer Insights</h3>
|
||||
<p className="text-muted-foreground">
|
||||
Understand your customer base and behavior
|
||||
</p>
|
||||
</div>
|
||||
<CustomerInsightsChart />
|
||||
</TabsContent>
|
||||
<TabsContent value="revenue" className="space-y-6">
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold">Revenue Trends</h3>
|
||||
<p className="text-muted-foreground">
|
||||
Track your revenue performance over the selected time period
|
||||
</p>
|
||||
</div>
|
||||
<RevenueChart timeRange={timeRange} />
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="orders" className="space-y-6">
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold">Order Analytics</h3>
|
||||
<p className="text-muted-foreground">
|
||||
Detailed analysis of order patterns and status distribution
|
||||
</p>
|
||||
</div>
|
||||
<OrderAnalyticsChart timeRange={timeRange} />
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
<TabsContent value="products" className="space-y-6">
|
||||
<div>
|
||||
<h3 className="text-lg font-semibold">Product Performance</h3>
|
||||
<p className="text-muted-foreground">
|
||||
Analyze which products are performing best (all-time data)
|
||||
</p>
|
||||
</div>
|
||||
<ProductPerformanceChart />
|
||||
</TabsContent>
|
||||
|
||||
<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>
|
||||
);
|
||||
}
|
||||
@@ -145,136 +145,64 @@ export default function OrderAnalyticsChart({ timeRange }: OrderAnalyticsChartPr
|
||||
}
|
||||
|
||||
const totalOrders = data.statusDistribution.reduce((sum, item) => sum + item.count, 0);
|
||||
const totalRevenue = data.dailyOrders.reduce((sum, item) => sum + item.revenue, 0);
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Order Status Distribution */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Order Status Distribution</CardTitle>
|
||||
<CardDescription>
|
||||
Breakdown of orders by current status
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-4">
|
||||
{data.statusDistribution.map((status) => {
|
||||
const percentage = totalOrders > 0 ? (status.count / totalOrders * 100).toFixed(1) : '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 ? (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2">
|
||||
<BarChart3 className="h-5 w-5" />
|
||||
Order Analytics
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Order status distribution for the selected time period
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<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>
|
||||
) : (
|
||||
<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>
|
||||
<>
|
||||
{/* 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>
|
||||
|
||||
{/* 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}
|
||||
{/* Status Distribution */}
|
||||
{data.statusDistribution.map((status) => {
|
||||
const percentage = totalOrders > 0 ? (status.count / totalOrders * 100).toFixed(1) : '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>
|
||||
</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>
|
||||
);
|
||||
})}
|
||||
</>
|
||||
)}
|
||||
</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>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
@@ -39,32 +39,6 @@ export default function ProductPerformanceChart() {
|
||||
fetchProductData();
|
||||
}, [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) {
|
||||
return (
|
||||
<Card>
|
||||
@@ -157,7 +131,6 @@ export default function ProductPerformanceChart() {
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Product</TableHead>
|
||||
<TableHead>Stock</TableHead>
|
||||
<TableHead className="text-right">Sold</TableHead>
|
||||
<TableHead className="text-right">Revenue</TableHead>
|
||||
<TableHead className="text-right">Orders</TableHead>
|
||||
@@ -185,19 +158,6 @@ export default function ProductPerformanceChart() {
|
||||
</div>
|
||||
</div>
|
||||
</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">
|
||||
{product.totalSold.toFixed(2)}
|
||||
</TableCell>
|
||||
|
||||
@@ -53,14 +53,17 @@ export default function RevenueChart({ timeRange }: RevenueChartProps) {
|
||||
|
||||
// Transform data for Recharts
|
||||
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 {
|
||||
date: date.toISOString().split('T')[0], // YYYY-MM-DD format
|
||||
revenue: item.revenue || 0,
|
||||
orders: item.orders || 0,
|
||||
formattedDate: date.toLocaleDateString('en-GB', {
|
||||
weekday: 'short',
|
||||
month: 'short',
|
||||
day: 'numeric'
|
||||
day: 'numeric',
|
||||
timeZone: 'UTC'
|
||||
})
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user