All files / src/components/features/admin/analytics/TopProductsCard index.tsx

0% Statements 0/150
100% Branches 0/0
0% Functions 0/1
0% Lines 0/150

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151                                                                                                                                                                                                                                                                                                             
/**
 * TopProductsCard Component
 *
 * Displays top products by revenue with percentage bars.
 */

"use client";

import { Icon } from "@/components/ui/icons";
import { cn } from "@/lib/core";

export interface TopProduct {
  productId: string;
  productName: string;
  revenue: number;
  unitsSold: number;
  percentage: number;
}

export interface TopProductsCardProps {
  products: TopProduct[];
  isLoading?: boolean;
  title?: string;
  limit?: number;
  className?: string;
}

function formatCurrency(value: number): string {
  return new Intl.NumberFormat("en-US", {
    style: "currency",
    currency: "USD",
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
  }).format(value);
}

export function TopProductsCard({
  products,
  isLoading,
  title = "Top Products",
  limit = 5,
  className,
}: TopProductsCardProps) {
  if (isLoading) {
    return (
      <div className={cn("bg-white dark:bg-gray-900 rounded-lg border border-gray-200 dark:border-gray-800 p-6", className)}>
        <div className="animate-pulse space-y-4">
          <div className="h-6 bg-gray-200 dark:bg-gray-700 rounded w-1/3"></div>
          {[...Array(5)].map((_, i) => (
            <div key={i} className="space-y-2">
              <div className="h-4 bg-gray-200 dark:bg-gray-700 rounded w-2/3"></div>
              <div className="h-2 bg-gray-200 dark:bg-gray-700 rounded"></div>
            </div>
          ))}
        </div>
      </div>
    );
  }

  const displayProducts = products.slice(0, limit);

  if (displayProducts.length === 0) {
    return (
      <div className={cn("bg-white dark:bg-gray-900 rounded-lg border border-gray-200 dark:border-gray-800 p-6", className)}>
        <h3 className="text-lg font-semibold text-gray-900 dark:text-white mb-4">{title}</h3>
        <div className="text-center py-8">
          <Icon name="package" className="h-12 w-12 mx-auto text-gray-300 dark:text-gray-600 mb-2" />
          <p className="text-gray-500 dark:text-gray-400">No product data available</p>
        </div>
      </div>
    );
  }

  const maxPercentage = Math.max(...displayProducts.map((p) => p.percentage), 1);

  return (
    <div className={cn("bg-white dark:bg-gray-900 rounded-lg border border-gray-200 dark:border-gray-800 overflow-hidden", className)}>
      <div className="p-6 border-b border-gray-200 dark:border-gray-800">
        <div className="flex items-center justify-between">
          <h3 className="text-lg font-semibold text-gray-900 dark:text-white">{title}</h3>
          <span className="text-sm text-gray-500 dark:text-gray-400">
            {products.length} products
          </span>
        </div>
      </div>

      <div className="p-6 space-y-4">
        {displayProducts.map((product, index) => {
          const barWidth = (product.percentage / maxPercentage) * 100;

          return (
            <div key={product.productId} className="space-y-2">
              <div className="flex items-center justify-between">
                <div className="flex items-center gap-2 min-w-0">
                  <span className="flex-shrink-0 w-6 h-6 flex items-center justify-center rounded-full bg-gray-100 dark:bg-gray-800 text-xs font-medium text-gray-600 dark:text-gray-400">
                    {index + 1}
                  </span>
                  <span className="text-sm font-medium text-gray-900 dark:text-white truncate">
                    {product.productName}
                  </span>
                </div>
                <span className="flex-shrink-0 text-sm font-semibold text-gray-900 dark:text-white ml-2">
                  {formatCurrency(product.revenue)}
                </span>
              </div>

              <div className="flex items-center gap-2">
                <div className="flex-1 h-2 bg-gray-100 dark:bg-gray-800 rounded-full overflow-hidden">
                  <div
                    className={cn(
                      "h-full rounded-full transition-all duration-500",
                      index === 0
                        ? "bg-primary-500"
                        : index === 1
                        ? "bg-primary-400"
                        : index === 2
                        ? "bg-primary-300"
                        : "bg-gray-300 dark:bg-gray-600"
                    )}
                    style={{ width: `${barWidth}%` }}
                  />
                </div>
                <span className="flex-shrink-0 text-xs text-gray-500 dark:text-gray-400 w-12 text-right">
                  {product.percentage.toFixed(1)}%
                </span>
              </div>

              <div className="flex items-center justify-between text-xs text-gray-500 dark:text-gray-400">
                <span>{product.unitsSold.toLocaleString()} units sold</span>
                <span>
                  {formatCurrency(product.revenue / Math.max(product.unitsSold, 1))}/unit
                </span>
              </div>
            </div>
          );
        })}
      </div>

      {products.length > limit && (
        <div className="px-6 py-3 border-t border-gray-200 dark:border-gray-800 bg-gray-50 dark:bg-gray-800/50">
          <button className="text-sm text-primary-600 dark:text-primary-400 hover:underline">
            View all {products.length} products →
          </button>
        </div>
      )}
    </div>
  );
}

export default TopProductsCard;