"use client"; import React, { useState, useEffect, useRef } from 'react'; import Link from 'next/link'; import { Swiper, SwiperSlide } from 'swiper/react'; import { Navigation, A11y } from 'swiper/modules'; import type { Swiper as SwiperType } from 'swiper'; import { ProductCard } from '../components/ProductCard'; import { ArrowRight, ChevronLeft, ChevronRight } from 'lucide-react'; import { Product } from '../types'; import { getImageUrl } from '../lib/utils'; import { getSectionMeta, getSectionTitle } from '../lib/homepageDefaults'; import 'swiper/css'; import 'swiper/css/navigation'; const DEFAULT_BANNER_IMAGE = 'https://images.unsplash.com/photo-1610945415295-d9bbf067e59c?w=600&auto=format&fit=crop&q=80'; interface RecentlyLaunchedSectionProps { config?: any; products?: Product[]; loading?: boolean; error?: boolean; } export const RecentlyLaunchedSection: React.FC = ({ config, products, error, }) => { const [carouselProducts, setCarouselProducts] = useState([]); const [canSlide, setCanSlide] = useState(false); const swiperRef = useRef(null); useEffect(() => { const pinnedSlugs: string[] = config?.metadata_json?.pinned_product_slugs || config?.metadata?.pinned_product_slugs || []; if (pinnedSlugs.length > 0 && products && products.length > 0) { const pinned = pinnedSlugs .map((slug) => products.find((p) => p.slug === slug || p.id === slug)) .filter(Boolean) as Product[]; setCarouselProducts(pinned.length > 0 ? pinned : products.slice(0, 8)); return; } const rawItems = config?.metadata_json?.items || config?.metadata?.items; if (rawItems && rawItems.length > 0) { setCarouselProducts( rawItems.map((it: any) => ({ id: it.id || String(Math.random()), name: it.title || it.name, price: it.price || 0, oldPrice: it.oldPrice || it.originalPrice, discount: it.discount, image: getImageUrl(it.image), rating: it.rating || 5.0, reviewsCount: it.reviewsCount || 12, category: 'Accessories', brand: 'Tech', slug: it.slug || it.id, })) ); } else if (products && products.length > 0) { const recent = products.filter((p) => p.isRecentlyLaunched); setCarouselProducts(recent.length > 0 ? recent : products.slice(0, 8)); } else { setCarouselProducts([]); } }, [config, products, error]); const title = getSectionTitle(config, 'recently_launched'); const bannerTitle = config?.metadata_json?.banner_title || config?.metadata?.banner_title || 'iPhone Prodigy'; const bannerSubtitle = config?.metadata_json?.banner_subtitle || config?.metadata?.banner_subtitle || 'Performance in Motion'; const rawBannerImage = config?.metadata_json?.banner_image || config?.metadata?.banner_image || ''; const bannerImage = getImageUrl(rawBannerImage) || DEFAULT_BANNER_IMAGE; const bannerLink = config?.metadata_json?.banner_link || config?.metadata?.banner_link || '/shop'; const syncOverflow = (swiper: SwiperType) => { if (!swiper) return; setCanSlide(!swiper.isLocked && carouselProducts.length > 0); }; useEffect(() => { const swiper = swiperRef.current; if (!swiper) return; setTimeout(() => { if (swiper.navigation) { try { swiper.navigation.destroy(); swiper.navigation.init(); swiper.navigation.update(); } catch (e) {} } syncOverflow(swiper); }, 0); }, [carouselProducts.length]); if (carouselProducts.length === 0) { return (

{title}

Recently launched products will appear here once they are in the catalog.
); } return (
{/* Header — title + View All, line underneath (not joined to content) */}

{title}

View All
{/* Banner + product strip */}
{/* Lavender feature card */}

{bannerTitle}

{bannerSubtitle}

Shop Now
{bannerTitle}
{/* Product carousel with vertical dividers */}
{/* Blue circular arrows — between banner & strip / far right */} { swiperRef.current = swiper; setTimeout(() => { if ( swiper && swiper.params && swiper.params.navigation && typeof swiper.params.navigation !== 'boolean' ) { swiper.params.navigation.prevEl = '.rl-swiper-prev'; swiper.params.navigation.nextEl = '.rl-swiper-next'; } if (swiper?.navigation) { try { swiper.navigation.destroy(); swiper.navigation.init(); swiper.navigation.update(); } catch (e) {} } syncOverflow(swiper); }, 0); }} onResize={syncOverflow} onBreakpoint={syncOverflow} onLock={() => setCanSlide(false)} onUnlock={() => setCanSlide(true)} navigation={{ prevEl: '.rl-swiper-prev', nextEl: '.rl-swiper-next', }} watchOverflow spaceBetween={0} slidesPerView={2} breakpoints={{ 640: { slidesPerView: 2 }, 900: { slidesPerView: 3 }, 1200: { slidesPerView: 4 }, }} className="h-full" > {carouselProducts.map((product) => ( ))}
); };