All files / src/features/wallet/components wallet-card.tsx

82.35% Statements 14/17
81.25% Branches 26/32
75% Functions 3/4
82.35% Lines 14/17

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                                                                      2x             26x       26x                                                           2x     13x     13x     13x 13x 13x     13x 1x 1x           13x                                                                                                                                                                                           2x      
import { forwardRef, useState } from 'react';
import { Badge } from '@/components/badge';
import { Button } from '@/components/button';
import { Heading } from '@/components/heading';
import { cn } from '@/lib/utils';
import bgWalletImage from '@/assets/images/bg-wallet.webp';
import { currencyFormatter } from '@/lib/currency-formatter';
import { useWalletOperations } from '../hooks/use-wallet';
import { WithdrawDialog } from './withdraw-dialog';
import {
  WALLET_PAGE_HEADER,
  WALLET_BUTTON_LABELS,
  WALLET_SECTION_TITLES,
  WALLET_SECTION_DESCRIPTIONS,
  DEFAULT_CURRENCY,
} from '@/constants/wallet';
 
interface WalletCardProps {
  showWithdrawButton?: boolean;
  onWithdraw?: () => void;
  className?: string;
}
 
/**
 * WalletBadgeSection - Displays formatted currency amount with description
 * Uses shared currency formatter for consistency
 */
interface WalletBadgeSectionProps {
  title: string;
  amount: string | number;
  currency: string;
  description: string;
  isLoading?: boolean;
}
 
const WalletBadgeSection = ({
  title,
  amount,
  currency,
  description,
  isLoading = false,
}: WalletBadgeSectionProps) => {
  const formattedAmount = isLoading
    ? currencyFormatter.formatLoading()
    : currencyFormatter.format(amount, currency);
 
  return (
    <div className="flex flex-col space-y-3">
      <Badge
        className={cn(
          'bg-gradient-diagonal text-white text-xs px-4 py-2 rounded-full self-start',
          'transition-all duration-200 hover:shadow-lg',
          isLoading && 'animate-pulse'
        )}
      >
        {title}
      </Badge>
 
      <div className="text-white space-y-2">
        <div
          className={cn(
            'text-2xl md:text-3xl lg:text-4xl font-bold',
            isLoading && 'animate-pulse bg-white/20 rounded h-8 w-32'
          )}
        >
          {!isLoading && formattedAmount}
        </div>
 
        <div className="text-white/80 text-xs md:text-sm leading-relaxed max-w-[200px]">
          {description}
        </div>
      </div>
    </div>
  );
};
 
const WalletCard = forwardRef<HTMLDivElement, WalletCardProps>(
  ({ showWithdrawButton = true, onWithdraw, className, ...props }, ref) => {
    // Internal state for withdraw dialog
    const [isWithdrawDialogOpen, setIsWithdrawDialogOpen] = useState(false);
 
    // Fetch wallet data internally
    const { data: walletData, isLoading } = useWalletOperations();
 
    // Default values
    const totalEarnings = walletData?.totalEarnings || '0.00';
    const balance = walletData?.balance || '0.00';
    const currency = walletData?.currency || DEFAULT_CURRENCY;
 
    // Handle withdraw click
    const handleWithdrawClick = () => {
      if (onWithdraw) {
        onWithdraw();
      } else Eif (walletData && parseFloat(walletData.balance) > 0) {
        setIsWithdrawDialogOpen(true);
      }
    };
 
    return (
      <>
        <div
          ref={ref}
          className={cn(
            'relative w-full min-h-[280px] md:min-h-[320px] lg:min-h-[300px]',
            'rounded-[20px] overflow-hidden',
            'p-6 md:p-8 flex flex-col',
            className
          )}
          {...props}
        >
          {/* Gradient Background Layer */}
          <div className="absolute inset-0 z-0 bg-gradient-diagonal-alt" />
 
          {/* Background Image Layer */}
          <div
            className="absolute inset-0 z-10 opacity-90"
            style={{
              backgroundImage: `url(${bgWalletImage})`,
              backgroundSize: 'cover',
              backgroundPosition: 'center',
              backgroundRepeat: 'no-repeat',
            }}
          />
 
          {/* Content Layer */}
          <div className="relative z-20 flex flex-col h-full">
            {/* Header Section */}
            <header className="flex items-center justify-between mb-8 md:mb-12">
              <Heading
                as="h2"
                size="h3"
                className={cn(
                  'text-white',
                  isLoading && 'animate-pulse bg-white/20 rounded h-6 w-24'
                )}
              >
                {!isLoading && WALLET_PAGE_HEADER}
              </Heading>
 
              {showWithdrawButton && (
                <Button
                  className={cn(
                    'text-default bg-white hover:opacity-90 h-10 px-6 border-0',
                    'transition-all duration-200 hover:shadow-lg hover:scale-105',
                    'text-sm md:text-base',
                    isLoading && 'opacity-50 cursor-not-allowed'
                  )}
                  onClick={handleWithdrawClick}
                  disabled={isLoading}
                >
                  {WALLET_BUTTON_LABELS.WITHDRAW_FUNDS}
                </Button>
              )}
            </header>
 
            {/* Balance Information Section */}
            <div className="flex-1 flex items-end">
              <div className="w-full grid grid-cols-1 lg:grid-cols-2 gap-8 lg:gap-12">
                <WalletBadgeSection
                  title={WALLET_SECTION_TITLES.TOTAL_EARNINGS}
                  amount={totalEarnings}
                  currency={currency}
                  description={WALLET_SECTION_DESCRIPTIONS.TOTAL_EARNINGS}
                  isLoading={isLoading}
                />
 
                <WalletBadgeSection
                  title={WALLET_SECTION_TITLES.AVAILABLE_BALANCE}
                  amount={balance}
                  currency={currency}
                  description={WALLET_SECTION_DESCRIPTIONS.AVAILABLE_BALANCE}
                  isLoading={isLoading}
                />
              </div>
            </div>
          </div>
        </div>
 
        {/* Withdraw Dialog */}
        {walletData && (
          <WithdrawDialog
            isOpen={isWithdrawDialogOpen}
            onClose={() => setIsWithdrawDialogOpen(false)}
            availableBalance={walletData.balance}
            currency={walletData.currency}
          />
        )}
      </>
    );
  }
);
 
WalletCard.displayName = 'WalletCard';
 
export { WalletCard, type WalletCardProps };