'use client' import { useEffect, useState } from 'react' import { getLiveQuote, formatMarketValue, type MappedQuote } from '@/lib/stockApi' interface StockCardProps { symbol?: string } export function StockCard({ symbol = 'AAPL' }: StockCardProps) { const [quote, setQuote] = useState(null) const [error, setError] = useState(null) const [loading, setLoading] = useState(true) useEffect(() => { setLoading(true) setError(null) getLiveQuote(symbol) .then(setQuote) .catch((err) => setError(err.message)) .finally(() => setLoading(false)) }, [symbol]) if (loading) { return (
Loading {symbol}...
) } if (error || !quote) { return (
{symbol}
{error ?? 'No data available'}
) } const changeColor = quote.priceChange > 0 ? 'text-positive' : quote.priceChange < 0 ? 'text-negative' : 'text-foreground' const changeSign = quote.priceChange > 0 ? '+' : '' return (
{quote.symbol}
${quote.lastPrice.toFixed(2)}
{changeSign}${quote.priceChange.toFixed(2)} ({changeSign}{quote.percentChange.toFixed(2)}%)
Open {formatMarketValue(quote.openPrice, quote.marketOpen, (v) => `$${v.toFixed(2)}`)}
Prev Close ${quote.previousClose.toFixed(2)}
Volume {formatMarketValue(quote.volume, quote.marketOpen, (v) => v.toLocaleString())}
) }