"use client" import { useState, useEffect } from "react" import OrderStats from "./order-stats" import QuickActions from "./quick-actions" import RecentActivity from "./recent-activity" import { WidgetSettings } from "./widget-settings" import { WidgetSettingsModal } from "./widget-settings-modal" import { DashboardEditor } from "./dashboard-editor" import { DraggableWidget } from "./draggable-widget" import RevenueWidget from "./revenue-widget" import LowStockWidget from "./low-stock-widget" import RecentCustomersWidget from "./recent-customers-widget" import PendingChatsWidget from "./pending-chats-widget" 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, ArrowRight } 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" import { motion } from "framer-motion" import Link from "next/link" import { useWidgetLayout, WidgetConfig } from "@/hooks/useWidgetLayout" interface ContentProps { username: string orderStats: OrderStatsData } interface TopProduct { id: string; name: string; price: number | 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(); const { widgets, toggleWidget, moveWidget, reorderWidgets, resetLayout, isWidgetVisible, updateWidgetSettings } = useWidgetLayout(); const [configuredWidget, setConfiguredWidget] = useState(null); // Initialize with a default quote to match server-side rendering, then randomize on client const [randomQuote, setRandomQuote] = useState({ text: "Loading wisdom...", author: "..." }); useEffect(() => { // Determine quote on client-side to avoid hydration mismatch setRandomQuote(getRandomQuote()); }, []); 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"); } finally { setIsLoading(false); } }; const handleRetry = () => { fetchTopProducts(); }; const renderWidget = (widget: WidgetConfig) => { switch (widget.id) { case "quick-actions": return (

Quick Actions

); case "overview": return (

Overview

{statsConfig.map((stat, index) => ( ))}
); case "recent-activity": return (

Recent Activity

); case "top-products": return (

Top Performing Listings

Top Performing Listings Your products with the highest sales volume
{error && ( )}
{isLoading ? (
{[...Array(5)].map((_, i) => (
))}
) : error ? (
Failed to load product insights
) : topProducts.length === 0 ? (

Begin your sales journey

Your top performing listings will materialize here as you receive orders.

) : (
{topProducts.map((product, index) => (
{!product.image && ( )}

{product.name}

£{(Number(Array.isArray(product.price) ? product.price[0] : product.price) || 0).toFixed(2)}
{product.count}
Units Sold
£{product.revenue.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}
))}
)}
); case "revenue-chart": return ; case "low-stock": return ; case "recent-customers": return ; case "pending-chats": return ; default: return null; } }; useEffect(() => { setGreeting(getGreeting()); fetchTopProducts(); }, []); return (

{greeting}, {username}!

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

setConfiguredWidget(widget)} />
{ }} onReorder={reorderWidgets} onReset={resetLayout} >
{widgets.map((widget) => { if (!widget.visible) return null; return ( setConfiguredWidget(widget)} onToggleVisibility={() => toggleWidget(widget.id)} > {renderWidget(widget)} ); })}
{/* Widget Settings Modal */} !open && setConfiguredWidget(null)} onSave={(widgetId, settings) => { updateWidgetSettings(widgetId, settings); }} />
); }