'use client'; import { useState, useEffect } from 'react'; import { ArticleGenerationModal, ArticleGenerationParams } from './ArticleGenerationModal'; import { Button } from '@/components/ui/button'; import { Checkbox } from '@/components/ui/checkbox'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; interface ArticleProps { className?: string; sourceArticle: string; onSourceArticleChange: (text: string) => void; isSourceLocked: boolean; onSourceLockedChange: (locked: boolean) => void; } export function Article({ className = '', sourceArticle, onSourceArticleChange, isSourceLocked, onSourceLockedChange, }: ArticleProps) { const [wordCount, setWordCount] = useState(0); const [isModalOpen, setIsModalOpen] = useState(false); const [isGeneratingArticle, setIsGeneratingArticle] = useState(false); const [isExpandOpen, setIsExpandOpen] = useState(false); const updateWordCount = (text: string) => { const words = text.trim().split(/\s+/).length; setWordCount(text.trim() === '' ? 0 : words); }; useEffect(() => { updateWordCount(sourceArticle); }, [sourceArticle]); const handleSourceArticleChange = (text: string) => { if (!isSourceLocked) { onSourceArticleChange(text); } }; const handleGenerateArticle = async (params: ArticleGenerationParams) => { setIsGeneratingArticle(true); try { // Call the API to generate article const response = await fetch('/api/generate-article', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(params), }); if (response.ok) { const { article } = await response.json(); onSourceArticleChange(article); setIsModalOpen(false); } else { console.error('Failed to generate article'); } } catch (error) { console.error('Error generating article:', error); } finally { setIsGeneratingArticle(false); } }; return ( ๐Ÿ“„ Source Article
{wordCount} words