import React, { useEffect, useState } from 'react'; import { blogWriterApi } from '../../services/blogWriterApi'; interface Props { sectionId: string; refreshToken?: number; disabled?: boolean; flowAnalysisResults?: any; } export const ContinuityBadge: React.FC = ({ sectionId, refreshToken, disabled = false, flowAnalysisResults }) => { const [metrics, setMetrics] = useState | null>(null); const [hover, setHover] = useState(false); useEffect(() => { let mounted = true; // If we have flow analysis results, use them instead of API call if (flowAnalysisResults && flowAnalysisResults.sections) { console.log('🔍 [ContinuityBadge] Flow analysis results available:', flowAnalysisResults); console.log('🔍 [ContinuityBadge] Looking for section ID:', sectionId); console.log('🔍 [ContinuityBadge] Available section IDs:', flowAnalysisResults.sections.map((s: any) => s.section_id)); const sectionAnalysis = flowAnalysisResults.sections.find((s: any) => s.section_id === sectionId); if (sectionAnalysis) { console.log('🔍 [ContinuityBadge] Found section analysis:', sectionAnalysis); if (mounted) { setMetrics({ flow: sectionAnalysis.flow_score, // Already in decimal format (0.0-1.0) consistency: sectionAnalysis.consistency_score, progression: sectionAnalysis.progression_score }); } return; } else { console.log('🔍 [ContinuityBadge] No matching section found for ID:', sectionId); } } // Fallback to API call if no flow analysis results console.log('🔍 [ContinuityBadge] Fetching continuity for section:', sectionId); blogWriterApi.getContinuity(sectionId) .then(res => { console.log('🔍 [ContinuityBadge] Received continuity data:', res); if (mounted) setMetrics(res.continuity_metrics || null); }) .catch((error) => { console.log('🔍 [ContinuityBadge] Error fetching continuity:', error); /* ignore */ }); return () => { mounted = false; }; }, [sectionId, refreshToken, flowAnalysisResults]); // Show badge even if metrics are null (for debugging) const flow = metrics ? Math.round(((metrics.flow || 0) * 100)) : 0; const consistency = metrics ? Math.round(((metrics.consistency || 0) * 100)) : 0; const progression = metrics ? Math.round(((metrics.progression || 0) * 100)) : 0; // Enable badge if we have flow analysis results or metrics const isEnabled = !disabled || (flowAnalysisResults && flowAnalysisResults.sections) || metrics; // Enhanced color coding with actionable feedback const getFlowColor = (score: number) => { if (score >= 80) return '#2e7d32'; // Green - Excellent if (score >= 60) return '#f9a825'; // Yellow - Good return '#c62828'; // Red - Needs improvement }; const getFlowSuggestion = (score: number) => { if (score >= 80) return "🎉 Excellent narrative flow!"; if (score >= 60) return "💡 Good flow - try connecting ideas more smoothly"; return "🔧 Consider adding transitions between paragraphs"; }; const getConsistencySuggestion = (score: number) => { if (score >= 80) return "✨ Consistent tone and style"; if (score >= 60) return "📝 Good consistency - maintain your voice"; return "🎯 Work on maintaining consistent tone throughout"; }; const getProgressionSuggestion = (score: number) => { if (score >= 80) return "🚀 Great logical progression!"; if (score >= 60) return "📈 Good progression - build on previous points"; return "🔗 Strengthen connections between ideas"; }; const color = getFlowColor(flow); return ( setHover(true)} onMouseLeave={() => setHover(false)} style={{ position: 'relative', display: 'inline-block' }} > {!isEnabled ? 'Flow --' : (metrics ? `Flow ${flow}%` : 'Flow --')} {hover && isEnabled && (
📊 Content Quality Analysis
{/* Flow Analysis */}
Flow {flow}%
{getFlowSuggestion(flow)}
{/* Consistency Analysis */}
Consistency {consistency}%
{getConsistencySuggestion(consistency)}
{/* Progression Analysis */}
Progression {progression}%
{getProgressionSuggestion(progression)}
{/* Overall Quality Indicator */}
💡 Hover over other sections to compare quality metrics
)}
); }; export default ContinuityBadge;