Overview
TheuseDashboardData hook is the primary data management hook for the CryptoDash application. It fetches global market statistics, top market coins, Bitcoin performance data, and manages portfolio state. The hook provides comprehensive dashboard data with formatted values ready for display.
Import
Signature
Return Value
Returns an object containing all dashboard state and functions:Loading state indicator for initial data fetch
Error message if data fetching fails
Formatted global market capitalization (e.g., “$2.5T”)
Formatted 24h market cap change percentage (e.g., “+2.34%”)
Whether the market cap change is positive or negative
Formatted global trading volume (e.g., “$120.5B”)
Array of summary card data objects, each containing:
title: Card title (e.g., “Total Portfolio Balance”)value: Formatted display valuechange: Formatted percentage changepositive: Boolean indicating positive/negative changepath: SVG path for sparkline chart
Array of market table row data, each containing:
rank: Market cap rankname: Coin namesymbol: Coin symbol (uppercase)price: Formatted pricechange: Formatted 24h change percentagepositive: Boolean for change directionmarketCap: Formatted market capvolume: Formatted volumeiconBg,iconColor,icon: Icon styling datatrendPath: SVG path for 7-day trend
SVG path string for Bitcoin 30-day performance chart
Raw Bitcoin price data points for the chart
Formatted current Bitcoin price
Formatted Bitcoin price change percentage
Whether Bitcoin performance is positive
Raw market data array from the API
Current user’s primary portfolio object with positions
SVG path for portfolio performance chart
Portfolio value data points over time
Formatted current portfolio value
Formatted portfolio change percentage
Whether portfolio performance is positive
Default USD allocation amount for adding new assets
Portfolio summary object containing:
name: Portfolio nametotalValueUsd: Total portfolio value in USDinvestedUsd: Total invested amountchange24hUsd: 24-hour change in USDchange24hPercent: 24-hour change percentage
Async function to add an asset to the portfolio. See below for details.
Functions
addAssetToPortfolio
Adds a cryptocurrency asset to the user’s primary portfolio. Signature:The cryptocurrency asset ID (e.g., “bitcoin”, “ethereum”)
USD amount to allocate for this asset. Defaults to
getDefaultAllocationUsd()Usage Example
Basic Usage in Layout
useDashboardData.js:7-26
Adding Assets to Portfolio
Accessing Market Data
Implementation Details
-
Data Sources: Fetches from multiple APIs concurrently using
Promise.all- Global market statistics via
getGlobalStats() - Top market coins via
getTopMarketCoins() - Bitcoin 30-day chart via
getCoinPerformanceChart()
- Global market statistics via
- Portfolio Management: Automatically creates or retrieves the user’s primary portfolio
- Performance Calculation: Builds portfolio performance series by combining position amounts with historical sparkline data
-
Memoization: Uses
useMemoextensively to prevent unnecessary recalculations - Cleanup: Properly handles component unmounting to prevent state updates on unmounted components
Related Hooks
- useTranslations - Used internally for error messages and labels
Source Location
/workspace/source/src/hooks/useDashboardData.js:45