"use client" import { useState, useEffect } from "react" import OrderStats from "./order-stats" import { getGreeting } from "@/lib/utils/general" import { statsConfig } from "@/config/dashboard" import { getRandomQuote } from "@/config/quotes" import type { OrderStatsData } from "@/lib/types" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { ShoppingCart, RefreshCcw } from "lucide-react" import { Button } from "@/components/ui/button" import { useToast } from "@/components/ui/use-toast" import { Skeleton } from "@/components/ui/skeleton" import { clientFetch } from "@/lib/api" interface ContentProps { username: string orderStats: OrderStatsData } interface TopProduct { id: string; name: string; price: number; image: string; count: number; revenue: number; } export default function Content({ username, orderStats }: ContentProps) { const [greeting, setGreeting] = useState(""); const [topProducts, setTopProducts] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const { toast } = useToast(); // Initialize with a random quote from the quotes config const [randomQuote, setRandomQuote] = useState(getRandomQuote()); // Fetch top-selling products data const fetchTopProducts = async () => { try { setIsLoading(true); const data = await clientFetch('/orders/top-products'); setTopProducts(data); } catch (err) { console.error("Error fetching top products:", err); setError(err instanceof Error ? err.message : "Failed to fetch top products"); toast({ title: "Error loading top products", description: "Please try refreshing the page", variant: "destructive" }); } finally { setIsLoading(false); } }; // Initialize greeting and fetch data on component mount useEffect(() => { setGreeting(getGreeting()); fetchTopProducts(); }, []); // Retry fetching top products data const handleRetry = () => { fetchTopProducts(); }; return (

{greeting}, {username}!

"{randomQuote.text}" — {randomQuote.author}

{/* Order Statistics */}
{statsConfig.map((stat) => ( ))}
{/* Best Selling Products Section */}
Your Best Selling Products Products with the highest sales from your store
{error && ( )}
{isLoading ? ( // Loading skeleton
{[...Array(5)].map((_, i) => (
))}
) : error ? ( // Error state
Failed to load products
) : topProducts.length === 0 ? ( // Empty state

No products sold yet

Your best-selling products will appear here after you make some sales.

) : ( // Data view
{topProducts.map((product) => (
{!product.image && ( )}

{product.name}

{product.count} sold
))}
)}
); }