import { useState } from "react"; import { analyze, explainText } from "../api"; import type { AnalyzeResult, ExplainResult } from "../api"; import ConfidenceBars from "./ConfidenceBars"; import TokenHeatmap from "./TokenHeatmap"; const LABEL_BADGE: Record = { negative: "bg-red-100 text-red-700", neutral: "bg-slate-100 text-slate-700", positive: "bg-emerald-100 text-emerald-700", }; export default function AnalyzeForm() { const [text, setText] = useState(""); const [result, setResult] = useState(null); const [explanation, setExplanation] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); // Both buttons share one submit path; `withExplain` decides which endpoint. // Explain is opt-in because Integrated Gradients costs ~50 forward passes. const run = async (withExplain: boolean) => { if (!text.trim()) return; setLoading(true); setError(null); setExplanation(null); try { if (withExplain) { const res = await explainText(text); setResult(res); setExplanation(res); } else { setResult(await analyze(text)); } } catch (e) { setResult(null); setError(e instanceof Error ? e.message : "Something went wrong"); } finally { setLoading(false); } }; return (