# All files / src/components/ui breadcrumbs.tsx

0% Statements 0/73
0% Branches 0/50
0% Functions 0/17
0% Lines 0/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345

     'use client';

    import React from 'react';
    import Link from 'next/link';
    import { usePathname } from 'next/navigation';
    import { ChevronRight, Home } from 'lucide-react';
    import { cn } from '@/lib/utils';
    import { useTranslation } from 'react-i18next';

    export interface BreadcrumbItem {
      label: string;
      href?: string;
      icon?: React.ReactNode;
    }

    export interface BreadcrumbsProps {
      items?: BreadcrumbItem[];
      className?: string;
      showHome?: boolean;
      homeHref?: string;
      separator?: React.ReactNode;
    }

    /**
     * Breadcrumbs Component
     *
     * Features:
     * - Auto-generated from pathname or custom items
     * - Home link optional
     * - Custom separator
     * - Responsive design
     * - i18n support
     */
    export function Breadcrumbs({
      items,
      className,
      showHome = true,
      homeHref = '/admin',
      separator = <ChevronRight className="h-4 w-4 text-slate-500" />}: BreadcrumbsProps) {
      const pathname = usePathname();
      const { t } = useTranslation();

      // Generate breadcrumb items from pathname if not provided
      const breadcrumbItems = React.useMemo(() => {
        if (items) return items;

        const paths = pathname.split('/').filter(Boolean);
        const generatedItems: BreadcrumbItem[] = [];

        let currentPath = '';
        paths.forEach((path, index) => {
          currentPath += `/${path}`;

          // Skip if it's a dynamic route parameter (starts with [)
          if (path.startsWith('[')) return;

          // Capitalize and format the label
          const label = path
            .split('-')
            .map(word => word.charAt(0).toUpperCase() + word.slice(1))
            .join(' ');

          generatedItems.push({
            label: t(`breadcrumbs.${path}`, label),
            href: index === paths.length - 1 ? undefined : currentPath});
        });

        return generatedItems;
      }, [pathname, items, t]);

      if (breadcrumbItems.length === 0 && !showHome) {
        return null;
      }

      return (
        <nav
          aria-label={t('common.details')}
          className={cn('flex items-center space-x-2 text-sm', className)}
        >
          {showHome && (
            <>
              <Link
                href={homeHref}
                className="flex items-center text-slate-400 hover:text-white transition-colors duration-200 focus-ring"
                aria-label={t('common.dashboard')}
              >
                <Home className="h-4 w-4" />
              </Link>
              {breadcrumbItems.length > 0 && (
                <span className="flex items-center" aria-hidden="true">
                  {separator}
                </span>
              )}
            </>
          )}

          {breadcrumbItems.map((item, index) => {
            const isLast = index === breadcrumbItems.length - 1;

            return (
              <React.Fragment key={index}>
                {item.href ? (
                  <Link
                    href={item.href}
                    className="flex items-center gap-1 text-slate-400 hover:text-white transition-colors duration-200 focus-ring truncate max-w-[200px]"
                  >
                    {item.icon}
                    <span>{item.label}</span>
                  </Link>
                ) : (
                  <span
                    className="flex items-center gap-1 text-white font-medium truncate max-w-[200px]"
                    aria-current={isLast ? 'page' : undefined}
                  >
                    {item.icon}
                    <span>{item.label}</span>
                  </span>
                )}

                {!isLast && (
                  <span className="flex items-center" aria-hidden="true">
                    {separator}
                  </span>
                )}
              </React.Fragment>
            );
          })}
        </nav>
      );
    }

    /**
     * Compact Breadcrumbs
     * Shows only last 2 items with ellipsis
     */
    export function CompactBreadcrumbs({
      items,
      className,
      showHome = true,
      homeHref = '/admin'}: Omit<BreadcrumbsProps, 'separator'>) {
      const pathname = usePathname();
      const { t } = useTranslation();

      const breadcrumbItems = React.useMemo(() => {
        if (items) return items;

        const paths = pathname.split('/').filter(Boolean);
        const generatedItems: BreadcrumbItem[] = [];

        let currentPath = '';
        paths.forEach((path, index) => {
          currentPath += `/${path}`;

          if (path.startsWith('[')) return;

          const label = path
            .split('-')
            .map(word => word.charAt(0).toUpperCase() + word.slice(1))
            .join(' ');

          generatedItems.push({
            label: t(`breadcrumbs.${path}`, label),
            href: index === paths.length - 1 ? undefined : currentPath});
        });

        return generatedItems;
      }, [pathname, items, t]);

      // Show only last 2 items
      const displayItems = breadcrumbItems.length > 2
        ? breadcrumbItems.slice(-2)
        : breadcrumbItems;

      const hasHiddenItems = breadcrumbItems.length > 2;

      return (
        <nav
          aria-label={t('common.details')}
          className={cn('flex items-center space-x-2 text-sm', className)}
        >
          {showHome && (
            <>
              <Link
                href={homeHref}
                className="flex items-center text-slate-400 hover:text-white transition-colors duration-200 focus-ring"
                aria-label={t('common.dashboard')}
              >
                <Home className="h-4 w-4" />
              </Link>
              <ChevronRight className="h-4 w-4 text-slate-500" />
            </>
          )}

          {hasHiddenItems && (
            <>
              <span className="text-slate-500">...</span>
              <ChevronRight className="h-4 w-4 text-slate-500" />
            </>
          )}

          {displayItems.map((item, index) => {
            const isLast = index === displayItems.length - 1;

            return (
              <React.Fragment key={index}>
                {item.href ? (
                  <Link
                    href={item.href}
                    className="flex items-center gap-1 text-slate-400 hover:text-white transition-colors duration-200 focus-ring truncate max-w-[150px]"
                  >
                    {item.icon}
                    <span>{item.label}</span>
                  </Link>
                ) : (
                  <span
                    className="flex items-center gap-1 text-white font-medium truncate max-w-[150px]"
                    aria-current={isLast ? 'page' : undefined}
                  >
                    {item.icon}
                    <span>{item.label}</span>
                  </span>
                )}

                {!isLast && <ChevronRight className="h-4 w-4 text-slate-500" />}
              </React.Fragment>
            );
          })}
        </nav>
      );
    }

    /**
     * Breadcrumbs with Dropdown
     * Shows dropdown for middle items when there are many
     */
    export function DropdownBreadcrumbs({
      items,
      className,
      showHome = true,
      homeHref = '/admin'}: Omit<BreadcrumbsProps, 'separator'>) {
      const pathname = usePathname();
      const { t } = useTranslation();
      const [isOpen, setIsOpen] = React.useState(false);

      const breadcrumbItems = React.useMemo(() => {
        if (items) return items;

        const paths = pathname.split('/').filter(Boolean);
        const generatedItems: BreadcrumbItem[] = [];

        let currentPath = '';
        paths.forEach((path, index) => {
          currentPath += `/${path}`;

          if (path.startsWith('[')) return;

          const label = path
            .split('-')
            .map(word => word.charAt(0).toUpperCase() + word.slice(1))
            .join(' ');

          generatedItems.push({
            label: t(`breadcrumbs.${path}`, label),
            href: index === paths.length - 1 ? undefined : currentPath});
        });

        return generatedItems;
      }, [pathname, items, t]);

      if (breadcrumbItems.length <= 3) {
        return <Breadcrumbs items={breadcrumbItems} className={className} showHome={showHome} homeHref={homeHref} />;
      }

      const firstItem = breadcrumbItems[0];
      const middleItems = breadcrumbItems.slice(1, -1);
      const lastItem = breadcrumbItems[breadcrumbItems.length - 1];

      return (
        <nav
          aria-label={t('common.details')}
          className={cn('flex items-center space-x-2 text-sm', className)}
        >
          {showHome && (
            <>
              <Link
                href={homeHref}
                className="flex items-center text-slate-400 hover:text-white transition-colors duration-200 focus-ring"
                aria-label={t('common.dashboard')}
              >
                <Home className="h-4 w-4" />
              </Link>
              <ChevronRight className="h-4 w-4 text-slate-500" />
            </>
          )}

          {firstItem.href && (
            <>
              <Link
                href={firstItem.href}
                className="flex items-center gap-1 text-slate-400 hover:text-white transition-colors duration-200 focus-ring"
              >
                {firstItem.icon}
                <span>{firstItem.label}</span>
              </Link>
              <ChevronRight className="h-4 w-4 text-slate-500" />
            </>
          )}

          <div className="relative">
            <button
              onClick={() => setIsOpen(!isOpen)}
              className="text-slate-400 hover:text-white transition-colors duration-200 focus-ring px-2 py-1 rounded"
              aria-label={t('common.next')}
            >
              ...
            </button>
            {isOpen && (
              <div className="absolute top-full left-0 mt-1 bg-slate-900/95 backdrop-blur-md border border-slate-700/50 rounded-lg shadow-xl z-50 min-w-[200px]">
                {middleItems.map((item, index) => (
                  <Link
                    key={index}
                    href={item.href || '#'}
                    className="block px-4 py-2 text-slate-300 hover:bg-slate-800/50 hover:text-white transition-colors duration-200 first:rounded-t-lg last:rounded-b-lg"
                    onClick={() => setIsOpen(false)}
                  >
                    {item.label}
                  </Link>
                ))}
              </div>
            )}
          </div>

          <ChevronRight className="h-4 w-4 text-slate-500" />

          <span
            className="flex items-center gap-1 text-white font-medium truncate max-w-[200px]"
            aria-current="page"
          >
            {lastItem.icon}
            <span>{lastItem.label}</span>
          </span>
        </nav>
      );
    }