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())}` ); const data = await res.json(); if (!res.ok) throw new Error(data.error || "API error"); setIndicators(data.indicators); } catch (err) { const message = err instanceof Error ? err.message : "Failed to fetch indicators."; setError(message); } finally { setLoading(false); } }; return (
setSymbol(e.target.value.toUpperCase())} placeholder="Enter stock symbol (e.g. AAPL)" className="flex-1 border border-gray-300 rounded-lg px-4 py-2.5 text-gray-900 placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent" onKeyDown={(e) => e.key === "Enter" && fetchIndicators()} />
{error && (

{error}

)} {indicators && (

Results for {symbol.toUpperCase()}

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