summaryrefslogtreecommitdiff
path: root/src/lib/category/components/Breadcrumb.jsx
blob: 29bc9c0a6345eaed347d12a75fea3d6d7412139c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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, shortDesc, 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 (
      <div className='container mx-auto py-4 md:py-6'>
        <Skeleton isLoaded={!breadcrumbs.isLoading} className='w-2/3'>
          <ChakraBreadcrumb
            spacing='8px'
            sx={{
              '& ol': {
                display: 'flex',
                flexWrap: { base: 'wrap', md: 'nowrap' },
                alignItems: 'center',
              },
              '& li': { display: 'inline-flex', alignItems: 'center' },
              '& li:not(:last-of-type)': {
                flex: '0 0 auto',
                whiteSpace: 'nowrap',
              },
              '& li:last-of-type': {
                flex: '1 1 auto',
                minWidth: 0,
              },
            }}
          >
            {/* Home */}
            <BreadcrumbItem>
              <BreadcrumbLink as={Link} href='/' className='!text-danger-500'>
                Home
              </BreadcrumbLink>
            </BreadcrumbItem>

            {/* Categories */}
            {items.map((category, index) => {
              const isLastCategory = index === items.length - 1;
              const isClickable = currentLabel || !isLastCategory;

              return (
                <BreadcrumbItem key={index} isCurrentPage={!isClickable}>
                  {isClickable ? (
                    <BreadcrumbLink
                      as={Link}
                      href={createSlug(
                        '/shop/category/',
                        category.name,
                        category.id,
                      )}
                      className='!text-danger-500'
                    >
                      {category.name}
                    </BreadcrumbLink>
                  ) : (
                    <BreadcrumbLink className='block whitespace-normal break-words md:whitespace-nowrap'>
                      {category.name}
                    </BreadcrumbLink>
                  )}
                </BreadcrumbItem>
              );
            })}

            {/* Searchkey / Current Page */}
            {currentLabel && (
              <BreadcrumbItem isCurrentPage>
                <BreadcrumbLink className='block whitespace-normal break-words md:whitespace-nowrap'>
                  {currentLabel}
                </BreadcrumbLink>
              </BreadcrumbItem>
            )}
          </ChakraBreadcrumb>
        </Skeleton>
        {shortDesc && (
          <div
            className='
    w-full mt-2
    text-sm text-neutral-600
    leading-7
    text-justify
    break-words
    [hyphens:auto]
    max-w-none
  '
          >
            {shortDesc}
          </div>
        )}
      </div>
    );
  }

  /* =========================
     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 (
      <div className='container mx-auto py-2 mt-2'>
        <Skeleton isLoaded={!breadcrumbs.isLoading} className='w-full'>
          <ChakraBreadcrumb
            separator={<span className='mx-1'>/</span>}
            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 */}
            <BreadcrumbItem>
              <BreadcrumbLink as={Link} href='/' className='!text-danger-500'>
                Home
              </BreadcrumbLink>
            </BreadcrumbItem>

            {/* Jika ada kategori sebelum secondLast, tampilkan '..' (link ke beforeSecond) */}
            {beforeSecond && (
              <BreadcrumbItem>
                <BreadcrumbLink
                  as={Link}
                  href={createSlug(
                    '/shop/category/',
                    beforeSecond.name,
                    beforeSecond.id,
                  )}
                  title={hiddenText}
                  className='!text-danger-500'
                >
                  ..
                </BreadcrumbLink>
              </BreadcrumbItem>
            )}

            {secondLast && (
              <BreadcrumbItem>
                <BreadcrumbLink
                  as={Link}
                  href={createSlug(
                    '/shop/category/',
                    secondLast.name,
                    secondLast.id,
                  )}
                  className='!text-danger-500'
                >
                  {secondLast.name}
                </BreadcrumbLink>
              </BreadcrumbItem>
            )}

            {/* lastCat (current) dengan truncate & lebar dibatasi */}
            {lastCat && (
              <BreadcrumbItem isCurrentPage>
                <span
                  className='inline-block truncate align-bottom'
                  style={{ maxWidth: '60vw' }}
                  title={lastCat.name}
                >
                  {finalLabel}
                </span>
              </BreadcrumbItem>
            )}
          </ChakraBreadcrumb>
        </Skeleton>

        {shortDesc && (
          <div
            className='
    w-full mt-2
    text-sm text-neutral-600
    leading-7
    text-justify
    break-words
    [hyphens:auto]
    max-w-none
  '
          >
            {shortDesc}
          </div>
        )}
      </div>
    );
  }

  return null;
};

export default Breadcrumb;