import odooApi from '@/core/api/odooApi';
import { createSlug } from '@/core/utils/slug';
import {
Breadcrumb as ChakraBreadcrumb,
BreadcrumbItem,
BreadcrumbLink,
Skeleton,
} from '@chakra-ui/react';
import Link from 'next/link';
import React from 'react';
import { useQuery } from 'react-query';
import useDevice from '@/core/hooks/useDevice';
const Breadcrumb = ({ categoryId, currentLabel }) => {
const breadcrumbs = useQuery(
['category-breadcrumbs', categoryId],
async () =>
await odooApi('GET', `/api/v1/category/${categoryId}/category-breadcrumb`)
);
const { isDesktop, isMobile } = useDevice();
const items = breadcrumbs.data ?? [];
/* =========================
DESKTOP
========================== */
if (isDesktop) {
return (
{/* Home */}
Home
{/* Categories */}
{items.map((category, index) => {
const isLastCategory = index === items.length - 1;
const isClickable = currentLabel || !isLastCategory;
return (
{isClickable ? (
{category.name}
) : (
{category.name}
)}
);
})}
{/* Searchkey / Current Page */}
{currentLabel && (
{currentLabel}
)}
);
}
/* =========================
MOBILE
========================== */
if (isMobile) {
const n = items.length;
const lastCat = n >= 1 ? items[n - 1] : null;
const secondLast = n >= 2 ? items[n - 2] : null;
const beforeSecond = n >= 3 ? items[n - 3] : null;
const hiddenText =
n >= 3
? items
.slice(0, n - 2)
.map((c) => c.name)
.join(' / ')
: '';
const finalLabel = currentLabel || lastCat?.name;
return (
/}
spacing='4px'
sx={{
'& ol': {
display: 'flex',
alignItems: 'center',
overflow: 'hidden',
whiteSpace: 'nowrap',
gap: '0',
},
'& li': { display: 'inline-flex', alignItems: 'center' },
'& li:not(:last-of-type)': {
flex: '0 0 auto',
whiteSpace: 'nowrap',
},
'& li:last-of-type': {
flex: '0 1 auto',
minWidth: 0,
},
}}
className='text-caption-2 p-0'
>
{/* Home */}
Home
{/* Ellipsis */}
{beforeSecond && (
..
)}
{/* Second last category */}
{secondLast && (
{secondLast.name}
)}
{/* Current */}
{finalLabel && (
{finalLabel}
)}
);
}
return null;
};
export default Breadcrumb;