import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Edit, Trash, AlertTriangle, CheckCircle, AlertCircle, Calculator, Copy, PackageX, Archive } from "lucide-react"; import { Button } from "@/components/ui/button"; import React, { useState, useEffect } from "react"; import { Product } from "@/models/products"; import { Badge } from "@/components/ui/badge"; import { Switch } from "@/components/ui/switch"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { motion, AnimatePresence } from "framer-motion"; const getProductImageUrl = (product: Product) => { if (!product.image) return null; if (typeof product.image === 'string' && product.image.startsWith('http')) return product.image; // Use the API endpoint to serve the image return `${process.env.NEXT_PUBLIC_API_URL}/products/${product._id}/image`; }; interface ProductTableProps { products: Product[]; loading: boolean; onEdit: (product: Product) => void; onClone?: (product: Product) => void; onDelete: (productId: string) => void; onToggleEnabled: (productId: string, enabled: boolean) => void; onProfitAnalysis?: (productId: string, productName: string) => void; getCategoryNameById: (categoryId: string) => string; } const ProductTable = ({ products, loading, onEdit, onClone, onDelete, onToggleEnabled, onProfitAnalysis, getCategoryNameById, }: ProductTableProps) => { // Separate enabled and disabled products const enabledProducts = products.filter((p) => p.enabled !== false); const disabledProducts = products.filter((p) => p.enabled === false); const sortByCategory = (productList: Product[]) => { return [...productList].sort((a, b) => { const categoryNameA = getCategoryNameById(a.category); const categoryNameB = getCategoryNameById(b.category); return categoryNameA.localeCompare(categoryNameB); }); }; const sortedEnabledProducts = sortByCategory(enabledProducts); const sortedDisabledProducts = sortByCategory(disabledProducts); const getStockIcon = (product: Product) => { if (!product.stockTracking) return null; if (product.stockStatus === "out_of_stock") { return ; } else if (product.stockStatus === "low_stock") { return ; } else { return ; } }; const renderProductRow = (product: Product, index: number, isDisabled: boolean = false) => (
{getProductImageUrl(product) ? ( {product.name} ) : ( {product.name.charAt(0).toUpperCase()} )}
{product.name}
{getCategoryNameById(product.category)}
{getCategoryNameById(product.category)} {product.unitType} {product.stockTracking ? (
{getStockIcon(product)} {product.currentStock !== undefined ? product.currentStock : 0}
) : ( Unlimited )}
onToggleEnabled(product._id as string, checked) } className="data-[state=checked]:bg-primary" />
{onProfitAnalysis && ( )} {onClone && ( )}
); const [searchQuery, setSearchQuery] = useState(""); // Browser detection const [isFirefox, setIsFirefox] = useState(false); useEffect(() => { const ua = navigator.userAgent.toLowerCase(); setIsFirefox(ua.includes("firefox") && !ua.includes("chrome")); }, []); const renderTableHeader = () => ( Product Category Unit Stock Enabled Actions ); return (
{/* Enabled Products Table */}
Active Products {sortedEnabledProducts.length}
{renderTableHeader()} {isFirefox ? ( loading ? (
Loading products...
) : sortedEnabledProducts.length > 0 ? ( sortedEnabledProducts.map((product, index) => renderProductRow(product, index)) ) : (

No active products found

) ) : ( {loading ? (
Loading products...
) : sortedEnabledProducts.length > 0 ? ( sortedEnabledProducts.map((product, index) => renderProductRow(product, index)) ) : (

No active products found

)} )}
{/* Disabled Products Section */} {!loading && disabledProducts.length > 0 && ( Archived / Disabled {sortedDisabledProducts.length}
{renderTableHeader()} {sortedDisabledProducts.map((product, index) => renderProductRow(product, index, true), )}
)}
); }; export default ProductTable;