175 lines
5.7 KiB
TypeScript
175 lines
5.7 KiB
TypeScript
"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<AnalyticsOverview>(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 (
|
|
<div className="space-y-6">
|
|
{/* Key Metrics Cards */}
|
|
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
|
|
{metrics.map((metric) => (
|
|
<MetricsCard key={metric.title} {...metric} />
|
|
))}
|
|
</div>
|
|
|
|
{/* Completion Rate Card */}
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle className="flex items-center gap-2">
|
|
<Activity className="h-5 w-5" />
|
|
Order Completion Rate
|
|
</CardTitle>
|
|
<CardDescription>
|
|
Percentage of orders that have been successfully completed
|
|
</CardDescription>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="flex items-center gap-4">
|
|
<div className="text-3xl font-bold">
|
|
{data.orders.completionRate}%
|
|
</div>
|
|
<div className="flex-1">
|
|
<div className="w-full bg-secondary rounded-full h-2">
|
|
<div
|
|
className="bg-primary h-2 rounded-full transition-all duration-300"
|
|
style={{ width: `${data.orders.completionRate}%` }}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<Badge variant="secondary">
|
|
{data.orders.completed} / {data.orders.total}
|
|
</Badge>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
{/* Analytics Tabs */}
|
|
<div className="space-y-6">
|
|
<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">
|
|
<RevenueChart timeRange={timeRange} />
|
|
</TabsContent>
|
|
|
|
<TabsContent value="products" className="space-y-6">
|
|
<ProductPerformanceChart />
|
|
</TabsContent>
|
|
|
|
<TabsContent value="customers" className="space-y-6">
|
|
<CustomerInsightsChart />
|
|
</TabsContent>
|
|
|
|
<TabsContent value="orders" className="space-y-6">
|
|
<OrderAnalyticsChart timeRange={timeRange} />
|
|
</TabsContent>
|
|
</Tabs>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|