Skip to content

Commit 8321242

Browse files
committed
fix
1 parent 2fcf75c commit 8321242

File tree

4 files changed

+38
-48
lines changed

4 files changed

+38
-48
lines changed

app/coins/[id]/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const CoinDetails = async ({ params }: { params: Promise<{ id: string }> }) => {
2323
const { id } = await params;
2424
const coinData = await getCoinDetails(id);
2525
const topGainersLosers = await getTopGainersLosers();
26-
const coinOHLCData = await getCoinOHLC(id, 30, 'usd', 'hourly', 'full');
26+
const coinOHLCData = await getCoinOHLC(id, 1, 'usd', 'hourly', 'full');
2727
const pool = await fetchPools(id);
2828

2929
const coin = {

app/page.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ const Home = async () => {
2424
const coinData = await getCoinDetails('bitcoin');
2525
const trendingCoins = await getTrendingCoins();
2626
const categories = await getCategories();
27-
const coinOHLCData = await getCoinOHLC(
28-
'bitcoin',
29-
30,
30-
'usd',
31-
'hourly',
32-
'full'
33-
);
27+
const coinOHLCData = await getCoinOHLC('bitcoin', 1, 'usd', 'hourly', 'full');
3428

3529
return (
3630
<main className='main-container'>

hooks/useCoinGeckoWebSocket.ts

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ export function useCoinGeckoWebSocket({
2222
const handleMessage = useCallback((event: MessageEvent) => {
2323
const ws = wsRef.current;
2424
const msg: WebSocketMessage = JSON.parse(event.data);
25-
25+
// Ping/Pong checking to keep connection alive as suggested in their docs
2626
if (msg.type === 'ping') return ws?.send(JSON.stringify({ type: 'pong' }));
2727

2828
if (msg.type === 'confirm_subscription') {
2929
const { channel } = JSON.parse(msg?.identifier ?? '');
3030
subscribed.current.add(channel);
31-
console.log(`Subscribed to: ${channel}`);
31+
3232
return;
3333
}
3434

@@ -62,44 +62,40 @@ export function useCoinGeckoWebSocket({
6262
});
6363
}
6464

65-
// G3: OHLCV updates
66-
if (msg.ch === 'G3') {
67-
setOhlcv((prev) => {
68-
const lastCandle = prev[prev.length - 1];
69-
70-
const newTimeMs = msg.t ?? 0;
71-
const newCandle: OHLCData = [
72-
newTimeMs,
73-
Number(msg.o ?? 0),
74-
Number(msg.h ?? 0),
75-
Number(msg.l ?? 0),
76-
Number(msg.c ?? 0),
77-
];
78-
79-
// If same timestamp, update the existing candle
80-
if (lastCandle && lastCandle[0] === newTimeMs) {
81-
return [...prev.slice(0, -1), newCandle];
82-
}
83-
84-
// Only append if timestamp is newer than the last candle
85-
if (lastCandle && newTimeMs < lastCandle[0]) {
86-
console.warn(
87-
'Skipping out-of-order candle:',
88-
newTimeMs,
89-
'vs',
90-
lastCandle[0]
91-
);
92-
return prev;
93-
}
94-
95-
// Keep all historical data + last 100 live candles
96-
const historicalCount = historicalDataLength.current;
97-
const liveCandles = prev.slice(historicalCount);
98-
const limitedLiveCandles = [...liveCandles, newCandle].slice(-100);
99-
100-
return [...prev.slice(0, historicalCount), ...limitedLiveCandles];
101-
});
65+
// G3: OHLCV updates
66+
if (msg.ch === 'G3') {
67+
setOhlcv((prev) => {
68+
const newTimeMs = (msg.t ?? 0) * 1000;
69+
console.log('Received OHLCV update for time:', newTimeMs);
70+
71+
const newCandle: OHLCData = [
72+
newTimeMs,
73+
Number(msg.o ?? 0),
74+
Number(msg.h ?? 0),
75+
Number(msg.l ?? 0),
76+
Number(msg.c ?? 0),
77+
];
78+
79+
const historicalCount = historicalDataLength.current;
80+
const historical = prev.slice(0, historicalCount);
81+
const live = prev.slice(historicalCount);
82+
83+
// 🔑 Upsert by timestamp
84+
const map = new Map<number, OHLCData>();
85+
for (const candle of live) {
86+
map.set(candle[0], candle);
10287
}
88+
map.set(newTimeMs, newCandle);
89+
90+
// 🔑 Sort by time ASC
91+
const updatedLive = Array.from(map.values())
92+
.sort((a, b) => a[0] - b[0])
93+
.slice(-100);
94+
95+
return [...historical, ...updatedLive];
96+
});
97+
}
98+
10399
}, []);
104100

105101
// WebSocket connection setup

lib/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const navItems = [
1515
href: '/',
1616
},
1717
{
18-
label: 'Coins',
18+
label: 'All Coins',
1919
href: '/coins',
2020
},
2121
];

0 commit comments

Comments
 (0)