"use client" import { useState, useEffect } from 'react'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { TrendingUp, ShoppingCart, Users, Package, DollarSign, BarChart3, PieChart, Activity, RefreshCw } from "lucide-react"; import { useToast } from "@/hooks/use-toast"; import RevenueChart from "./RevenueChart"; import ProductPerformanceChart from "./ProductPerformanceChart"; import CustomerInsightsChart from "./CustomerInsightsChart"; import OrderAnalyticsChart from "./OrderAnalyticsChart"; import MetricsCard from "./MetricsCard"; import { getAnalyticsOverviewWithStore, type AnalyticsOverview } from "@/lib/services/analytics-service"; import { formatGBP } from "@/utils/format"; interface AnalyticsDashboardProps { initialData: AnalyticsOverview; } export default function AnalyticsDashboard({ initialData }: AnalyticsDashboardProps) { const [data, setData] = useState(initialData); const [isLoading, setIsLoading] = useState(false); const [timeRange, setTimeRange] = useState('30'); const { toast } = useToast(); const refreshData = async () => { try { setIsLoading(true); const newData = await getAnalyticsOverviewWithStore(); setData(newData); toast({ title: "Data refreshed", description: "Analytics data has been updated successfully.", }); } catch (error) { toast({ title: "Error", description: "Failed to refresh analytics data.", variant: "destructive", }); } finally { setIsLoading(false); } }; const metrics = [ { title: "Total Revenue", value: formatGBP(data.revenue.total), description: "All-time revenue", icon: DollarSign, trend: data.revenue.monthly > 0 ? "up" as const : "neutral" as const, trendValue: `${formatGBP(data.revenue.monthly)} this month` }, { title: "Total Orders", value: data.orders.total.toLocaleString(), description: "All-time orders", icon: ShoppingCart, trend: data.orders.completed > 0 ? "up" as const : "neutral" as const, trendValue: `${data.orders.completed} completed` }, { title: "Unique Customers", value: data.customers.unique.toLocaleString(), description: "Total customers", icon: Users, trend: "neutral" as const, trendValue: "Lifetime customers" }, { title: "Products", value: data.products.total.toLocaleString(), description: "Active products", icon: Package, trend: "neutral" as const, trendValue: "In your store" } ]; return (
{/* Key Metrics Cards */}
{metrics.map((metric) => ( ))}
{/* Completion Rate Card */} Order Completion Rate Percentage of orders that have been successfully completed
{data.orders.completionRate}%
{data.orders.completed} / {data.orders.total}
{/* Analytics Tabs */} Revenue Products Customers Orders

Revenue Trends

Track your revenue performance over time

Product Performance

Analyze which products are performing best

Customer Insights

Understand your customer base and behavior

Order Analytics

Detailed analysis of order patterns and status distribution

); }