-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoinInfo.tsx
78 lines (73 loc) · 3.02 KB
/
CoinInfo.tsx
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
'use client'
import { useTickerData } from '@/hooks/queries/useTickerData'
import { cn } from '@/lib/utils'
import { formatPrice, formatPriceChange, formatPercentChange, formatVolume } from '@/utils/formatting'
import { useEffect, useRef, useState } from 'react'
interface CoinInfoProps {
symbol: string
baseAsset: string
quoteAsset: string
}
export const CoinInfo = ({ symbol, baseAsset, quoteAsset }: CoinInfoProps) => {
const { lastPrice, priceChange, priceChangePercent, highPrice, volume, isConnected } = useTickerData(symbol)
const prevPriceRef = useRef(lastPrice)
const [priceFlash, setPriceFlash] = useState<'flash-green' | 'flash-red' | null>(null)
useEffect(() => {
if (prevPriceRef.current !== lastPrice && lastPrice !== undefined) {
setPriceFlash(lastPrice > prevPriceRef.current ? 'flash-green' : 'flash-red')
const timer = setTimeout(() => setPriceFlash(null), 1000)
prevPriceRef.current = lastPrice
return () => clearTimeout(timer)
}
}, [lastPrice])
return (
<div className="flex flex-col gap-4 2xl:flex-row 2xl:items-center 2xl:justify-between p-4 bg-card rounded-lg border mb-4">
<div className="flex items-center gap-4 min-w-[220px]">
<div className="flex items-center gap-2">
<h1 className="text-md font-bold">
{baseAsset}/{quoteAsset}
</h1>
<span className={cn('w-2 h-2 rounded-full', isConnected ? 'bg-green-500' : 'bg-red-500')} />
</div>
<div className="flex items-center gap-2">
<span
className={cn(
'text-md font-semibold tabular-nums',
priceFlash && 'animate-flash',
priceFlash === 'flash-green' && 'text-green-500',
priceFlash === 'flash-red' && 'text-red-500'
)}
>{`$${formatPrice(lastPrice)}`}</span>
<span
className={cn(
'text-xs font-medium min-w-[80px] tabular-nums',
priceChangePercent >= 0 ? 'text-green-500' : 'text-red-500'
)}
>
{formatPercentChange(priceChangePercent)}
</span>
</div>
</div>
<div className="flex gap-2 text-xs">
<div className="min-w-[140px]">
<div className="text-xs text-muted-foreground">24h Change</div>
<div className={cn('text-xs font-medium tabular-nums', priceChange >= 0 ? 'text-green-500' : 'text-red-500')}>
{formatPriceChange(priceChange)}
{formatPercentChange(priceChangePercent)}
</div>
</div>
<div className="min-w-[100px]">
<div className="text-xs text-muted-foreground">24h High</div>
<div className="text-xs font-medium tabular-nums">{formatPrice(highPrice)}</div>
</div>
<div className="min-w-[80px]">
<div className="text-xs text-muted-foreground">24h Volume</div>
<div className="text-xs font-medium tabular-nums">
{formatVolume(volume)} {baseAsset}
</div>
</div>
</div>
</div>
)
}