import { getAllProducts } from '@/lib/mdx'; import { mapFileSlugToTranslated } from '@/lib/slugs'; import { getTranslations } from 'next-intl/server'; import Image from 'next/image'; import Link from 'next/link'; interface RelatedProductsProps { currentSlug: string; categories: string[]; locale: string; } export default async function RelatedProducts({ currentSlug, categories, locale }: RelatedProductsProps) { const allProducts = await getAllProducts(locale); const t = await getTranslations('Products'); // Filter products: same category, not current product const related = allProducts .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(async (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'; const translatedProductSlug = await mapFileSlugToTranslated(product.slug, locale); return (
{product.frontmatter.images?.[0] ? ( <> {product.frontmatter.title}
) : (
No Image
)}
{product.frontmatter.categories.slice(0, 1).map((cat, idx) => ( {cat} ))}

{product.frontmatter.title}

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