/* eslint-disable jsx-a11y/click-events-have-key-events */ /* eslint-disable jsx-a11y/no-static-element-interactions */ import React, { useState, useRef } from 'react'; import Slider from 'react-slick'; import 'slick-carousel/slick/slick.css'; import 'slick-carousel/slick/slick-theme.css'; import { Image } from '@components/common/Image.js'; import { useProduct } from '@components/frontStore/catalog/ProductContext.js'; import './Media.scss'; import { ProductNoThumbnail } from '@components/common/ProductNoThumbnail.js'; const SliderComponent = Slider as any; type SliderType = any; const PrevArrow = (props: any) => { const { className, onClick } = props; return ( ); }; const NextArrow = (props: any) => { const { className, onClick } = props; return ( ); }; interface ImageWithDimensionsProps { url: string; alt?: string; width: number; height: number; } interface MediaProps { imageSize?: { width: number; height: number; }; thumbnailSize?: { width: number; height: number; }; modalSize?: { width: number; height: number; }; } export const Media: React.FC = ({ imageSize = { width: 600, height: 600 }, thumbnailSize = { width: 100, height: 100 }, modalSize = { width: 1200, height: 1200 } }) => { const product = useProduct(); const [isModalOpen, setIsModalOpen] = useState(false); const [activeSlide, setActiveSlide] = useState(0); const [isImageLoading, setIsImageLoading] = useState(false); const mainSliderRef = useRef(null); const modalSliderRef = useRef(null); const allImages: ImageWithDimensionsProps[] = []; const fullscreenWidth = modalSize.width * 1.5; const fullscreenHeight = modalSize.height * 1.5; if (product.image) { allImages.push({ url: product.image.url, alt: product.image.alt || product.name, width: imageSize.width, height: imageSize.height }); } if (product.gallery && Array.isArray(product.gallery)) { product.gallery.forEach((img) => { allImages.push({ url: img.url, alt: img.alt || product.name, width: imageSize.width, height: imageSize.height }); }); } const mainSliderSettings = { dots: allImages.length > 1, dotsClass: 'slick-dots slick-thumb', infinite: true, speed: 500, slidesToShow: 1, slidesToScroll: 1, arrows: allImages.length > 1, fade: false, prevArrow: , nextArrow: , beforeChange: (_: number, next: number) => { setActiveSlide(next); }, customPaging: function (i: number) { return ( ); } }; const modalSliderSettings = { dots: allImages.length > 1, dotsClass: 'slick-dots slick-thumb', infinite: true, speed: 500, slidesToShow: 1, slidesToScroll: 1, arrows: true, prevArrow: , nextArrow: , initialSlide: activeSlide, adaptiveHeight: true, lazyLoad: 'ondemand', fade: false, swipe: true, beforeChange: () => setIsImageLoading(true), afterChange: () => setIsImageLoading(false), customPaging: function (i: number) { return ( ); } }; const openModal = (index: number) => { setActiveSlide(index); setIsModalOpen(true); setTimeout(() => { if (modalSliderRef.current) { modalSliderRef.current.slickGoTo(index); } }, 100); document.addEventListener('keydown', handleKeyDown); document.body.style.overflow = 'hidden'; }; const closeModal = () => { setIsModalOpen(false); document.removeEventListener('keydown', handleKeyDown); document.body.style.overflow = ''; }; const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') { closeModal(); } else if (e.key === 'ArrowRight' && modalSliderRef.current) { modalSliderRef.current.slickNext(); } else if (e.key === 'ArrowLeft' && modalSliderRef.current) { modalSliderRef.current.slickPrev(); } }; return ( {allImages.length > 0 && ( {allImages.map((image, index) => ( openModal(index)} style={{ width: imageSize.width, height: imageSize.height }} > ))} )} {allImages.length === 0 && ( )} {isModalOpen && ( {isImageLoading && ( )} {allImages.map((image, index) => ( ))} )} ); };