"use client" import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs" import { cn } from "@/lib/utils" import { Bell, DollarSign, Filter, MoreHorizontal, Package, Search, ShoppingCart, TrendingDown, TrendingUp, Users, } from "lucide-react" import { useMemo, useState } from "react" import { useResponsiveOrientation } from "../hooks/use-responsive-orientation" import { formatDate, formatPrice } from "../lib/utils" import { Sidebar } from "./sidebar" type OrderStatus = "awaiting_shipment" | "processing" | "fulfilled" | "cancelled" interface Order { id: number customerName: string customerEmail: string customerAvatar: string productName: string productImage: string productPrice: number quantity: number shippingPrice: number discountRate: number totalPrice: number status: OrderStatus createdAt: string } const mockOrders: Order[] = [ { id: 1001, customerName: "Ahmed Hassan", customerEmail: "ahmed.hassan@email.com", customerAvatar: "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=100&h=100&fit=crop&crop=face", productName: "iPhone 15 Pro Max", productImage: "https://images.unsplash.com/photo-1592750475338-74b7b21085ab?w=100&h=100&fit=crop", productPrice: 1199, quantity: 1, shippingPrice: 25, discountRate: 0.1, totalPrice: 1104.1, status: "fulfilled", createdAt: "2024-01-15T10:30:00Z", }, { id: 1002, customerName: "Fatima Al-Zahra", customerEmail: "fatima.zahra@email.com", customerAvatar: "https://images.unsplash.com/photo-1517336714731-489689fd1ca8?w=100&h=100&fit=crop&crop=face", productName: "MacBook Air M3", productImage: "https://images.unsplash.com/photo-1517336714731-489689fd1ca8?w=100&h=100&fit=crop", productPrice: 1299, quantity: 1, shippingPrice: 0, discountRate: 0, totalPrice: 1299, status: "processing", createdAt: "2024-01-14T14:20:00Z", }, { id: 1003, customerName: "Omar Khaled", customerEmail: "omar.khaled@email.com", customerAvatar: "https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=100&h=100&fit=crop&crop=face", productName: "AirPods Pro 2", productImage: "https://images.unsplash.com/photo-1606220945770-b5b6c2c55bf1?w=100&h=100&fit=crop", productPrice: 249, quantity: 2, shippingPrice: 15, discountRate: 0.05, totalPrice: 487.1, status: "awaiting_shipment", createdAt: "2024-01-14T09:15:00Z", }, { id: 1004, customerName: "Layla Mohammed", customerEmail: "layla.mohammed@email.com", customerAvatar: "https://images.unsplash.com/photo-1544244015-0df4b3ffc6b0?w=100&h=100&fit=crop&crop=face", productName: "iPad Pro 12.9", productImage: "https://images.unsplash.com/photo-1544244015-0df4b3ffc6b0?w=100&h=100&fit=crop", productPrice: 1099, quantity: 1, shippingPrice: 20, discountRate: 0.15, totalPrice: 954.15, status: "cancelled", createdAt: "2024-01-13T16:45:00Z", }, { id: 1005, customerName: "Youssef Ali", customerEmail: "youssef.ali@email.com", customerAvatar: "", productName: "Apple Watch Series 9", productImage: "https://images.unsplash.com/photo-1523275335684-37898b6baf30?w=100&h=100&fit=crop", productPrice: 399, quantity: 1, shippingPrice: 10, discountRate: 0, totalPrice: 409, status: "fulfilled", createdAt: "2024-01-13T11:30:00Z", }, { id: 1006, customerName: "Nour Abdel Rahman", customerEmail: "nour.abdel@email.com", customerAvatar: "", productName: "Magic Keyboard", productImage: "https://images.unsplash.com/photo-1517336714731-489689fd1ca8?w=100&h=100&fit=crop", productPrice: 299, quantity: 1, shippingPrice: 12, discountRate: 0.08, totalPrice: 286.08, status: "processing", createdAt: "2024-01-12T13:20:00Z", }, ] const getStatusBadgeClassName = (status: OrderStatus) => { switch (status) { case "cancelled": return "bg-red-500 text-white hover:bg-red-600" case "processing": return "bg-yellow-500 text-white hover:bg-yellow-600" case "fulfilled": return "bg-green-500 text-white hover:bg-green-600" case "awaiting_shipment": return "bg-blue-500 text-white hover:bg-blue-600" default: return "bg-gray-500 text-white hover:bg-gray-600" } } const LABEL_MAP: Record = { awaiting_shipment: "Awaiting Shipment", processing: "Processing", cancelled: "Cancelled", fulfilled: "Fulfilled", } function DashboardHeader() { return (

E-commerce Admin

AD
) } function DashboardSummary({ orders }: { orders: Order[] }) { const stats = useMemo(() => { const totalSales = orders .filter((order) => order.status !== "cancelled") .reduce((sum, order) => sum + order.totalPrice, 0) const totalOrders = orders.length const totalCustomers = new Set(orders.map((order) => order.customerEmail)).size const avgOrderValue = totalSales / Math.max(totalOrders, 1) return { totalSales, totalOrders, totalCustomers, avgOrderValue, } }, [orders]) return (
Total Revenue
{formatPrice(stats.totalSales)}

+12.5% from last month

Total Orders
{stats.totalOrders}

+8.2% from last month

Customers
{stats.totalCustomers}

+5.1% from last month

Avg. Order Value
{formatPrice(stats.avgOrderValue)}

-2.1% from last month

) } export default function Dashboard() { const [filter, setFilter] = useState("all") const orientation = useResponsiveOrientation() const filteredOrders = useMemo(() => { if (filter === "all") return mockOrders return mockOrders.filter((order) => order.status === filter) }, [filter]) const calculateDiscountedPrice = (price: number, discount: number) => { return price * (1 - discount) } const calculateTotal = (order: Order) => { const discountedPrice = calculateDiscountedPrice(order.productPrice, order.discountRate) return discountedPrice * order.quantity + order.shippingPrice } return ( <>

Dashboard

Welcome back! Here's what's happening with your store today.

Recent Orders

All Orders Awaiting Processing Fulfilled Cancelled Orders Manage your orders and view their status.
Order Customer Product Price Discount Qty Shipping Total Date Status {filteredOrders.map((order) => ( #{order.id}
{order.customerName .split(" ") .map((n) => n[0]) .join("")}
{order.customerName}
{order.customerEmail}
{order.productName}
{order.productName}
{order.discountRate > 0 ? (
{formatPrice(order.productPrice)}
{formatPrice(calculateDiscountedPrice(order.productPrice, order.discountRate))}
) : (
{formatPrice(order.productPrice)}
)}
{order.discountRate > 0 ? ( {Math.round(order.discountRate * 100)}% OFF ) : ( No discount )} {order.quantity} {order.shippingPrice > 0 ? formatPrice(order.shippingPrice) : "Free"} {formatPrice(calculateTotal(order))} {formatDate(order.createdAt)} {LABEL_MAP[order.status]} View Order Edit Order Customer Details Cancel Order
))}
) }