A small React + TypeScript single-page application for visualizing live crypto market data. It displays candlestick charts and an order book for selected trading pairs, fetching initial data over HTTP and subscribing to a WebSocket server for live updates. This project is a frontend task/demo intended to show real-time market data visualization with simple, testable components.
- Purpose: Demo frontend to visualize live candlestick charts and order book data for crypto pairs.
- Primary features:
- Candlestick chart (Google Charts)
- Order book view (asks and bids)
- WebSocket subscription for live updates
- Lightweight, testable components with Tailwind CSS styling
- Framework: React (v19)
- Language: TypeScript
- Bundler / Dev server: Vite
- Styling: Tailwind CSS
- Charts:
react-google-charts(CandlestickChart) - WebSocket client:
react-use-websocket - State Management:
mobx/mobx-react - Testing: Vitest + Testing Library (
vitest,@testing-library/react,@testing-library/jest-dom) - Dev tooling:
@vitejs/plugin-react,typescript,jsdom,biomeavailable in devDependencies
src/App.tsx— Main app: fetches initial data, manages selected pair/stream, subscribes to WebSocket and routes incoming messages to formatters/state.src/components/CandleChart/CandleChart.tsx— Candlestick chart wrapper usingreact-google-charts. Shows spinner while loading.src/components/OrderBook/OrderBook.tsx— Order book UI (asks/bids), calculates & displays current market price, shows spinner while loading.src/constants/constants.ts— Chart options and coin metadata.src/constants/types.ts— Shared TypeScript types for candles and order book items.src/utils/endpoints.ts—getCandlesandgetOrderBookinitial HTTP fetchers.src/utils/formatters.ts—ArrAPItoChartDataandAppendItemToChartDataconverters for chart input.vite.config.ts,tsconfig*.json,package.json— Build and tooling configuration.
-
src/App.tsx- Uses: React hooks (
useState,useEffect),react-use-websocket. - Responsibilities:
- Fetch initial candles and orderbook via
getCandles/getOrderBook. - Transform candle API responses using
ArrAPItoChartData. - Subscribe to WebSocket via
sendJsonMessage({ type: "subscribe", pair, stream }). - Update local state on incoming
lastJsonMessagevalues.
- Fetch initial candles and orderbook via
- Uses: React hooks (
-
src/components/CandleChart/CandleChart.tsx- Uses:
react-google-charts'sChartcomponent. - Responsibilities:
- Render
CandlestickChartwithCHART_OPTIONS. - Display a centered spinner when
loadingprop istrue.
- Render
- Test strategy:
- Mock
react-google-chartsto a lightweight stub in unit tests.
- Mock
- Uses:
-
src/components/OrderBook/OrderBook.tsx- Uses: Plain React + Tailwind CSS. No external component library required.
- Responsibilities:
- Render lists of asks and bids.
- Compute
bestPriceas the average of top ask and top bid:((asks[0]?.price || 0) + (bids[0]?.price || 0)) / 2. - Display spinner when
loadingprop istrue.
- Edge cases:
- Empty asks / bids are handled using optional chaining and defaults.
-
src/utils/endpoints.ts- Exports expected:
getCandles(pair)andgetOrderBook(pair). - Responsibilities: fetch initial data shapes used by formatters and components.
- Exports expected:
-
src/utils/formatters.ts- Exports:
ArrAPItoChartDataandAppendItemToChartData. - Responsibilities: adapt API candle arrays to the
react-google-charts2D array format (header row + numeric rows) and append updates.
- Exports:
- Hook:
react-use-websocket - URL used in source:
ws://localhost:3001 - Flow:
- On mount and when
selectedCoin/selectedDatachange,Appsends a subscribe message. - Incoming messages appear in
lastJsonMessage.Apphandlestype === 'candle_update' | 'initial_candles'and orderbook-related message types, updating state appropriately.
- On mount and when
- Install dependencies and run project:
npm install
npm run dev