import ProText from "components/ProText"; import ProTitle from "components/ProTitle"; import { useTranslation } from "react-i18next"; import { useAppSelector } from "redux/hooks"; import { Product } from "utils/types/appTypes"; import styles from "./MenuList.module.css"; import ProductCard from "pages/menu/components/MenuList/ProductCard.tsx"; import { ProductBottomSheet } from "pages/product/components/ProductBottomSheet"; import { useState } from "react"; import { Category } from "utils/types/appTypes"; interface MenuListProps { data: | { products: Product[]; categories: { id: number; name: string; image?: string }[]; } | undefined; categoryRefs: React.RefObject<{ [key: number]: HTMLDivElement | null }>; } export function MenuList({ data, categoryRefs }: MenuListProps) { const { isRTL } = useAppSelector((state) => state.locale); const products = data?.products; const [isBottomSheetOpen, setIsBottomSheetOpen] = useState(false); const { t } = useTranslation(); const { themeName } = useAppSelector((state) => state.theme); // Show error state if data exists but has no products if (data && (!data.products || data.products.length === 0)) { return (
{t("menu.noMenuItemsAvailable")}
); } // Group products by category const productsByCategory = products?.reduce( (acc, product) => { if (product.categoryId && !acc[product?.categoryId]) { acc[product?.categoryId] = []; } acc[product?.categoryId || 0].push(product); return acc; }, {} as Record, ); return ( <>
{data?.categories?.map((category: Category, index) => { const categoryProducts = productsByCategory?.[category.id] || []; if (categoryProducts.length === 0) return null; return (
{ if (categoryRefs.current) { categoryRefs.current[category.id] = el; } }} style={{ marginBottom: "2rem" }} > {index !== 0 && ( {isRTL ? category.nameAR : category.nameEN} )}
{categoryProducts.map((item: Product) => ( ))}
); })} setIsBottomSheetOpen(false)} />
); }