|
| 1 | +import React from 'react'; |
| 2 | +import type { Position, LendingTheme } from '../../types'; |
| 3 | +import { LIGHT_COLORS } from '../../utils/theme'; |
| 4 | + |
| 5 | +interface PositionCardProps { |
| 6 | + position: Position; |
| 7 | + theme?: LendingTheme; |
| 8 | + onSupply?: (position: Position) => void; |
| 9 | + onBorrow?: (position: Position) => void; |
| 10 | + onRepay?: (position: Position) => void; |
| 11 | + onWithdraw?: (position: Position) => void; |
| 12 | + isLoading?: boolean; |
| 13 | + className?: string; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * PositionCard — displays a single lending position with supply/borrow details. |
| 18 | + * Supports dark/light theming and loading/empty states. |
| 19 | + */ |
| 20 | +export function PositionCard({ |
| 21 | + position, |
| 22 | + theme, |
| 23 | + onSupply, |
| 24 | + onBorrow, |
| 25 | + onRepay, |
| 26 | + onWithdraw, |
| 27 | + isLoading = false, |
| 28 | + className = '', |
| 29 | +}: PositionCardProps) { |
| 30 | + const colors = theme?.colors ?? LIGHT_COLORS; |
| 31 | + |
| 32 | + if (isLoading) { |
| 33 | + return ( |
| 34 | + <div |
| 35 | + className={`position-card position-card--loading ${className}`} |
| 36 | + style={{ background: colors.surface, border: `1px solid ${colors.border}`, borderRadius: 12, padding: 20 }} |
| 37 | + role="status" |
| 38 | + aria-label="Loading position" |
| 39 | + > |
| 40 | + <div style={{ height: 16, background: colors.border, borderRadius: 4, marginBottom: 8, width: '60%' }} /> |
| 41 | + <div style={{ height: 12, background: colors.border, borderRadius: 4, width: '40%' }} /> |
| 42 | + </div> |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + const netValue = (position.supplied - position.borrowed) * position.price; |
| 47 | + |
| 48 | + return ( |
| 49 | + <div |
| 50 | + className={`position-card ${className}`} |
| 51 | + style={{ background: colors.surface, border: `1px solid ${colors.border}`, borderRadius: 12, padding: 20 }} |
| 52 | + data-testid="position-card" |
| 53 | + > |
| 54 | + <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 16 }}> |
| 55 | + <div> |
| 56 | + <h3 style={{ color: colors.text, margin: 0, fontSize: 18, fontWeight: 600 }}>{position.asset}</h3> |
| 57 | + <span style={{ color: colors.textMuted, fontSize: 13 }}>{position.symbol}</span> |
| 58 | + </div> |
| 59 | + <span style={{ color: netValue >= 0 ? colors.success : colors.danger, fontWeight: 600 }}> |
| 60 | + ${netValue.toFixed(2)} |
| 61 | + </span> |
| 62 | + </div> |
| 63 | + |
| 64 | + <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12, marginBottom: 16 }}> |
| 65 | + <div> |
| 66 | + <div style={{ color: colors.textMuted, fontSize: 12, marginBottom: 4 }}>Supplied</div> |
| 67 | + <div style={{ color: colors.text, fontWeight: 500 }}>{position.supplied.toFixed(4)}</div> |
| 68 | + <div style={{ color: colors.success, fontSize: 12 }}>{position.supplyApy.toFixed(2)}% APY</div> |
| 69 | + </div> |
| 70 | + <div> |
| 71 | + <div style={{ color: colors.textMuted, fontSize: 12, marginBottom: 4 }}>Borrowed</div> |
| 72 | + <div style={{ color: colors.text, fontWeight: 500 }}>{position.borrowed.toFixed(4)}</div> |
| 73 | + <div style={{ color: colors.danger, fontSize: 12 }}>{position.borrowApy.toFixed(2)}% APY</div> |
| 74 | + </div> |
| 75 | + </div> |
| 76 | + |
| 77 | + <div style={{ display: 'flex', gap: 8, flexWrap: 'wrap' }}> |
| 78 | + {onSupply && ( |
| 79 | + <button onClick={() => onSupply(position)} style={{ flex: 1, padding: '8px 12px', background: colors.primary, color: '#fff', border: 'none', borderRadius: 8, cursor: 'pointer', fontSize: 13 }}> |
| 80 | + Supply |
| 81 | + </button> |
| 82 | + )} |
| 83 | + {onBorrow && ( |
| 84 | + <button onClick={() => onBorrow(position)} style={{ flex: 1, padding: '8px 12px', background: colors.secondary, color: '#fff', border: 'none', borderRadius: 8, cursor: 'pointer', fontSize: 13 }}> |
| 85 | + Borrow |
| 86 | + </button> |
| 87 | + )} |
| 88 | + {onRepay && position.borrowed > 0 && ( |
| 89 | + <button onClick={() => onRepay(position)} style={{ flex: 1, padding: '8px 12px', background: colors.warning, color: '#fff', border: 'none', borderRadius: 8, cursor: 'pointer', fontSize: 13 }}> |
| 90 | + Repay |
| 91 | + </button> |
| 92 | + )} |
| 93 | + {onWithdraw && position.supplied > 0 && ( |
| 94 | + <button onClick={() => onWithdraw(position)} style={{ flex: 1, padding: '8px 12px', background: colors.surface, color: colors.text, border: `1px solid ${colors.border}`, borderRadius: 8, cursor: 'pointer', fontSize: 13 }}> |
| 95 | + Withdraw |
| 96 | + </button> |
| 97 | + )} |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + ); |
| 101 | +} |
0 commit comments