import { useState } from "react"; export default function StockViewer() { const [symbol, setSymbol] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [indicators, setIndicators] = useState<{ sma: number; ema: number; rsi: number; macd: number; } | null>(null); const fetchIndicators = async () => { if (!symbol.trim()) return; setLoading(true); setError(null); try { const res = await fetch( `/api/indicators?symbol=${encodeURIComponent(symbol.trim())}` ); if (!res.ok) throw new Error("API error"); const data = await res.json(); setIndicators(data.indicators); } catch { setError("Failed to fetch indicators. Check the symbol and try again."); } finally { setLoading(false); } }; return (

Stock Indicators

setSymbol(e.target.value)} placeholder="Enter stock symbol (e.g. AAPL)" className="flex-1 border rounded p-2" onKeyDown={(e) => e.key === "Enter" && fetchIndicators()} />
{error &&

{error}

} {indicators && (

Results for {symbol.toUpperCase()}

{Object.entries(indicators).map(([key, value]) => ( ))}
Indicator Value
{key} {value.toFixed(2)}
)}
); }