|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState } from 'react'; |
| 4 | +import { ChevronLeft, ChevronRight } from 'lucide-react'; |
| 5 | + |
| 6 | +interface ProductDisplayProps { |
| 7 | + name: string; |
| 8 | + description: string; |
| 9 | + images: string[]; |
| 10 | + buyNowLink: string; |
| 11 | +} |
| 12 | + |
| 13 | +// Simple markdown parser for basic formatting |
| 14 | +const parseMarkdown = (text: string) => { |
| 15 | + return text |
| 16 | + .replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>') |
| 17 | + .replace(/\*(.*?)\*/g, '<em>$1</em>') |
| 18 | + .replace(/\n/g, '<br />'); |
| 19 | +}; |
| 20 | + |
| 21 | +export const ProductDisplay = ({ |
| 22 | + name, |
| 23 | + description, |
| 24 | + images = [], // Provide default empty array |
| 25 | + buyNowLink, |
| 26 | +}: ProductDisplayProps) => { |
| 27 | + const [currentIndex, setCurrentIndex] = useState(0); |
| 28 | + |
| 29 | + const nextImage = () => { |
| 30 | + if (!images || images.length <= 1) return; |
| 31 | + setCurrentIndex((prevIndex) => |
| 32 | + prevIndex === images.length - 1 ? 0 : prevIndex + 1, |
| 33 | + ); |
| 34 | + }; |
| 35 | + |
| 36 | + const prevImage = () => { |
| 37 | + if (!images || images.length <= 1) return; |
| 38 | + setCurrentIndex((prevIndex) => |
| 39 | + prevIndex === 0 ? images.length - 1 : prevIndex - 1, |
| 40 | + ); |
| 41 | + }; |
| 42 | + |
| 43 | + return ( |
| 44 | + <div className="w-full mx-auto p-4 sm:p-6 max-h-[800px]"> |
| 45 | + <div className="grid grid-cols-1 md:grid-cols-2 gap-6 md:gap-8 lg:gap-12"> |
| 46 | + {/* Product Image */} |
| 47 | + <div className="relative w-full"> |
| 48 | + <div className="relative aspect-square overflow-hidden rounded-2xl border"> |
| 49 | + <img |
| 50 | + src={ |
| 51 | + images && images.length > 0 |
| 52 | + ? images[currentIndex] |
| 53 | + : '/placeholder.svg' |
| 54 | + } |
| 55 | + alt={`${name} - View ${currentIndex + 1}`} |
| 56 | + className={`object-cover transition-all duration-300 p-4`} |
| 57 | + /> |
| 58 | + {images.length > 1 && ( |
| 59 | + <> |
| 60 | + <button |
| 61 | + onClick={prevImage} |
| 62 | + className="absolute left-3 top-1/2 -translate-y-1/2 bg-white/80 hover:bg-white rounded-full p-1 shadow-md transition-all duration-200" |
| 63 | + aria-label="Previous image" |
| 64 | + > |
| 65 | + <ChevronLeft className="size-4 text-gray-700" /> |
| 66 | + </button> |
| 67 | + <button |
| 68 | + onClick={nextImage} |
| 69 | + className="absolute right-3 top-1/2 -translate-y-1/2 bg-white/80 hover:bg-white rounded-full p-1 shadow-md transition-all duration-200" |
| 70 | + aria-label="Next image" |
| 71 | + > |
| 72 | + <ChevronRight className="size-4 text-gray-700" /> |
| 73 | + </button> |
| 74 | + </> |
| 75 | + )} |
| 76 | + {images.length > 1 && ( |
| 77 | + <div className="absolute bottom-3 left-0 right-0 flex justify-center gap-2 py-2"> |
| 78 | + {images.map((_, index) => ( |
| 79 | + <button |
| 80 | + key={index} |
| 81 | + onClick={() => setCurrentIndex(index)} |
| 82 | + className={`w-3 h-3 rounded-full transition-colors border ${ |
| 83 | + currentIndex === index |
| 84 | + ? 'bg-black border-black' |
| 85 | + : 'bg-white border-gray-700 hover:bg-gray-200' |
| 86 | + }`} |
| 87 | + aria-label={`View image ${index + 1}`} |
| 88 | + /> |
| 89 | + ))} |
| 90 | + </div> |
| 91 | + )} |
| 92 | + </div> |
| 93 | + </div> |
| 94 | + |
| 95 | + {/* Product Details */} |
| 96 | + <div className="flex flex-col justify-between h-full"> |
| 97 | + <div> |
| 98 | + <h1 className="text-2xl sm:text-3xl md:text-4xl font-bold text-gray-900"> |
| 99 | + {name} |
| 100 | + </h1> |
| 101 | + {/* Description */} |
| 102 | + <div className="mt-4"> |
| 103 | + <h2 className="text-md font-medium text-gray-900 text-wrap"> |
| 104 | + Description |
| 105 | + </h2> |
| 106 | + <div |
| 107 | + className="mt-2 text-gray-600 text-sm sm:text-md overflow-auto prose prose-sm max-w-none" |
| 108 | + dangerouslySetInnerHTML={{ __html: parseMarkdown(description) }} |
| 109 | + /> |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + {/* Buy Now Button */} |
| 113 | + <div className="mt-6 md:mt-8"> |
| 114 | + <a |
| 115 | + href={buyNowLink} |
| 116 | + target="_blank" |
| 117 | + rel="noopener noreferrer" |
| 118 | + className="inline-block w-full md:w-[150px] px-6 py-3 text-center text-base font-medium bg-blue-700 text-white rounded-full hover:bg-blue-800 transition-colors shadow-sm hover:shadow-md" |
| 119 | + > |
| 120 | + Buy Now |
| 121 | + </a> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + ); |
| 127 | +}; |
0 commit comments