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

0% Statements 0/31
0% Branches 0/58
0% Functions 0/14
0% Lines 0/31

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

     'use client';

    import React from 'react';
    import { useTranslation } from 'react-i18next';
    import { cn } from '@/lib/utils';
    import { Loader2, RefreshCw } from 'lucide-react';

    interface LoadingSpinnerProps {
      size?: 'sm' | 'md' | 'lg' | 'xl';
      variant?: 'default' | 'dots' | 'pulse' | 'bounce';
      className?: string;
    }

    interface LoadingOverlayProps {
      isLoading: boolean;
      children: React.ReactNode;
      message?: string;
      className?: string;
      spinnerSize?: LoadingSpinnerProps['size'];
      backdrop?: boolean;
    }

    interface LoadingStateProps {
      isLoading: boolean;
      error?: string | null;
      children: React.ReactNode;
      loadingComponent?: React.ReactNode;
      errorComponent?: React.ReactNode;
      onRetry?: () => void;
      className?: string;
    }

    interface SkeletonProps {
      className?: string;
      variant?: 'text' | 'circular' | 'rectangular';
      width?: string | number;
      height?: string | number;
      lines?: number;
    }

    // Loading Spinner Component
    export function LoadingSpinner({
      size = 'md',
      variant = 'default',
      className
    }: LoadingSpinnerProps) {
      const sizeClasses = {
        sm: 'h-4 w-4',
        md: 'h-6 w-6',
        lg: 'h-8 w-8',
        xl: 'h-12 w-12'};

      if (variant === 'dots') {
        return (
          <div className={cn('flex space-x-1', className)}>
            {[0, 1, 2].map((i) => (
              <div
                key={i}
                className={cn(
                  'rounded-full bg-current animate-pulse',
                  size === 'sm' && 'h-1 w-1',
                  size === 'md' && 'h-2 w-2',
                  size === 'lg' && 'h-3 w-3',
                  size === 'xl' && 'h-4 w-4'
                )}
                style={{
                  animationDelay: `${i * 0.2}s`,
                  animationDuration: '1.4s'}}
              />
            ))}
          </div>
        );
      }

      if (variant === 'pulse') {
        return (
          <div
            className={cn(
              'rounded-full bg-current animate-pulse',
              sizeClasses[size],
              className
            )}
          />
        );
      }

      if (variant === 'bounce') {
        return (
          <div className={cn('flex space-x-1', className)}>
            {[0, 1, 2].map((i) => (
              <div
                key={i}
                className={cn(
                  'rounded-full bg-current animate-bounce',
                  size === 'sm' && 'h-2 w-2',
                  size === 'md' && 'h-3 w-3',
                  size === 'lg' && 'h-4 w-4',
                  size === 'xl' && 'h-6 w-6'
                )}
                style={{
                  animationDelay: `${i * 0.1}s`}}
              />
            ))}
          </div>
        );
      }

      // Default spinner
      return (
        <Loader2
          className={cn(
            'animate-spin',
            sizeClasses[size],
            className
          )}
        />
      );
    }

    // Loading Overlay Component
    export function LoadingOverlay({
      isLoading,
      children,
      message,
      className,
      spinnerSize = 'md',
      backdrop = true}: LoadingOverlayProps) {
      const { t } = useTranslation();
      return (
        <div className={cn('relative', className)}>
          {children}
          {isLoading && (
            <div
              className={cn(
                'absolute inset-0 flex flex-col items-center justify-center z-50',
                backdrop && 'bg-white/80 backdrop-blur-sm'
              )}
            >
              <LoadingSpinner size={spinnerSize} />
              <p className="mt-2 text-sm text-gray-600 font-medium">
                {message ?? t('common.loading')}
              </p>
            </div>
          )}
        </div>
      );
    }

    // Loading State Component
    export function LoadingState({
      isLoading,
      error,
      children,
      loadingComponent,
      errorComponent,
      onRetry,
      className}: LoadingStateProps) {
      const { t } = useTranslation();
      if (isLoading) {
        if (loadingComponent) {
          return <>{loadingComponent}</>;
        }

        return (
            <div className={cn('flex flex-col items-center justify-center py-8', className)}>
            <LoadingSpinner size="lg" />
            <p className="mt-4 text-sm text-gray-600">{t('common.loading')}</p>
          </div>
        );
      }

      if (error) {
        if (errorComponent) {
          return <>{errorComponent}</>;
        }

        return (
          <div className={cn('flex flex-col items-center justify-center py-8', className)}>
            <div className="text-center">
              <div className="mx-auto h-12 w-12 text-red-500 mb-4">
                <svg fill="none" viewBox="0 0 24 24" stroke="currentColor">
                  <path
                    strokeLinecap="round"
                    strokeLinejoin="round"
                    strokeWidth={2}
                    d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.732 16.5c-.77.833.192 2.5 1.732 2.5z"
                  />
                </svg>
              </div>
              <h3 className="text-lg font-medium text-gray-900 mb-2">
                {t('common.somethingWentWrong')}
              </h3>
              <p className="text-sm text-gray-600 mb-4">
                {error}
              </p>
              {onRetry && (
                <button
                  onClick={onRetry}
                  className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
                >
                  <RefreshCw className="h-4 w-4 mr-2" />
                  {t('common.tryAgain')}
                </button>
              )}
            </div>
          </div>
        );
      }

      return <>{children}</>;
    }

    // Skeleton Component
    export function Skeleton({
      className,
      variant = 'rectangular',
      width,
      height,
      lines = 1}: SkeletonProps) {
      const baseClasses = 'animate-pulse bg-gray-200 dark:bg-gray-700';

      if (variant === 'text') {
        return (
          <div className={cn('space-y-2', className)}>
            {Array.from({ length: lines }).map((_, i) => (
              <div
                key={i}
                className={cn(
                  baseClasses,
                  'h-4 rounded',
                  i === lines - 1 && lines > 1 && 'w-3/4' // Last line shorter
                )}
                style={{ width, height }}
              />
            ))}
          </div>
        );
      }

      if (variant === 'circular') {
        return (
          <div
            className={cn(
              baseClasses,
              'rounded-full',
              !width && !height && 'h-12 w-12',
              className
            )}
            style={{ width, height }}
          />
        );
      }

      // Rectangular (default)
      return (
        <div
          className={cn(
            baseClasses,
            'rounded',
            !width && !height && 'h-4 w-full',
            className
          )}
          style={{ width, height }}
        />
      );
    }

    // Skeleton variants for common use cases
    export function SkeletonCard({ className }: { className?: string }) {
      return (
        <div className={cn('p-4 border rounded-lg space-y-3', className)}>
          <Skeleton variant="rectangular" height="12rem" />
          <Skeleton variant="text" lines={2} />
          <div className="flex items-center space-x-2">
            <Skeleton variant="circular" width="2rem" height="2rem" />
            <Skeleton variant="text" width="6rem" />
          </div>
        </div>
      );
    }

    export function SkeletonTable({
      rows = 5,
      columns = 4,
      className
    }: {
      rows?: number;
      columns?: number;
      className?: string;
    }) {
      return (
        <div className={cn('space-y-3', className)}>
          {/* Header */}
          <div className="grid gap-4" style={{ gridTemplateColumns: `repeat(${columns}, 1fr)` }}>
            {Array.from({ length: columns }).map((_, i) => (
              <Skeleton key={`header-${i}`} height="1.5rem" />
            ))}
          </div>

          {/* Rows */}
          {Array.from({ length: rows }).map((_, rowIndex) => (
            <div
              key={`row-${rowIndex}`}
              className="grid gap-4"
              style={{ gridTemplateColumns: `repeat(${columns}, 1fr)` }}
            >
              {Array.from({ length: columns }).map((_, colIndex) => (
                <Skeleton key={`cell-${rowIndex}-${colIndex}`} height="1rem" />
              ))}
            </div>
          ))}
        </div>
      );
    }

    export function SkeletonList({
      items = 5,
      className
    }: {
      items?: number;
      className?: string;
    }) {
      return (
        <div className={cn('space-y-4', className)}>
          {Array.from({ length: items }).map((_, i) => (
            <div key={i} className="flex items-center space-x-3">
              <Skeleton variant="circular" width="2.5rem" height="2.5rem" />
              <div className="flex-1 space-y-2">
                <Skeleton height="1rem" width="60%" />
                <Skeleton height="0.75rem" width="40%" />
              </div>
            </div>
          ))}
        </div>
      );
    }