'use client' import {ProductResource} from '@prisma/client' import {EmblaOptionsType} from 'embla-carousel' import useEmblaCarousel from 'embla-carousel-react' import Image from 'next/image' import React, {useCallback, useEffect, useState} from 'react' import {Thumb} from './thumb' import {Dialog, DialogContent, DialogTitle} from '@/ui/dialog' type PropType = { slides: number[] options?: EmblaOptionsType } export default function ProductCarousel({ images, title }: { images: ProductResource[] | null title: string }) { //const {slides, options} = props const [selectedIndex, setSelectedIndex] = useState(0) const [emblaMainRef, emblaMainApi] = useEmblaCarousel({}) //options const [emblaThumbsRef, emblaThumbsApi] = useEmblaCarousel({ containScroll: 'keepSnaps', dragFree: true }) const onThumbClick = useCallback( (index: number) => { if (!emblaMainApi || !emblaThumbsApi) return emblaMainApi.scrollTo(index) }, [emblaMainApi, emblaThumbsApi] ) const onSelect = useCallback(() => { if (!emblaMainApi || !emblaThumbsApi) return setSelectedIndex(emblaMainApi.selectedScrollSnap()) emblaThumbsApi.scrollTo(emblaMainApi.selectedScrollSnap()) }, [emblaMainApi, emblaThumbsApi, setSelectedIndex]) useEffect(() => { if (!emblaMainApi) return onSelect() emblaMainApi.on('select', onSelect).on('reInit', onSelect) }, [emblaMainApi, onSelect]) //let featuredImage: ProductResource | null | undefined /*if ((resources || []).length > 0) { featuredImage = resources?.find(resource => resource.isFeature) }*/ return (
{images?.map((image: ProductResource, index: number) => (
))}
{images?.map((image: ProductResource, index: number) => ( {title} onThumbClick(index)} selected={index === selectedIndex} index={index} /> ))}
) }