import { getAllProducts } from '@/lib/products'; import { getTranslations } from 'next-intl/server'; import Image from 'next/image'; import { RelatedProductLink } from './RelatedProductLink'; import { mapFileSlugToTranslated } from '@/lib/slugs'; 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'); const productsSlug = await mapFileSlugToTranslated('products', locale); // 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; // Pre-calculate translated slugs for related products const relatedWithTranslatedSlugs = await Promise.all( 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 catFileSlug = categorySlugs.find((slug) => { return product.frontmatter.categories.some( (cat) => cat.toLowerCase().replace(/\s+/g, '-') === slug, ); }) || 'low-voltage-cables'; const catSlug = await mapFileSlugToTranslated(catFileSlug, locale); return { ...product, catSlug, }; }), ); return (

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

{relatedWithTranslatedSlugs.map((product) => { 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')}
); })}
); }