-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptoChart.tsx
More file actions
282 lines (260 loc) · 8.77 KB
/
CryptoChart.tsx
File metadata and controls
282 lines (260 loc) · 8.77 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import React, { useEffect, useRef, useState } from 'react';
import { createChart, ColorType, IChartApi, ISeriesApi, LineData } from 'lightweight-charts';
import { ArrowLeft, Calendar, TrendingUp, TrendingDown } from 'lucide-react';
import { GlassCard } from '../GlassCard';
import { useLanguage } from '../../hooks/useLanguage';
interface CryptoChartProps {
crypto: {
id: string;
name: string;
symbol: string;
image: string;
current_price: number;
market_cap: number;
total_volume: number;
price_change_percentage_24h: number;
};
onBack: () => void;
}
interface PriceData {
time: number;
value: number;
}
export const CryptoChart: React.FC<CryptoChartProps> = ({ crypto, onBack }) => {
const { currentLanguage } = useLanguage();
const chartContainerRef = useRef<HTMLDivElement>(null);
const [chart, setChart] = useState<IChartApi | null>(null);
const [series, setSeries] = useState<ISeriesApi<'Line'> | null>(null);
const [priceData, setPriceData] = useState<PriceData[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [timeframe, setTimeframe] = useState<'1D' | '7D' | '1M' | '6M'>('6M');
const t = {
en: {
marketCap: 'Market Cap',
volume: '24h Volume',
price: 'Price',
loading: 'Loading chart data...',
error: 'Failed to load chart data',
timeframes: {
'1D': '1 Day',
'7D': '7 Days',
'1M': '1 Month',
'6M': '6 Months'
}
},
ru: {
marketCap: 'Капитализация',
volume: 'Объем 24ч',
price: 'Цена',
loading: 'Загрузка графика...',
error: 'Ошибка загрузки данных',
timeframes: {
'1D': '1 День',
'7D': '7 Дней',
'1M': '1 Месяц',
'6M': '6 Месяцев'
}
}
}[currentLanguage];
const formatNumber = (num: number) => {
if (num >= 1e9) return `$${(num / 1e9).toFixed(2)}B`;
if (num >= 1e6) return `$${(num / 1e6).toFixed(2)}M`;
if (num >= 1e3) return `$${(num / 1e3).toFixed(2)}K`;
return `$${num.toFixed(2)}`;
};
useEffect(() => {
const fetchHistoricalData = async () => {
try {
setLoading(true);
const days = timeframe === '1D' ? 1 : timeframe === '7D' ? 7 : timeframe === '1M' ? 30 : 180;
const response = await fetch(
`https://api.coingecko.com/api/v3/coins/${crypto.id}/market_chart?vs_currency=usd&days=${days}`
);
if (!response.ok) {
throw new Error('Failed to fetch historical data');
}
const data = await response.json();
const chartData = data.prices.map(([time, price]: [number, number]) => ({
time: time / 1000,
value: price
}));
setPriceData(chartData);
setError(null);
} catch (err) {
console.error('Error fetching historical data:', err);
setError(t.error);
} finally {
setLoading(false);
}
};
fetchHistoricalData();
}, [crypto.id, timeframe]);
useEffect(() => {
if (!chartContainerRef.current || !priceData.length) return;
const chartOptions = {
layout: {
background: { type: ColorType.Solid, color: 'transparent' },
textColor: 'rgba(255, 255, 255, 0.9)',
},
grid: {
vertLines: { color: 'rgba(255, 255, 255, 0.1)' },
horzLines: { color: 'rgba(255, 255, 255, 0.1)' },
},
timeScale: {
timeVisible: true,
secondsVisible: false,
borderColor: 'rgba(255, 255, 255, 0.1)',
},
rightPriceScale: {
borderColor: 'rgba(255, 255, 255, 0.1)',
},
crosshair: {
vertLine: {
color: 'rgba(255, 255, 255, 0.4)',
width: 1,
style: 3,
},
horzLine: {
color: 'rgba(255, 255, 255, 0.4)',
width: 1,
style: 3,
},
},
handleScroll: {
mouseWheel: true,
pressedMouseMove: true,
horzTouchDrag: true,
vertTouchDrag: true,
},
handleScale: {
axisPressedMouseMove: true,
mouseWheel: true,
pinch: true,
},
};
const newChart = createChart(chartContainerRef.current, chartOptions);
const newSeries = newChart.addLineSeries({
color: crypto.price_change_percentage_24h >= 0 ? '#34D399' : '#EF4444',
lineWidth: 2,
});
newSeries.setData(priceData);
newChart.timeScale().fitContent();
setChart(newChart);
setSeries(newSeries);
const handleResize = () => {
if (chartContainerRef.current) {
newChart.applyOptions({
width: chartContainerRef.current.clientWidth,
height: chartContainerRef.current.clientHeight,
});
}
};
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
newChart.remove();
};
}, [priceData, crypto.price_change_percentage_24h]);
return (
<div className="min-h-screen">
<div className="sticky top-0 z-50 bg-black/80 backdrop-blur-lg px-4 py-3 border-b border-white/10">
<div className="flex items-center space-x-4">
<button
onClick={onBack}
className="p-2 rounded-full hover:bg-white/5 transition-colors"
>
<ArrowLeft className="text-white" size={24} />
</button>
<div className="flex items-center space-x-3">
<img
src={crypto.image}
alt={crypto.name}
className="w-8 h-8 rounded-full"
/>
<div>
<h2 className="text-xl font-bold">{crypto.name}</h2>
<p className="text-sm text-gray-400">{crypto.symbol.toUpperCase()}</p>
</div>
</div>
</div>
</div>
<div className="p-4 space-y-4">
<GlassCard variant="settings" className="p-4">
<div className="grid grid-cols-2 gap-4">
<div>
<p className="text-sm text-gray-400">{t.price}</p>
<div className="flex items-center space-x-2">
<p className="text-xl font-mono font-semibold">
${crypto.current_price.toLocaleString()}
</p>
<div
className={`flex items-center text-sm ${
crypto.price_change_percentage_24h >= 0
? 'text-green-400'
: 'text-red-400'
}`}
>
{crypto.price_change_percentage_24h >= 0 ? (
<TrendingUp size={16} className="mr-1" />
) : (
<TrendingDown size={16} className="mr-1" />
)}
{Math.abs(crypto.price_change_percentage_24h).toFixed(2)}%
</div>
</div>
</div>
<div>
<p className="text-sm text-gray-400">{t.marketCap}</p>
<p className="text-lg font-mono">
{formatNumber(crypto.market_cap)}
</p>
</div>
<div>
<p className="text-sm text-gray-400">{t.volume}</p>
<p className="text-lg font-mono">
{formatNumber(crypto.total_volume)}
</p>
</div>
</div>
</GlassCard>
<GlassCard variant="settings" className="p-4">
<div className="flex items-center justify-between mb-4">
<div className="flex items-center space-x-2">
<Calendar size={16} className="text-gray-400" />
<span className="text-sm text-gray-400">
{t.timeframes[timeframe]}
</span>
</div>
<div className="flex space-x-2">
{(['1D', '7D', '1M', '6M'] as const).map((tf) => (
<button
key={tf}
onClick={() => setTimeframe(tf)}
className={`px-3 py-1 rounded-lg text-sm transition-colors ${
timeframe === tf
? 'bg-white/20 text-white'
: 'bg-white/5 text-gray-400 hover:bg-white/10'
}`}
>
{tf}
</button>
))}
</div>
</div>
{loading ? (
<div className="h-[400px] flex items-center justify-center">
<div className="text-white/50">{t.loading}</div>
</div>
) : error ? (
<div className="h-[400px] flex items-center justify-center">
<p className="text-red-400">{error}</p>
</div>
) : (
<div ref={chartContainerRef} className="h-[400px]" />
)}
</GlassCard>
</div>
</div>
);
};