import { useCallback, useState, useRef, useEffect } from "react"; import { useNavigate } from "@remix-run/react"; import { safeGoBack } from "~/utils/helpers"; import { Carousel, CarouselContent, CarouselItem, type CarouselApi, } from "~/components/ui/carousel"; import { Drawer, DrawerContent, DrawerHeader, DrawerTitle, } from "~/components/ui/drawer"; import { Dialog, DialogContent, DialogOverlay } from "~/components/ui/dialog"; import { Button } from "~/components/ui/button"; import { useCarousel } from "~/hooks/useCarousel"; import { isVideo } from "../MondrianProductList"; import { Edit3, Trash2, X, EllipsisVertical, EyeOff, Eye, ArrowRight, ChevronLeft, ChevronRight, } from "lucide-react"; import { useDeleteProduct, useToggleProductStatus, } from "~/requestHandler/use-product-hooks"; import placeholderImage from "~/assets/icons/product.png"; import placeholderImageDark from "~/assets/icons/product-dark.png"; import { useRootData } from "~/hooks/use-root-data"; interface ProductImageCarouselProps { images: string[]; productId?: string; storeId?: string; hideIndex?: boolean; height?: number; borderRadius?: number; isOwner?: boolean; isActive?: boolean; controlsIsOn?: boolean; showArrows?: boolean; onStatusChange?: (newStatus: boolean) => void; isChangeStatusDialogOpen?: boolean; setIsChangeStatusDialogOpen?: (open: boolean) => void; } export const ProductImageCarousel = ({ images, productId, storeId, hideIndex = false, height, borderRadius, controlsIsOn = false, showArrows = false, isOwner = false, isActive = true, onStatusChange, isChangeStatusDialogOpen, setIsChangeStatusDialogOpen, }: ProductImageCarouselProps) => { const navigate = useNavigate(); const [currentIndex, setCurrentIndex] = useState(0); const [isDrawerOpen, setIsDrawerOpen] = useState(false); const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false); const [currentActiveStatus, setCurrentActiveStatus] = useState(isActive); const { theme } = useRootData(); const { api, setApi } = useCarousel({ onSelect: useCallback( (carouselApi: CarouselApi) => { if (!carouselApi) return; setCurrentIndex(carouselApi.selectedScrollSnap()); }, [setCurrentIndex] ), }); const videoRefs = useRef<(HTMLVideoElement | null)[]>([]); // Hooks for product management const deleteProductMutation = useDeleteProduct(); const toggleProductStatusMutation = useToggleProductStatus(); // Sync local state with prop changes useEffect(() => { setCurrentActiveStatus(isActive); }, [isActive]); const handleSettingsClick = () => { setIsDrawerOpen(true); }; const handleEditProduct = () => { if (productId && storeId) { navigate(`/store/add/manual/${storeId}?edit=${productId}`); } setIsDrawerOpen(false); }; const handleDeleteClick = () => { setIsDrawerOpen(false); setIsDeleteDialogOpen(true); }; const handleDeactiveClick = () => { setIsDrawerOpen(false); if (setIsChangeStatusDialogOpen) setIsChangeStatusDialogOpen(true); }; const handleConfirmDelete = () => { if (productId) { deleteProductMutation.mutate(productId, { onSuccess: () => { setIsDeleteDialogOpen(false); // Navigate back or refresh the page navigate(-1); }, }); } }; const handleConfirmStatusChange = () => { if (productId) { const newStatus = !currentActiveStatus; toggleProductStatusMutation.mutate( { productId, isActive: newStatus }, { onSuccess: () => { if (setIsChangeStatusDialogOpen) setIsChangeStatusDialogOpen(false); // Update local state setCurrentActiveStatus(newStatus); // Call parent callback if provided if (onStatusChange) { onStatusChange(newStatus); } }, } ); } }; return (
{!hideIndex && images.length > 1 && (
{images.length} / {currentIndex + 1}
)} {isOwner && ( <> )} {images.map((media, index) => (
{isVideo(media) ? (
) : ( { e.currentTarget.src = theme === "dark" ? placeholderImageDark : placeholderImage; e.currentTarget.style.objectFit = "contain"; e.currentTarget.style.backgroundColor = theme === "dark" ? "#121212" : "#f2f0f0"; }} alt={`تصویر ${index + 1}`} className={`w-full ${media ? "object-cover" : "object-contain"}`} style={{ height: height ? `${height}px` : "100%", borderRadius: borderRadius !== undefined ? `${borderRadius}px` : "10px", }} /> )}
))}
{images.length > 1 && (
{images.map((_, index) => (
)} {/* Prev / next arrows (desktop, opt-in) */} {showArrows && images.length > 1 && ( <> )} {/* Settings Drawer */} setIsDrawerOpen(false)} /> تنظیمات محصول
{ if (e.key === "Enter" || e.key === " ") { handleEditProduct(); } }} role="button" tabIndex={0} aria-label="edit product" >

ویرایش محصول

{ if (e.key === "Enter" || e.key === " ") { handleDeactiveClick(); } }} role="button" tabIndex={0} aria-label={ currentActiveStatus ? "deactive product" : "active product" } >
{currentActiveStatus ? ( ) : ( )}

{currentActiveStatus ? "عدم نمایش محصول" : "نمایش محصول"}

{ if (e.key === "Enter" || e.key === " ") { handleDeleteClick(); } }} role="button" tabIndex={0} aria-label="Delete product" >

حذف محصول

{/* Delete Confirmation Dialog */}

حذف محصول

آیا می‌خواهید این محصول را حذف کنید؟

{/* Status Change Confirmation Dialog */}
{currentActiveStatus ? ( ) : ( )}

{currentActiveStatus ? "عدم نمایش محصول" : "نمایش محصول"}

{currentActiveStatus ? "آیا می‌خواهید این محصول را غیرفعال کنید؟" : "آیا می‌خواهید این محصول را فعال کنید؟"}

); };