Skip to content

Commit 9d28202

Browse files
committed
fix
1 parent 1c61e2f commit 9d28202

File tree

6 files changed

+48
-261
lines changed

6 files changed

+48
-261
lines changed

app/page.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import {
1818
} from '@/components/ui/table';
1919

2020
import ChartSection from '@/components/ChartSection';
21-
// import CoinCard from '@/components/CoinCard';
22-
// import { popularCoins } from '@/lib/constants';
2321
import { TrendingDown, TrendingUp } from 'lucide-react';
2422
import CoinCard from '@/components/CoinCard';
2523

@@ -29,14 +27,12 @@ const Home = async () => {
2927
const coinData = await getCoinDetails('bitcoin');
3028
const coinOHLCData = await getCoinOHLC(
3129
'bitcoin',
32-
30, // days
33-
'usd', // vs_currency
34-
'hourly', // interval
35-
'full' // precision
30+
30,
31+
'usd',
32+
'hourly',
33+
'full'
3634
);
3735

38-
console.log('===topGainersLosers', topGainersLosers);
39-
4036
return (
4137
<main className='py-6 md:py-12 container size-full space-y-6 md:space-y-6'>
4238
<section className='grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 items-start lg:items-center gap-6'>

components/LiveCoinHeader.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default function LiveCoinHeader({
1717
name,
1818
image,
1919
}: LiveCoinHeaderProps) {
20-
const { prices, connected } = useCoinPrice([coinId]);
20+
const { prices } = useCoinPrice([coinId]);
2121
const priceData = prices[coinId];
2222

2323
const isTrendingUp = priceData

components/LivePriceDisplay.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import { formatPrice, formatPercentage } from '@/lib/utils';
55
import { cn } from '@/lib/utils';
66

77
export default function LivePriceDisplay({ coinId }: { coinId: string }) {
8-
const { prices, connected } = useCoinPrice([coinId]) as LiveCoinPrice;
8+
const { prices } = useCoinPrice([coinId]);
99
const priceData = prices[coinId];
1010

11-
console.log('======Websocket connected', connected);
12-
console.log('======Websocket prices', prices);
11+
if (!priceData) {
12+
return <div className='text-sm text-gray-400'>Loading...</div>;
13+
}
1314

1415
const isTrendingUp = priceData.priceChangePercentage24h > 0;
1516

hooks/useCoinGeckoSocket.ts

Lines changed: 0 additions & 166 deletions
This file was deleted.

hooks/useCoinPrice.ts

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,35 @@
1-
import { useState } from 'react';
2-
import { useCoinGeckoSocket } from './useCoinGeckoSocket';
3-
4-
type PriceInfo = {
5-
coinId: string;
6-
price: number;
7-
priceChangePercentage24h: number;
8-
marketCap: number;
9-
volume24h: number;
10-
lastUpdated: number;
11-
};
12-
13-
type PricesMap = Record<string, PriceInfo>;
1+
import { useEffect, useState } from 'react';
2+
// import { subscribe } from '@/lib/websocket';
143

154
export const useCoinPrice = (coinIds: string[]) => {
165
const [prices, setPrices] = useState<PricesMap>({});
17-
const [connected, setConnected] = useState(false);
6+
const coinIdsKey = coinIds.join(',');
7+
8+
useEffect(() => {
9+
const cleanupFunctions: (() => void)[] = [];
10+
11+
// Subscribe to each coin
12+
// coinIds.forEach((coinId) => {
13+
// const cleanup = subscribe(coinId, (allPrices) => {
14+
// setPrices(allPrices);
15+
// });
16+
// cleanupFunctions.push(cleanup);
17+
// });
18+
19+
// Cleanup all subscriptions
20+
return () => {
21+
cleanupFunctions.forEach((cleanup) => cleanup());
22+
};
23+
24+
}, [coinIdsKey]); // Use string key to avoid array reference changes
1825

19-
useCoinGeckoSocket({
20-
channel: 'CGSimplePrice',
21-
subscribeParams: coinIds,
22-
subscribeMessage: (coins) => ({
23-
command: 'message',
24-
identifier: JSON.stringify({ channel: 'CGSimplePrice' }),
25-
data: JSON.stringify({ action: 'set_tokens', coin_id: coins }),
26-
}),
27-
onReady: () => setConnected(true),
28-
onData: (msg) => {
29-
if (msg.c === 'C1' && msg.i && msg.p !== undefined) {
30-
setPrices((prev) => ({
31-
...prev,
32-
[msg.i]: {
33-
coinId: msg.i,
34-
price: msg.p,
35-
priceChangePercentage24h: msg.pp || 0,
36-
marketCap: msg.m || 0,
37-
volume24h: msg.v || 0,
38-
lastUpdated: msg.t || Date.now() / 1000,
39-
},
40-
}));
41-
}
42-
},
26+
// Filter prices to only include requested coins
27+
const filteredPrices: PricesMap = {};
28+
coinIds.forEach((coinId) => {
29+
if (prices[coinId]) {
30+
filteredPrices[coinId] = prices[coinId];
31+
}
4332
});
4433

45-
return { prices, connected };
34+
return { prices: filteredPrices };
4635
};

types.d.ts

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
21
type OHLCData = [number, number, number, number, number];
32

43
interface CandlestickChartProps {
@@ -94,34 +93,21 @@ interface SearchCoin {
9493
};
9594
}
9695

97-
interface LiveCoinPrice {
98-
prices: {
99-
[key: string]: {
100-
price: number;
101-
priceChangePercentage24h: number;
102-
marketCap: number;
103-
volume24h: number;
104-
};
105-
};
106-
connected: boolean;
107-
}
108-
109-
interface UseCoinGeckoSocketProps {
110-
channel: string;
111-
onReady: (socket: WebSocket) => void;
112-
onData: (message: any) => void;
96+
// WebSocket Types
97+
interface CoinPriceData {
98+
coinId: string;
99+
price: number;
100+
priceChangePercentage24h: number;
101+
marketCap: number;
102+
volume24h: number;
103+
lastUpdated: number;
113104
}
114105

115-
interface CGSimplePriceMessage {
116-
c: string;
117-
i: string;
118-
m: number;
119-
p: number;
120-
pp: number;
121-
t: number;
122-
v: number;
106+
interface PricesMap {
107+
[coinId: string]: CoinPriceData;
123108
}
124109

110+
// Chart Section Props
125111
interface ChartSectionProps {
126112
coinData: {
127113
image: { large: string };
@@ -135,25 +121,6 @@ interface ChartSectionProps {
135121
coinId: string;
136122
}
137123

138-
interface CoinPriceData {
139-
coinId: string;
140-
price: number;
141-
priceChangePercentage24h: number;
142-
marketCap: number;
143-
volume24h: number;
144-
lastUpdated: number;
145-
}
146-
147-
interface PricesMap {
148-
[coinId: string]: CoinPriceData;
149-
}
150-
151-
type UseCoinGeckoSocketProps = {
152-
channel: string;
153-
onReady: (socket: WebSocket) => void;
154-
onData: (data: any) => void;
155-
};
156-
157124
interface TopGainersLosers {
158125
id: string;
159126
name: string;

0 commit comments

Comments
 (0)