import { useState } from 'react' import Plot from 'react-plotly.js' import { fetchIVSurface } from '../api/client' import { usePortfolioStore } from '../store/portfolio' export default function IVSurface() { const { ticker } = usePortfolioStore() const [data, setData] = useState(null) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [optType, setOptType] = useState('call') const load = async () => { setLoading(true); setError(null) try { const result = await fetchIVSurface(ticker) setData(result) } catch (e) { setError(e?.response?.data?.detail || 'Failed to fetch option chain.') } finally { setLoading(false) } } const filtered = data?.data.filter(d => d.type === optType) ?? [] const expiries = [...new Set(filtered.map(d => d.expiry))].sort() const strikes = [...new Set(filtered.map(d => d.strike))].sort((a, b) => a - b) // Build IV matrix: rows=strikes, cols=expiries const Z = strikes.map(k => expiries.map(exp => { const pt = filtered.find(d => d.strike === k && d.expiry === exp) return pt ? pt.iv * 100 : null }) ) const plotData = data ? [{ type: 'surface', x: expiries, y: strikes, z: Z, colorscale: 'RdYlGn_r', colorbar: { title: 'IV (%)', tickfont: { color: '#e2e8f0' } }, contours: { z: { show: true, usecolormap: true, project: { z: true } } }, }] : [] return (