import React, { useState } from 'react'; import { BlogOutlineSection, BlogResearchResponse, blogWriterApi } from '../../services/blogWriterApi'; interface EnhancedTitleSelectorProps { titleOptions: string[]; selectedTitle?: string; onTitleSelect: (title: string) => void; onCustomTitle?: (title: string) => void; sections: BlogOutlineSection[]; researchTitles?: string[]; aiGeneratedTitles?: string[]; research?: BlogResearchResponse; onTitlesGenerated?: (titles: string[]) => void; } const EnhancedTitleSelector: React.FC = ({ titleOptions, selectedTitle, onTitleSelect, onCustomTitle, sections, researchTitles = [], aiGeneratedTitles = [], research, onTitlesGenerated }) => { const [showModal, setShowModal] = useState(false); const [customTitle, setCustomTitle] = useState(''); const [isGenerating, setIsGenerating] = useState(false); const [generatedTitles, setGeneratedTitles] = useState([]); const [generationProgress, setGenerationProgress] = useState(''); const handleTitleSelect = (title: string) => { onTitleSelect(title); setShowModal(false); }; const handleCustomTitleSubmit = () => { if (customTitle.trim() && onCustomTitle) { onCustomTitle(customTitle.trim()); setCustomTitle(''); setShowModal(false); } }; const handleGenerateSEOTitles = async () => { if (!research || !sections.length || isGenerating) { return; } setIsGenerating(true); setGenerationProgress('Analyzing research data and outline structure...'); try { const keywordAnalysis = research.keyword_analysis || {}; const primaryKeywords = keywordAnalysis.primary || []; const secondaryKeywords = keywordAnalysis.secondary || []; const contentAngles = research.suggested_angles || []; const searchIntent = keywordAnalysis.search_intent || 'informational'; // Simulate progress updates setTimeout(() => setGenerationProgress('Extracting keywords and content angles...'), 500); setTimeout(() => setGenerationProgress('Generating SEO-optimized titles with AI...'), 1500); const result = await blogWriterApi.generateSEOTitles({ research, outline: sections, primary_keywords: primaryKeywords, secondary_keywords: secondaryKeywords, content_angles: contentAngles, search_intent: searchIntent, word_count: sections.reduce((sum, s) => sum + (s.target_words || 0), 0) }); setGenerationProgress('Finalizing titles...'); if (result.success && result.titles) { setTimeout(() => { setGeneratedTitles(result.titles); setGenerationProgress(''); if (onTitlesGenerated) { onTitlesGenerated(result.titles); } }, 500); } } catch (error) { console.error('Failed to generate SEO titles:', error); setGenerationProgress(''); alert('Failed to generate SEO titles. Please try again.'); } finally { setTimeout(() => { setIsGenerating(false); }, 1000); } }; const getSectionSummary = () => { return sections.map(section => ({ title: section.heading, wordCount: section.target_words || 0, subheadings: section.subheadings.length, keyPoints: section.key_points.length })); }; const sectionSummary = getSectionSummary(); return ( <> {/* Main Title Display */}

📝 Blog Title

{(selectedTitle || 'No title selected').length > 150 ? (selectedTitle || 'No title selected').substring(0, 150) + '...' : (selectedTitle || 'No title selected')}

{/* Title Selection Modal */} {showModal && (
{/* Modal Header */}

✨ ALwrity Title Suggestions

Choose from research-based content angles, AI-generated titles, or create your own

{/* Generate SEO Titles Button */} {research && sections.length > 0 && (
{isGenerating && (
)} {isGenerating && generationProgress && (

{generationProgress}

)}
)} {/* Title Options */}
{/* Generated SEO Titles */} {generatedTitles.length > 0 && (
🎯

SEO-Optimized Titles

Premium titles optimized for search engines (50-65 characters)

{generatedTitles.length}
{generatedTitles.map((title, index) => ( ))}
)} {/* Research Content Angles */} {researchTitles.length > 0 && (
🔍

Research Content Angles

Titles derived from your research data and content angles

{researchTitles.length}
{researchTitles.map((title, index) => ( ))}
)} {/* AI-Generated Titles */} {aiGeneratedTitles.length > 0 && (
🤖

AI-Generated Titles

Creative titles generated by AI based on your research

{aiGeneratedTitles.length}
{aiGeneratedTitles.map((title, index) => ( ))}
)} {/* Custom Title Input */}
✏️

Custom Title

Create your own unique title

setCustomTitle(e.target.value)} placeholder="Enter your custom title..." style={{ flex: 1, padding: '16px 20px', border: '1px solid #e5e7eb', borderRadius: '12px', fontSize: '15px', transition: 'all 0.2s ease' }} onKeyPress={(e) => e.key === 'Enter' && handleCustomTitleSubmit()} onFocus={(e) => { e.currentTarget.style.borderColor = '#1976d2'; e.currentTarget.style.boxShadow = '0 0 0 3px rgba(25, 118, 210, 0.1)'; }} onBlur={(e) => { e.currentTarget.style.borderColor = '#e5e7eb'; e.currentTarget.style.boxShadow = 'none'; }} />
{/* Section Information */}

📋 Current Outline Summary

Total Sections
{sections.length}
Total Words
{sections.reduce((sum, section) => sum + (section.target_words || 0), 0)}
Subheadings
{sections.reduce((sum, section) => sum + section.subheadings.length, 0)}
{/* Section Details */}
Section Details:
{sectionSummary.map((section, index) => (
{section.title}
{section.wordCount} words {section.subheadings} subheadings {section.keyPoints} key points
))}
{/* Modal Footer */}
)} ); }; export default EnhancedTitleSelector;