-
Notifications
You must be signed in to change notification settings - Fork 0
setup-the-dashbaord-perfectly #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import React, { useState, useEffect } from 'react'; | ||
| import '@/styles/components/live-market.css'; | ||
|
|
||
| export const LiveMarketDashboard: React.FC = () => { | ||
| const [marketData, setMarketData] = useState({ gainers: [], losers: [] }); | ||
| const [activeTab, setActiveTab] = useState('Stocks'); | ||
|
|
||
| useEffect(() => { | ||
| // 1. Connect to the FastAPI WebSocket Bridge | ||
| const ws = new WebSocket("ws://localhost:8000/dashboard/ws/market"); | ||
|
|
||
| // 2. Listen for live updates being pushed from FastAPI | ||
| ws.onmessage = (event) => { | ||
| try { | ||
| const liveData = JSON.parse(event.data); | ||
| console.log("Live Update Received:", liveData); | ||
| // Instantly update the React state | ||
| setMarketData(liveData); | ||
| } catch (err) { | ||
| console.error("Error parsing websocket message", err); | ||
| } | ||
| }; | ||
|
|
||
| // 3. Clean up the connection if the user leaves the page | ||
| return () => ws.close(); | ||
| }, []); | ||
|
|
||
| const formatPrice = (price: number) => { | ||
| if (!price) return "0.00"; | ||
| if (price < 0.01) { | ||
| return price.toExponential(5); | ||
| } | ||
| return price.toFixed(2); | ||
| }; | ||
|
|
||
| const getInitials = (symbol: string) => { | ||
| return symbol.substring(0, 2).toUpperCase(); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="market-dashboard"> | ||
| <div className="top-nav"> | ||
| <div className="nav-pills"> | ||
| {['Stocks', 'Crypto', 'Futures', 'Forex', 'Economy', 'Brokers'].map(tab => ( | ||
| <button | ||
| key={tab} | ||
| className={`nav-pill ${activeTab === tab ? 'active' : ''}`} | ||
| onClick={() => setActiveTab(tab)} | ||
| > | ||
| {tab} | ||
| </button> | ||
| ))} | ||
| </div> | ||
| </div> | ||
|
|
||
| <div className="market-sections"> | ||
| {/* Gainers */} | ||
| <div className="market-list-section"> | ||
| <div className="section-header"> | ||
| <h2> | ||
| {activeTab === 'Stocks' ? 'Stock' : activeTab} gainers | ||
| <span className="heading-arrow">></span> | ||
| </h2> | ||
| </div> | ||
|
|
||
| <div className="stock-list"> | ||
| {marketData.gainers && marketData.gainers.length > 0 ? ( | ||
| marketData.gainers.map((stock: any) => ( | ||
| <div className="stock-row" key={stock.symbol}> | ||
| <div className="stock-left"> | ||
| <div className="stock-icon icon-blue">{getInitials(stock.symbol)}</div> | ||
| <div className="stock-names"> | ||
| <div className="stock-name">{stock.symbol.split('.')[0]}</div> | ||
| <div className="stock-ticker">{stock.symbol}</div> | ||
| </div> | ||
| </div> | ||
| <div className="stock-middle"> | ||
| <span className="price">{formatPrice(stock.price)}</span> | ||
| <span className="currency">{stock.currency || 'USD'}</span> | ||
| </div> | ||
| <div className="stock-right"> | ||
| <div className="pill-gain">+{Math.abs(stock.percent_change).toFixed(2)}%</div> | ||
| </div> | ||
| </div> | ||
| )) | ||
| ) : ( | ||
| <div className="loading-state">Waiting for data...</div> | ||
| )} | ||
| </div> | ||
| </div> | ||
|
|
||
| {/* Losers */} | ||
| <div className="market-list-section"> | ||
| <div className="section-header"> | ||
| <h2> | ||
| {activeTab === 'Stocks' ? 'Stock' : activeTab} losers | ||
| <span className="heading-arrow">></span> | ||
| </h2> | ||
| </div> | ||
|
|
||
| <div className="stock-list"> | ||
| {marketData.losers && marketData.losers.length > 0 ? ( | ||
| marketData.losers.map((stock: any) => ( | ||
| <div className="stock-row" key={stock.symbol}> | ||
| <div className="stock-left"> | ||
| <div className="stock-icon icon-purple">{getInitials(stock.symbol)}</div> | ||
| <div className="stock-names"> | ||
| <div className="stock-name">{stock.symbol.split('.')[0]}</div> | ||
| <div className="stock-ticker">{stock.symbol}</div> | ||
| </div> | ||
| </div> | ||
| <div className="stock-middle"> | ||
| <span className="price">{formatPrice(stock.price)}</span> | ||
| <span className="currency">{stock.currency || 'USD'}</span> | ||
| </div> | ||
| <div className="stock-right"> | ||
| <div className="pill-loss"> | ||
| -{Math.abs(stock.percent_change).toFixed(2)}% | ||
| </div> | ||
| </div> | ||
| </div> | ||
| )) | ||
| ) : ( | ||
| <div className="loading-state">Waiting for data...</div> | ||
| )} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Avoid hardcoding localhost URLs.
The WebSocket connection uses a hardcoded
localhostURL, which will fail when the application is deployed to production or accessed from another device. Consider deriving the WebSocket URL from the current window location or using an environment variable.🛠 Proposed fix
For example, using Vite environment variables:
📝 Committable suggestion
🤖 Prompt for AI Agents