"use client" import { useState, useEffect } from "react" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Skeleton } from "@/components/ui/skeleton" import { Users, User, ArrowRight, DollarSign } from "lucide-react" import { getCustomerInsightsWithStore, formatGBP } from "@/lib/api" import { Avatar, AvatarFallback } from "@/components/ui/avatar" import Link from "next/link" interface RecentCustomersWidgetProps { settings?: { itemCount?: number showSpent?: boolean } } interface Customer { id: string name: string username?: string orderCount: number totalSpent: number } export default function RecentCustomersWidget({ settings }: RecentCustomersWidgetProps) { const itemCount = settings?.itemCount || 5 const showSpent = settings?.showSpent !== false const [customers, setCustomers] = useState([]) const [isLoading, setIsLoading] = useState(true) const [error, setError] = useState(null) const fetchCustomers = async () => { try { setIsLoading(true) setError(null) // The API returns topCustomers, but we'll use 'recent' sorting to show new engagement const response = await getCustomerInsightsWithStore(1, itemCount, "recent") const mappedCustomers = (response.topCustomers || []).map((c: any) => ({ id: c._id, name: c.displayName || c.username || `Customer ${c._id.slice(-4)}`, username: c.username, orderCount: c.orderCount || 0, totalSpent: c.totalSpent || 0 })) setCustomers(mappedCustomers) } catch (err) { console.error("Error fetching customers:", err) setError("Failed to load customer data") } finally { setIsLoading(false) } } useEffect(() => { fetchCustomers() }, [itemCount]) if (isLoading) { return ( {[1, 2, 3].map((i) => (
))}
) } return (
Recent Customers Latest and newest connections
{error ? (

{error}

) : customers.length === 0 ? (

No customers yet

This widget will populate once people start browsing and buying.

) : (
{customers.map((customer) => (
{customer.name.slice(0, 2).toUpperCase()}

{customer.name}

{customer.orderCount} order{customer.orderCount !== 1 ? 's' : ''}
{showSpent && (
{formatGBP(customer.totalSpent)}
Total Spent
)}
))}
)}
) }