|
1 | 1 |
|
2 | | -import React, { useEffect, useRef } from 'react'; |
| 2 | +import React, { useEffect, useMemo, useState } from 'react'; |
| 3 | + |
| 4 | +// A fully controlled, ultra-smooth ticker (no external widget) |
| 5 | +// - Fetches selected coins from CoinGecko public API |
| 6 | +// - Uses pure CSS animation you can precisely control (duration) |
| 7 | + |
| 8 | +type Coin = { |
| 9 | + id: string; |
| 10 | + symbol: string; |
| 11 | + label: string; |
| 12 | +}; |
| 13 | + |
| 14 | +const COINS: Coin[] = [ |
| 15 | + { id: 'bitcoin', symbol: 'BTC', label: 'Bitcoin' }, |
| 16 | + { id: 'ethereum', symbol: 'ETH', label: 'Ethereum' }, |
| 17 | + { id: 'binancecoin', symbol: 'BNB', label: 'BNB' }, |
| 18 | + { id: 'solana', symbol: 'SOL', label: 'Solana' }, |
| 19 | + { id: 'tether-gold', symbol: 'XAUt', label: 'Tether Gold' }, |
| 20 | + { id: 'chainlink', symbol: 'LINK', label: 'Chainlink' }, |
| 21 | + { id: 'polkadot', symbol: 'DOT', label: 'Polkadot' }, |
| 22 | + { id: 'avalanche-2', symbol: 'AVAX', label: 'Avalanche' }, |
| 23 | +]; |
| 24 | + |
| 25 | +const API = (ids: string[]) => |
| 26 | + `https://api.coingecko.com/api/v3/simple/price?ids=${ids.join(',')}&vs_currencies=usd&precision=2`; |
| 27 | + |
| 28 | +const DURATION_SEC = 180; // Adjust speed here (bigger = slower) |
| 29 | + |
| 30 | +const PriceTicker: React.FC = () => { |
| 31 | + const [prices, setPrices] = useState<Record<string, number>>({}); |
| 32 | + const [error, setError] = useState<string | null>(null); |
| 33 | + |
| 34 | + const ids = useMemo(() => COINS.map(c => c.id), []); |
3 | 35 |
|
4 | | -const PriceTicker = () => { |
5 | | - const containerRef = useRef<HTMLDivElement>(null); |
6 | | - |
7 | 36 | useEffect(() => { |
8 | | - // Remove any existing script from previous widgets |
9 | | - const existingScript = document.getElementById('coingecko-widget-script'); |
10 | | - if (existingScript) { |
11 | | - existingScript.remove(); |
12 | | - } |
13 | | - |
14 | | - // Create and add the CoinGecko script |
15 | | - const script = document.createElement('script'); |
16 | | - script.id = 'coingecko-widget-script'; |
17 | | - script.src = 'https://widgets.coingecko.com/gecko-coin-price-marquee-widget.js'; |
18 | | - script.async = true; |
19 | | - |
20 | | - // Cleanup function |
21 | | - const cleanup = () => { |
22 | | - script.remove(); |
23 | | - if (containerRef.current) { |
24 | | - containerRef.current.innerHTML = ''; |
25 | | - } |
26 | | - }; |
27 | | - |
28 | | - // Initialize widget after script loads |
29 | | - script.onload = () => { |
30 | | - if (containerRef.current) { |
31 | | - // Reset container to ensure proper initialization |
32 | | - containerRef.current.innerHTML = ''; |
33 | | - |
34 | | - // Create widget element with the new structure |
35 | | - const widgetElement = document.createElement('gecko-coin-price-marquee-widget'); |
36 | | - widgetElement.setAttribute('locale', 'es'); |
37 | | - widgetElement.setAttribute('dark-mode', 'true'); |
38 | | - widgetElement.setAttribute('transparent-background', 'true'); |
39 | | - widgetElement.setAttribute('outlined', 'true'); |
40 | | - widgetElement.setAttribute('coin-ids', 'bitcoin,tether-gold,ethereum,binancecoin,bitcoin-cash,bittensor,aave,solana,hyperliquid,avalanche-2,chainlink,injective-protocol,uniswap,internet-computer,aptos,cosmos,bitget-token,polkadot,sui,the-open-network,near,celestia,nexo,pi-network,tron,polygon-ecosystem-token,pancakeswap-token,osmosis,crypto-com-chain'); |
41 | | - widgetElement.setAttribute('initial-currency', 'usd'); |
42 | | - // Note: CoinGecko speed attribute doesn't always work, so we'll use CSS override |
43 | | - widgetElement.setAttribute('speed', '1'); |
44 | | - |
45 | | - // Add custom CSS to slow down the animation |
46 | | - const style = document.createElement('style'); |
47 | | - style.textContent = ` |
48 | | - gecko-coin-price-marquee-widget * { |
49 | | - animation-duration: 120s !important; |
50 | | - animation-timing-function: linear !important; |
51 | | - } |
52 | | - gecko-coin-price-marquee-widget [class*="marquee"] { |
53 | | - animation-duration: 120s !important; |
54 | | - } |
55 | | - `; |
56 | | - document.head.appendChild(style); |
57 | | - |
58 | | - containerRef.current.appendChild(widgetElement); |
59 | | - |
60 | | - console.log('CoinGecko widget initialized with new format'); |
61 | | - } |
62 | | - }; |
63 | | - |
64 | | - script.onerror = () => { |
65 | | - console.error('Error loading CoinGecko widget script'); |
66 | | - |
67 | | - // Fallback in case of error |
68 | | - if (containerRef.current) { |
69 | | - containerRef.current.innerHTML = ` |
70 | | - <div class="flex items-center justify-start gap-6 overflow-x-auto py-2 px-4 text-white"> |
71 | | - <div class="flex items-center gap-2"> |
72 | | - <span class="font-bold">BTC:</span> |
73 | | - <span class="text-alien-green">$64,750.21</span> |
74 | | - </div> |
75 | | - <div class="flex items-center gap-2"> |
76 | | - <span class="font-bold">ETH:</span> |
77 | | - <span class="text-alien-green">$3,145.89</span> |
78 | | - </div> |
79 | | - <div class="flex items-center gap-2"> |
80 | | - <span class="font-bold">BNB:</span> |
81 | | - <span class="text-alien-green">$596.24</span> |
82 | | - </div> |
83 | | - <div class="flex items-center gap-2"> |
84 | | - <span class="font-bold">SOL:</span> |
85 | | - <span class="text-alien-green">$152.36</span> |
86 | | - </div> |
87 | | - <div class="flex items-center gap-2"> |
88 | | - <span class="font-bold">COSMOS:</span> |
89 | | - <span class="text-alien-green">$6.82</span> |
90 | | - </div> |
91 | | - </div> |
92 | | - `; |
| 37 | + let mounted = true; |
| 38 | + |
| 39 | + const fetchPrices = async () => { |
| 40 | + try { |
| 41 | + setError(null); |
| 42 | + const res = await fetch(API(ids), { cache: 'no-store' }); |
| 43 | + if (!res.ok) throw new Error(`HTTP ${res.status}`); |
| 44 | + const data = await res.json(); |
| 45 | + if (!mounted) return; |
| 46 | + const mapped: Record<string, number> = {}; |
| 47 | + COINS.forEach(c => { |
| 48 | + const v = data?.[c.id]?.usd; |
| 49 | + if (typeof v === 'number') mapped[c.symbol] = v; |
| 50 | + }); |
| 51 | + setPrices(mapped); |
| 52 | + } catch (e: any) { |
| 53 | + if (!mounted) return; |
| 54 | + console.error('Ticker fetch failed', e); |
| 55 | + setError('offline'); |
93 | 56 | } |
94 | 57 | }; |
95 | | - |
96 | | - // Add the script to the document |
97 | | - document.body.appendChild(script); |
98 | | - |
99 | | - // Cleanup when unmounting the component |
| 58 | + |
| 59 | + fetchPrices(); |
| 60 | + const iv = setInterval(fetchPrices, 60_000); // refresh each minute |
100 | 61 | return () => { |
101 | | - cleanup(); |
| 62 | + mounted = false; |
| 63 | + clearInterval(iv); |
102 | 64 | }; |
103 | | - }, []); |
| 65 | + }, [ids]); |
| 66 | + |
| 67 | + const items = useMemo(() => { |
| 68 | + const list = Object.keys(prices).length |
| 69 | + ? COINS.map(c => ({ symbol: c.symbol, price: prices[c.symbol] })) |
| 70 | + : [ |
| 71 | + { symbol: 'BTC', price: 64750.21 }, |
| 72 | + { symbol: 'ETH', price: 3145.89 }, |
| 73 | + { symbol: 'BNB', price: 596.24 }, |
| 74 | + { symbol: 'SOL', price: 152.36 }, |
| 75 | + { symbol: 'XAUt', price: 2321.42 }, |
| 76 | + { symbol: 'LINK', price: 13.45 }, |
| 77 | + { symbol: 'DOT', price: 6.12 }, |
| 78 | + { symbol: 'AVAX', price: 28.77 }, |
| 79 | + ]; |
| 80 | + return list; |
| 81 | + }, [prices]); |
104 | 82 |
|
105 | 83 | return ( |
106 | 84 | <div className="w-full overflow-hidden bg-alien-space-dark/80 backdrop-blur-sm border-t border-b border-alien-gold/20 h-[40px]"> |
107 | | - <div |
108 | | - ref={containerRef} |
109 | | - className="w-full h-[40px]" |
110 | | - > |
111 | | - {/* The CoinGecko widget will be loaded here */} |
112 | | - <div className="flex items-center justify-start gap-6 overflow-x-auto py-2 px-4 text-white"> |
113 | | - <div className="flex items-center gap-2"> |
114 | | - <span className="font-bold">BTC:</span> |
115 | | - <span className="text-alien-green">$64,750.21</span> |
116 | | - </div> |
117 | | - <div className="flex items-center gap-2"> |
118 | | - <span className="font-bold">ETH:</span> |
119 | | - <span className="text-alien-green">$3,145.89</span> |
120 | | - </div> |
121 | | - <div className="flex items-center gap-2"> |
122 | | - <span className="font-bold">BNB:</span> |
123 | | - <span className="text-alien-green">$596.24</span> |
124 | | - </div> |
125 | | - <div className="flex items-center gap-2"> |
126 | | - <span className="font-bold">SOL:</span> |
127 | | - <span className="text-alien-green">$152.36</span> |
128 | | - </div> |
129 | | - <div className="flex items-center gap-2"> |
130 | | - <span className="font-bold">COSMOS:</span> |
131 | | - <span className="text-alien-green">$6.82</span> |
132 | | - </div> |
| 85 | + {/* Local keyframes to avoid touching global CSS */} |
| 86 | + <style>{` |
| 87 | + @keyframes ticker-scroll { from { transform: translateX(0); } to { transform: translateX(-50%); } } |
| 88 | + `}</style> |
| 89 | + |
| 90 | + <div className="relative w-full h-[40px]"> |
| 91 | + {/* Track wrapper */} |
| 92 | + <div className="absolute inset-0 flex items-center"> |
| 93 | + {/* Two tracks for seamless loop */} |
| 94 | + {[0,1].map(i => ( |
| 95 | + <div |
| 96 | + key={i} |
| 97 | + className="flex items-center gap-6 px-4 whitespace-nowrap" |
| 98 | + style={{ |
| 99 | + animation: `ticker-scroll ${DURATION_SEC}s linear infinite`, |
| 100 | + // Stagger the second copy to start where first ends |
| 101 | + animationDelay: i === 1 ? `${DURATION_SEC/2}s` : '0s', |
| 102 | + }} |
| 103 | + > |
| 104 | + {items.map((it, idx) => ( |
| 105 | + <div key={`${i}-${idx}`} className="flex items-center gap-2"> |
| 106 | + <span className="font-bold">{it.symbol}:</span> |
| 107 | + <span className="text-alien-green">${it.price.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}</span> |
| 108 | + </div> |
| 109 | + ))} |
| 110 | + </div> |
| 111 | + ))} |
133 | 112 | </div> |
134 | 113 | </div> |
135 | 114 | </div> |
|
0 commit comments