"use client" import { useEffect, useState } from "react" import { TrendingUp, Package, ShoppingCart, Users, ArrowUpRight, Loader2, Calendar, ChevronDown } from "lucide-react" import { XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, AreaChart, Area } from "recharts" import { useTheme } from "@/components/ThemeProvider" import { cn } from "@/lib/utils" type TimeRange = "7days" | "30days" | "1year" export default function DashboardPage() { const { theme } = useTheme() const isLight = theme === "light" const [stats, setStats] = useState([ { label: "Total Sales", value: "$0.00", icon: TrendingUp, color: "text-indigo-600", bg: "bg-indigo-100" }, { label: "Active Products", value: "0", icon: Package, color: "text-emerald-600", bg: "bg-emerald-100" }, { label: "Total Orders", value: "0", icon: ShoppingCart, color: "text-blue-600", bg: "bg-blue-100" }, { label: "Customers", value: "0", icon: Users, color: "text-orange-600", bg: "bg-orange-100" }, ]) const [chartData, setChartData] = useState<{ date: string; amount: number }[]>([]) const [loading, setLoading] = useState(true) const [timeRange, setTimeRange] = useState("7days") const fetchDashboardData = async () => { try { setLoading(true) const [productsRes, ordersRes, customersRes] = await Promise.all([ fetch("/api/shopify/inventory").catch(() => ({ ok: false, json: async () => ({ products: [], message: "Failed to fetch products" }) })), fetch("/api/shopify/orders").catch(() => ({ ok: false, json: async () => ({ orders: [], message: "Failed to fetch orders" }) })), fetch("/api/shopify/customers").catch(() => ({ ok: false, json: async () => ({ customers: [], message: "Failed to fetch customers" }) })) ]) const [productsData, ordersData, customersData] = await Promise.all([ productsRes.json().catch(() => ({ products: [] })), ordersRes.json().catch(() => ({ orders: [] })), customersRes.json().catch(() => ({ customers: [] })) ]) const orders = ordersData.orders || [] const totalSales = orders.reduce((sum: number, order: { total_price?: string }) => sum + parseFloat(order.total_price ?? "0"), 0) || 0 // Helper to generate date range const generateDateRange = (range: TimeRange) => { const dates = [] const now = new Date() if (range === "1year") { for (let i = 11; i >= 0; i--) { const d = new Date(now.getFullYear(), now.getMonth() - i, 1) dates.push(d.toLocaleDateString("en-US", { month: "short", year: "2-digit" })) } } else { const count = range === "30days" ? 30 : 7 for (let i = count - 1; i >= 0; i--) { const d = new Date(now.getFullYear(), now.getMonth(), now.getDate() - i) dates.push(d.toLocaleDateString("en-US", { month: "short", day: "numeric" })) } } return dates } const rangeLabels = generateDateRange(timeRange) const salesByDate: Record = {} orders.forEach((order: { created_at?: string; total_price?: string }) => { const orderDate = new Date(order.created_at ?? "") let label = "" if (timeRange === "1year") { label = orderDate.toLocaleDateString("en-US", { month: "short", year: "2-digit" }) } else { label = orderDate.toLocaleDateString("en-US", { month: "short", day: "numeric" }) } if (rangeLabels.includes(label)) { salesByDate[label] = (salesByDate[label] || 0) + parseFloat(order.total_price ?? "0") } }) const processedChartData = rangeLabels.map(date => ({ date, amount: Math.round((salesByDate[date] || 0) * 100) / 100 })) setChartData(processedChartData) setStats([ { label: "Total Sales", value: `$${totalSales.toLocaleString()}`, icon: TrendingUp, color: "text-indigo-600", bg: "bg-indigo-100" }, { label: "Active Products", value: productsData.products?.length.toString() || "0", icon: Package, color: "text-emerald-600", bg: "bg-emerald-100" }, { label: "Total Orders", value: orders.length.toString() || "0", icon: ShoppingCart, color: "text-blue-600", bg: "bg-blue-100" }, { label: "Customers", value: customersData.customers?.length.toString() || "0", icon: Users, color: "text-orange-600", bg: "bg-orange-100" }, ]) } catch (error) { console.error("Dashboard stats fetch error:", error) } finally { setLoading(false) } } useEffect(() => { fetchDashboardData() }, [timeRange]) const statCardStyle: Record = { "Total Sales": { bg: "bg-indigo-500/20 border-indigo-400/30", icon: "text-indigo-400", lightBg: "bg-indigo-50 border-indigo-100", lightIcon: "text-indigo-600" }, "Active Products": { bg: "bg-emerald-500/20 border-emerald-400/30", icon: "text-emerald-400", lightBg: "bg-emerald-50 border-emerald-100", lightIcon: "text-emerald-600" }, "Total Orders": { bg: "bg-blue-500/20 border-blue-400/30", icon: "text-blue-400", lightBg: "bg-blue-50 border-blue-100", lightIcon: "text-blue-600" }, "Customers": { bg: "bg-orange-500/20 border-orange-400/30", icon: "text-orange-400", lightBg: "bg-orange-50 border-orange-100", lightIcon: "text-orange-600" }, } return (

Overview

Welcome back! Here's a quick look at your live Shopify stats.

{stats.map((stat: (typeof stats)[number]) => (
{loading ? : }

{stat.label}

{stat.value}

))}

Recent Sales Trend

Revenue analysis over time

{(["7days", "30days", "1year"] as const).map((r) => ( ))}
{loading ? (
) : chartData.length > 0 ? ( `$${val > 999 ? (val / 1000).toFixed(1) + "k" : val}`} /> [value !== undefined ? `$${value.toLocaleString()}` : "$0", "Revenue"]} /> ) : (
No sales data found for this period.
)}
) }