-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathDutchAuctionCard.tsx
More file actions
164 lines (144 loc) · 5.64 KB
/
Copy pathDutchAuctionCard.tsx
File metadata and controls
164 lines (144 loc) · 5.64 KB
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
import { useState, useEffect } from 'react';
import type { DutchAuction } from '../types/dutchAuction';
import { COLOR, fmt } from '../utils/tokens';
import { PendingButton } from './PendingButton';
interface DutchAuctionCardProps {
auction: DutchAuction;
onPurchase?: (auctionId: string, price: number) => void;
}
const calculateCurrentPrice = (auction: DutchAuction): number => {
const now = Date.now();
const start = new Date(auction.startTime).getTime();
const end = new Date(auction.endTime).getTime();
if (now < start) return auction.startPrice;
if (now > end) return auction.floorPrice;
const elapsed = now - start;
const total = end - start;
const progress = elapsed / total;
return auction.startPrice - progress * (auction.startPrice - auction.floorPrice);
};
const formatTimeLeft = (endTime: string): string => {
const now = Date.now();
const end = new Date(endTime).getTime();
const diff = end - now;
if (diff <= 0) return 'Ended';
const seconds = Math.floor(diff / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const h = hours;
const m = minutes % 60;
const s = seconds % 60;
return `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`;
};
export const DutchAuctionCard: React.FC<DutchAuctionCardProps> = ({ auction, onPurchase }) => {
const [currentPrice, setCurrentPrice] = useState(calculateCurrentPrice(auction));
const [timeLeft, setTimeLeft] = useState(formatTimeLeft(auction.endTime));
useEffect(() => {
if (auction.status !== 'Active') return;
const interval = setInterval(() => {
setCurrentPrice(calculateCurrentPrice(auction));
setTimeLeft(formatTimeLeft(auction.endTime));
}, 1000);
return () => clearInterval(interval);
}, [auction]);
return (
<div className="card" style={{ padding: '1rem', marginBottom: '1rem' }}>
<div style={{ display: 'flex', gap: '1rem' }}>
<img
src={auction.nft.image}
alt={auction.nft.name}
style={{
width: '120px',
height: '120px',
borderRadius: '8px',
objectFit: 'cover',
border: `1px solid ${COLOR.border}`,
}}
/>
<div style={{ flex: 1, minWidth: 0 }}>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start' }}>
<div style={{ marginBottom: '0.5rem' }}>
<h3 style={{ margin: 0, color: COLOR.text, fontSize: '1.1rem' }}>
{auction.nft.name}
</h3>
<p style={{ margin: 0, color: COLOR.muted, fontSize: '0.85rem' }}>
{auction.nft.collection} · Token #{auction.nft.tokenId}
</p>
</div>
<div
style={{
padding: '0.25rem 0.5rem',
borderRadius: '4px',
fontSize: '0.75rem',
fontWeight: 500,
background: auction.status === 'Active'
? 'rgba(63,185,80,0.16)'
: auction.status === 'Completed'
? 'rgba(88,166,255,0.16)'
: 'rgba(248,81,73,0.16)',
color: auction.status === 'Active'
? '#8ee99d'
: auction.status === 'Completed'
? '#58a6ff'
: '#ffb0aa',
}}
>
{auction.status}
</div>
</div>
<p style={{ margin: '0.5rem 0', color: COLOR.text, fontSize: '0.9rem' }}>
{auction.nft.description}
</p>
<div style={{ display: 'flex', gap: '1rem', marginTop: '0.75rem', flexWrap: 'wrap' }}>
<div>
<p style={{ margin: 0, color: COLOR.muted, fontSize: '0.75rem' }}>
Current Price
</p>
<p style={{ margin: 0, color: COLOR.accent, fontSize: '1.25rem', fontWeight: 600 }}>
{auction.status === 'Active' ? fmt(currentPrice) : auction.finalPrice ? fmt(auction.finalPrice) : '-'}
</p>
</div>
<div>
<p style={{ margin: 0, color: COLOR.muted, fontSize: '0.75rem' }}>
Start / Floor
</p>
<p style={{ margin: 0, color: COLOR.text, fontSize: '0.9rem' }}>
{fmt(auction.startPrice)} / {fmt(auction.floorPrice)}
</p>
</div>
{auction.status === 'Active' && (
<div>
<p style={{ margin: 0, color: COLOR.muted, fontSize: '0.75rem' }}>
Time Left
</p>
<p style={{ margin: 0, color: COLOR.warning, fontSize: '1rem', fontWeight: 600 }}>
{timeLeft}
</p>
</div>
)}
{auction.status === 'Completed' && auction.winner && (
<div>
<p style={{ margin: 0, color: COLOR.muted, fontSize: '0.75rem' }}>
Winner
</p>
<p style={{ margin: 0, color: COLOR.text, fontSize: '0.9rem' }}>
{`${auction.winner.slice(0, 6)}...${auction.winner.slice(-4)}`}
</p>
</div>
)}
</div>
{auction.status === 'Active' && (
<div style={{ marginTop: '1rem' }}>
<PendingButton
onClick={() => onPurchase?.(auction.id, currentPrice)}
style={{ width: '100%' }}
>
Purchase Now for {fmt(currentPrice)}
</PendingButton>
</div>
)}
</div>
</div>
</div>
);
};