From 2403eae1686c565364691022d294123a16f4f031 Mon Sep 17 00:00:00 2001 From: Miqdad Date: Tue, 26 Aug 2025 12:04:46 +0700 Subject: Fix Breadcrumb & image --- .../product-detail/components/Breadcrumb.tsx | 139 ++++++++++++++++----- .../product-detail/components/ProductDetail.tsx | 4 +- 2 files changed, 108 insertions(+), 35 deletions(-) (limited to 'src-migrate/modules') diff --git a/src-migrate/modules/product-detail/components/Breadcrumb.tsx b/src-migrate/modules/product-detail/components/Breadcrumb.tsx index ba53aca4..d495b77b 100644 --- a/src-migrate/modules/product-detail/components/Breadcrumb.tsx +++ b/src-migrate/modules/product-detail/components/Breadcrumb.tsx @@ -1,68 +1,141 @@ import React, { Fragment } from 'react'; import { useQuery } from 'react-query'; -import { getProductCategoryBreadcrumb } from '~/services/product'; import Link from 'next/link'; +import { getProductCategoryBreadcrumb } from '~/services/product'; import { createSlug } from '~/libs/slug'; +import useDevice from '@/core/hooks/useDevice'; -type Props = { - id: number; - name: string; -}; - -const MAX_VISIBLE_CATEGORIES = 2; // tampilkan 2 level terakhir +type Props = { id: number; name: string }; const Breadcrumb = ({ id, name }: Props) => { + const { isDesktop, isMobile } = useDevice(); + const { data: breadcrumbs = [] } = useQuery({ - queryKey: ['product-category-breadcrumb', id], // penting: ikutkan id + queryKey: ['product-category-breadcrumb', id], queryFn: () => getProductCategoryBreadcrumb(id), refetchOnWindowFocus: false, }); const total = breadcrumbs.length; - const showEllipsis = total > MAX_VISIBLE_CATEGORIES; - const visible = showEllipsis - ? breadcrumbs.slice(total - MAX_VISIBLE_CATEGORIES) - : breadcrumbs; - const hiddenText = showEllipsis + const lastCat = total ? breadcrumbs[total - 1] : null; + const hasHidden = total > 1; + const hiddenText = hasHidden ? breadcrumbs - .slice(0, total - MAX_VISIBLE_CATEGORIES) + .slice(0, total - 1) .map((c) => c.name) .join(' / ') : ''; - return ( -
- + // ===== MOBILE: Home + .. + lastCat + product (truncate) ===== + if (isMobile) { + const crumbsMobile: React.ReactNode[] = []; + + // Home + crumbsMobile.push( + Home - / + ); + + // Indicator kategori tersembunyi (tanpa slash bawaan) + if (hasHidden) { + crumbsMobile.push( + + .. + + ); + } - {showEllipsis && ( - <> - - … - - / - - )} + // Kategori terakhir + if (lastCat) { + crumbsMobile.push( + + {lastCat.name} + + ); + } - {visible.map((category, index) => ( - + // Nama produk (dipotong bila tidak muat) + crumbsMobile.push( + + {name} + + ); + + return ( +
+ {crumbsMobile.map((node, i) => ( + + {node} + {i < crumbsMobile.length - 1 && ( + / + )} + + ))} +
+ ); + } + + // ===== DESKTOP: biarkan seperti semula ===== + if (isDesktop) { + return ( +
+ + Home + + / + {breadcrumbs.map((category, index) => ( + + + {category.name} + + / + + ))} + {name} +
+ ); + } + + // Fallback → layout desktop + return ( +
+ + Home + + / + {breadcrumbs.map((category, index) => ( + {category.name} - / + / ))} - - {name} + {name}
); }; diff --git a/src-migrate/modules/product-detail/components/ProductDetail.tsx b/src-migrate/modules/product-detail/components/ProductDetail.tsx index 8ecdb2fc..35df97df 100644 --- a/src-migrate/modules/product-detail/components/ProductDetail.tsx +++ b/src-migrate/modules/product-detail/components/ProductDetail.tsx @@ -161,7 +161,7 @@ return ( {`Gambar { (e.target as HTMLImageElement).src = '/path/to/fallback-image.jpg'; @@ -174,7 +174,7 @@ return ( Gambar produk
)} -- cgit v1.2.3