From c660178c19cf33380cfe0bf585f48f98031d0006 Mon Sep 17 00:00:00 2001 From: Miqdad Date: Fri, 26 Sep 2025 22:10:22 +0700 Subject: try fix crawl --- .../product-detail/components/ProductDetail.tsx | 392 ++++++++------------- 1 file changed, 141 insertions(+), 251 deletions(-) (limited to 'src-migrate') diff --git a/src-migrate/modules/product-detail/components/ProductDetail.tsx b/src-migrate/modules/product-detail/components/ProductDetail.tsx index f0679da4..39011620 100644 --- a/src-migrate/modules/product-detail/components/ProductDetail.tsx +++ b/src-migrate/modules/product-detail/components/ProductDetail.tsx @@ -1,13 +1,17 @@ import style from '../styles/product-detail.module.css'; - import Link from 'next/link'; import { useRouter } from 'next/router'; -import { useEffect, useRef, useState, UIEvent } from 'react'; +import { useEffect, useMemo, useState } from 'react'; +import dynamic from 'next/dynamic'; import { Button } from '@chakra-ui/react'; import { MessageCircleIcon, Share2Icon } from 'lucide-react'; import { LazyLoadComponent } from 'react-lazy-load-image-component'; -import { RWebShare } from 'react-web-share'; + +const RWebShare = dynamic( + () => import('react-web-share').then(m => m.RWebShare), + { ssr: false } +); import useDevice from '@/core/hooks/useDevice'; import { getAuth } from '~/libs/auth'; @@ -22,19 +26,22 @@ import Information from './Information'; import PriceAction from './PriceAction'; import SimilarBottom from './SimilarBottom'; import SimilarSide from './SimilarSide'; - import { gtagProductDetail } from '@/core/utils/googleTag'; -type Props = { - product: IProductDetail; -}; +type Props = { product: IProductDetail }; -const SELF_HOST = process.env.NEXT_PUBLIC_SELF_HOST; +const SELF_HOST = process.env.NEXT_PUBLIC_SELF_HOST || ''; const ProductDetail = ({ product }: Props) => { const { isDesktop, isMobile } = useDevice(); const router = useRouter(); - const auth = getAuth(); + + const [auth, setAuth] = useState(null); + useEffect(() => { + try { + setAuth(getAuth() ?? null); + } catch {} + }, []); const { setAskAdminUrl, @@ -43,306 +50,189 @@ const ProductDetail = ({ product }: Props) => { setIsApproval, isApproval, setSelectedVariant, - setSla, } = useProductDetail(); useEffect(() => { - gtagProductDetail(product); + try { + gtagProductDetail(product); + } catch {} }, [product]); + const currentPath = router?.asPath || '/'; useEffect(() => { const createdAskUrl = whatsappUrl({ template: 'product', payload: { - manufacture: product.manufacture.name, - productName: product.name, - url: process.env.NEXT_PUBLIC_SELF_HOST + router.asPath, + manufacture: product.manufacture?.name ?? '', + productName: product.name ?? '', + url: SELF_HOST + currentPath, }, - fallbackUrl: router.asPath, + fallbackUrl: currentPath, }); - setAskAdminUrl(createdAskUrl); - }, [router.asPath, product.manufacture.name, product.name, setAskAdminUrl]); + }, [currentPath, product.manufacture?.name, product.name, setAskAdminUrl]); useEffect(() => { - if (typeof auth === 'object') { - setIsApproval(auth?.feature?.soApproval); + if (auth && typeof auth === 'object') { + setIsApproval(!!auth?.feature?.soApproval); } const selectedVariant = - product?.variants?.find((variant) => variant.is_in_bu) || - product?.variants?.[0]; + product?.variants?.find(v => v.is_in_bu) || product?.variants?.[0] || null; setSelectedVariant(selectedVariant); - }, []); + }, [auth, product?.variants, setIsApproval, setSelectedVariant]); - // Gabungkan semua gambar produk (utama + tambahan) - const allImages = (() => { + const allImages = useMemo(() => { const arr: string[] = []; - if (product?.image) arr.push(product.image); // selalu masukkan utama, baik mobile maupun desktop - if ( - Array.isArray(product?.image_carousel) && - product.image_carousel.length - ) { - // hindari duplikat jika image utama juga ada di carousel - const set = new Set(arr); - for (const img of product.image_carousel) { - if (!set.has(img)) { - arr.push(img); - set.add(img); - } - } - } + if (Array.isArray(product?.image_carousel)) arr.push(...product.image_carousel); + if (product?.image) arr.unshift(product.image); return arr; - })(); + }, [product?.image, product?.image_carousel]); const [mainImage, setMainImage] = useState(allImages[0] || ''); - useEffect(() => { - // update mainImage jika sumber gambar berubah dan mainImage tidak ada di daftar - if (!allImages.includes(mainImage)) { - setMainImage(allImages[0] || ''); - } - }, [allImages]); // eslint-disable-line react-hooks/exhaustive-deps - - // ===== Slider mobile (tanpa dependency) ===== - const sliderRef = useRef(null); - const [currentIdx, setCurrentIdx] = useState(0); + if (!allImages.includes(mainImage)) setMainImage(allImages[0] || ''); + }, [allImages, mainImage]); - const handleMobileScroll = (e: UIEvent) => { - const el = e.currentTarget; - if (!el) return; - const idx = Math.round(el.scrollLeft / el.clientWidth); - if (idx !== currentIdx) { - setCurrentIdx(idx); - setMainImage(allImages[idx] || ''); - } - }; + const canShare = + typeof navigator !== 'undefined' && typeof (navigator as any).share === 'function'; - const scrollToIndex = (i: number) => { - const el = sliderRef.current; - if (!el) return; - el.scrollTo({ left: i * el.clientWidth, behavior: 'smooth' }); - setCurrentIdx(i); - setMainImage(allImages[i] || ''); - }; - // ============================================ + return ( + <> +
+
+ +
-return ( - <> -
-
- -
+
+
+
+ -
-
- {/* ===== Kolom kiri: gambar ===== */} -
- {/* === MOBILE: Slider swipeable, tanpa thumbnail carousel === */} - {isMobile ? ( -
-
- {allImages.length > 0 ? ( - allImages.map((img, i) => ( - // slide tetap selebar viewport untuk snap mulus + {allImages.length > 0 && ( +
+
+ {allImages.map((img, index) => (
setMainImage(img)} > - {/* gambar diperkecil */} {`Gambar { - (e.target as HTMLImageElement).src = - '/images/noimage.jpeg'; + alt={`Thumbnail ${index + 1}`} + className='w-full h-full object-cover rounded-sm' + loading='lazy' + onError={e => { + (e.target as HTMLImageElement).src = '/images/noimage.jpeg'; }} />
- )) - ) : ( -
- Gambar produk -
- )} -
- - {/* Dots indicator */} - {allImages.length > 1 && ( -
- {allImages.map((_, i) => ( -
- )} -
- ) : ( - <> - {/* === DESKTOP: Tetap seperti sebelumnya === */} - - - {/* Carousel horizontal (thumbnail) – hanya desktop */} - {allImages.length > 0 && ( -
-
- {allImages.map((img, index) => ( -
setMainImage(img)} - > - {`Thumbnail { - (e.target as HTMLImageElement).src = - '/images/noimage.jpeg'; - }} - /> -
- ))} -
-
- )} - - )} -
- {/* <<=== TUTUP kolom kiri */} +
+ )} +
- {/* ===== Kolom kanan: info ===== */} -
-
-

{product.name}

-
- -
+
+
+

{product.name}

+
+ +
+
-
-
- {isMobile && ( -
- -
- )} +
+ {isMobile && ( +
+ +
+ )} -
- {!!activeVariantId && !isApproval && ( - - )} +
+ {!!activeVariantId && !isApproval && ( + + )} -
+
-
-

Informasi Produk

-
-
-

' - ? 'Belum ada deskripsi' - : product.description, - }} - /> +
+

Informasi Produk

+
+
+

' + ? 'Belum ada deskripsi' + : product.description, + }} + /> +
-
- - {isDesktop && ( -
- -
- - | - - - - | - - + {isDesktop && ( +
+ +
- -
-
-
Produk Serupa
- -
- - -
- )} + | -
-
Kamu Mungkin Juga Suka
+ -
+ | - - - -
+ {canShare && ( + + + + )} +
-
-
- -); +
+
Produk Serupa
+
+ +
+ )} +
+
Kamu Mungkin Juga Suka
+
+ + + +
+
+
+ + ); }; export default ProductDetail; -- cgit v1.2.3