Skip to content

Commit 37ca69c

Browse files
committed
refactor: coin header
1 parent cbe15e5 commit 37ca69c

1 file changed

Lines changed: 69 additions & 66 deletions

File tree

components/CoinHeader.tsx

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -14,82 +14,85 @@ export default function CoinHeader({
1414
priceChange24h,
1515
}: LiveCoinHeaderProps) {
1616
const isTrendingUp = livePriceChangePercentage24h > 0;
17+
const isThirtyDayUp = priceChangePercentage30d > 0;
18+
const isPriceChangeUp = priceChange24h > 0;
19+
20+
const stats = [
21+
{
22+
label: 'Today',
23+
value: livePriceChangePercentage24h,
24+
isUp: isTrendingUp,
25+
formatter: formatPercentage,
26+
valueClassName: 'coin-header-stat-value',
27+
showIcon: true,
28+
},
29+
{
30+
label: '30 Days',
31+
value: priceChangePercentage30d,
32+
isUp: isThirtyDayUp,
33+
formatter: formatPercentage,
34+
valueClassName: 'coin-header-stat-value-30d',
35+
showIcon: true,
36+
},
37+
{
38+
label: 'Price Change (24h)',
39+
value: priceChange24h,
40+
isUp: isPriceChangeUp,
41+
formatter: formatPrice,
42+
valueClassName: 'coin-header-stat-price',
43+
showIcon: false,
44+
},
45+
];
1746

1847
return (
1948
<div className='coin-header-container'>
20-
<div className='coin-header-container'>
21-
<h3 className='text-3xl font-medium'>{name}</h3>
22-
<div className='coin-header-info'>
23-
<Image
24-
src={image}
25-
alt={name}
26-
width={77}
27-
height={77}
28-
className='coin-header-image'
29-
/>
30-
<div className='flex gap-4'>
31-
<h1 className='coin-header-price'>{formatPrice(livePrice)}</h1>
32-
<Badge
33-
className={cn(
34-
'coin-header-badge',
35-
isTrendingUp
36-
? 'bg-green-600/20 text-green-600'
37-
: 'bg-red-500/20 text-red-500'
38-
)}
39-
>
40-
{formatPercentage(livePriceChangePercentage24h)}
41-
{isTrendingUp ? <TrendingUp /> : <TrendingDown />}
42-
(24h)
43-
</Badge>
44-
</div>
49+
<h3 className='text-3xl font-medium'>{name}</h3>
50+
51+
<div className='coin-header-info'>
52+
<Image
53+
src={image}
54+
alt={name}
55+
width={77}
56+
height={77}
57+
className='coin-header-image'
58+
/>
59+
<div className='flex gap-4'>
60+
<h1 className='coin-header-price'>{formatPrice(livePrice)}</h1>
61+
<Badge
62+
className={cn(
63+
'coin-header-badge',
64+
isTrendingUp
65+
? 'bg-green-600/20 text-green-600'
66+
: 'bg-red-500/20 text-red-500'
67+
)}
68+
>
69+
{formatPercentage(livePriceChangePercentage24h)}
70+
{isTrendingUp ? <TrendingUp /> : <TrendingDown />}
71+
(24h)
72+
</Badge>
4573
</div>
46-
<div className='coin-header-stats'>
47-
<div className='coin-header-stat'>
48-
<p className='coin-header-stat-label'>Today</p>
49-
<div
50-
className={cn('coin-header-stat-value', {
51-
'text-green-500': livePriceChangePercentage24h > 0,
52-
'text-red-500': livePriceChangePercentage24h < 0,
53-
})}
54-
>
55-
<p>{formatPercentage(livePriceChangePercentage24h)}</p>
56-
{isTrendingUp ? (
57-
<TrendingUp width={16} height={16} />
58-
) : (
59-
<TrendingDown width={16} height={16} />
60-
)}
61-
</div>
62-
</div>
74+
</div>
6375

64-
<div className='coin-header-stat'>
65-
<p className='coin-header-stat-label'>30 Days</p>
76+
<div className='coin-header-stats'>
77+
{stats.map((stat) => (
78+
<div key={stat.label} className='coin-header-stat'>
79+
<p className='coin-header-stat-label'>{stat.label}</p>
6680
<div
67-
className={cn('coin-header-stat-value-30d', {
68-
'text-green-500': priceChangePercentage30d > 0,
69-
'text-red-500': priceChangePercentage30d < 0,
81+
className={cn(stat.valueClassName, {
82+
'text-green-500': stat.isUp,
83+
'text-red-500': !stat.isUp,
7084
})}
7185
>
72-
<p>{formatPercentage(priceChangePercentage30d)}</p>
73-
{isTrendingUp ? (
74-
<TrendingUp width={16} height={16} />
75-
) : (
76-
<TrendingDown width={16} height={16} />
77-
)}
86+
<p>{stat.formatter(stat.value)}</p>
87+
{stat.showIcon &&
88+
(stat.isUp ? (
89+
<TrendingUp width={16} height={16} />
90+
) : (
91+
<TrendingDown width={16} height={16} />
92+
))}
7893
</div>
7994
</div>
80-
81-
<div className='text-base flex flex-col gap-2'>
82-
<p className='coin-header-stat-label'>Price Change (24h)</p>
83-
<p
84-
className={cn('coin-header-stat-price', {
85-
'text-green-500': priceChange24h > 0,
86-
'text-red-500': priceChange24h < 0,
87-
})}
88-
>
89-
{formatPrice(priceChange24h)}
90-
</p>
91-
</div>
92-
</div>
95+
))}
9396
</div>
9497
</div>
9598
);

0 commit comments

Comments
 (0)