import { useState } from 'react'; import { Brain, ChevronDown, ChevronUp, TrendingUp, BarChart2, MessageSquare, AlertTriangle, Target } from 'lucide-react'; import type { Decision } from '../types'; interface AIAnalysisPanelProps { analysis: string; decision?: Decision | null; defaultExpanded?: boolean; } interface Section { title: string; content: string; icon: typeof Brain; } function parseAnalysis(analysis: string): Section[] { const sections: Section[] = []; const iconMap: Record = { 'Summary': Target, 'Technical Analysis': BarChart2, 'Fundamental Analysis': TrendingUp, 'Sentiment': MessageSquare, 'Risks': AlertTriangle, }; // Split by markdown headers (##) const parts = analysis.split(/^## /gm).filter(Boolean); for (const part of parts) { const lines = part.trim().split('\n'); const title = lines[0].trim(); const content = lines.slice(1).join('\n').trim(); if (title && content) { sections.push({ title, content, icon: iconMap[title] || Brain, }); } } // If no sections found, treat the whole thing as a summary if (sections.length === 0 && analysis.trim()) { sections.push({ title: 'Analysis', content: analysis.trim(), icon: Brain, }); } return sections; } function AnalysisSection({ section, defaultOpen = true }: { section: Section; defaultOpen?: boolean }) { const [isOpen, setIsOpen] = useState(defaultOpen); const Icon = section.icon; return (
{isOpen && (
{section.content.split('\n').map((line, i) => { // Handle bullet points if (line.trim().startsWith('- ')) { return (
{line.trim().substring(2)}
); } return

{line}

; })}
)}
); } export default function AIAnalysisPanel({ analysis, decision, defaultExpanded = false, }: AIAnalysisPanelProps) { const [isExpanded, setIsExpanded] = useState(defaultExpanded); const sections = parseAnalysis(analysis); const decisionGradient = { BUY: 'from-green-500 to-emerald-600', SELL: 'from-red-500 to-rose-600', HOLD: 'from-amber-500 to-orange-600', }; const gradient = decision ? decisionGradient[decision] : 'from-nifty-500 to-nifty-700'; return (
{/* Header with gradient */} {/* Content */} {isExpanded && (
{sections.map((section, index) => ( ))}
)}
); }