import { getAllProducts } from '@/lib/mdx'; import { getTranslations } from 'next-intl/server'; import Image from 'next/image'; import { RelatedProductLink } from './RelatedProductLink'; interface RelatedProductsProps { currentSlug: string; categories: string[]; locale: string; } export default async function RelatedProducts({ currentSlug, categories, locale, }: RelatedProductsProps) { const products = await getAllProducts(locale); const t = await getTranslations('Products'); // Filter products: same category, not current product const related = products .filter( (p) => p.slug !== currentSlug && p.frontmatter.categories.some((cat) => categories.includes(cat)), ) .slice(0, 3); // Limit to 3 for better spacing if (related.length === 0) return null; return (

{t('relatedProductsTitle') || 'Related Products'}

{related.map((product) => { // Find the category slug for the link const categorySlugs = [ 'low-voltage-cables', 'medium-voltage-cables', 'high-voltage-cables', 'solar-cables', ]; const catSlug = categorySlugs.find((slug) => { const key = slug .replace(/-cables$/, '') .replace(/-([a-z])/g, (g) => g[1].toUpperCase()); const title = t(`categories.${key}.title`); return product.frontmatter.categories.some( (cat) => cat.toLowerCase().replace(/\s+/g, '-') === slug || cat === title, ); }) || 'low-voltage-cables'; return (
{product.frontmatter.images?.[0] ? ( <> {product.frontmatter.title}
) : (
No Image
)}
{product.frontmatter.categories.slice(0, 1).map((cat: any, idx: number) => ( {cat} ))}

{product.frontmatter.title}

{t('details')}
); })}
); }