import React, { useState, useRef, useEffect, useCallback } from 'react'; import { v4 as uuidv4 } from 'uuid'; import { Plus, Play, Pause, SkipForward, SkipBack, Volume2, Gauge, Settings, Moon, Sun, Type, Keyboard } from 'lucide-react'; import { Article, PlaybackStatus, PlayerState, VoiceName, AudioSegment, ReaderSettings } from './types'; import { MIN_SPEED, MAX_SPEED, SPEED_STEP } from './constants'; import { extractArticleContent, generateSpeechFromText } from './services/geminiService'; import { base64ToUint8Array, createWavBlob } from './services/audioUtils'; import { segmentText } from './services/textUtils'; import { QueueItem } from './components/QueueItem'; import { VoiceSelector } from './components/VoiceSelector'; import { ReaderView } from './components/ReaderView'; export default function App() { // -- State -- const [inputUrl, setInputUrl] = useState(''); const [queue, setQueue] = useState([]); const [viewId, setViewId] = useState(null); const [showSettings, setShowSettings] = useState(false); const [playerState, setPlayerState] = useState({ isPlaying: false, playbackRate: 1.0, currentArticleId: null, selectedVoice: VoiceName.Puck, }); const [settings, setSettings] = useState({ isDarkMode: false, fontSize: 'lg', lineHeight: 'relaxed', fontFamily: 'serif', autoScroll: true }); // -- Refs -- const audioRef = useRef(new Audio()); const processingRef = useRef>(new Set()); // -- Helpers -- const getCurrentArticle = () => queue.find(a => a.id === playerState.currentArticleId); const getViewingArticle = () => { if (viewId) return queue.find(a => a.id === viewId); if (playerState.currentArticleId) return queue.find(a => a.id === playerState.currentArticleId); if (queue.length > 0) return queue[0]; return null; }; // -- State Updaters -- const updateArticle = (id: string, updates: Partial
) => { setQueue(prev => prev.map(item => item.id === id ? { ...item, ...updates } : item)); }; const updateSegment = (articleId: string, segmentId: string, updates: Partial) => { setQueue(prev => prev.map(article => { if (article.id !== articleId) return article; const newSegments = article.segments.map(seg => seg.id === segmentId ? { ...seg, ...updates } : seg ); return { ...article, segments: newSegments }; })); }; // -- Audio Generation Pipeline -- const processSegmentAudio = useCallback(async (articleId: string, segmentId: string, text: string, voice: VoiceName) => { const uniqueKey = `${articleId}-${segmentId}`; if (processingRef.current.has(uniqueKey)) return; processingRef.current.add(uniqueKey); updateSegment(articleId, segmentId, { isLoading: true }); try { const base64Audio = await generateSpeechFromText(text, voice); const pcmData = base64ToUint8Array(base64Audio); const wavBlob = createWavBlob(pcmData); const audioUrl = URL.createObjectURL(wavBlob); updateSegment(articleId, segmentId, { audioUrl, isLoading: false }); } catch (error) { console.error("Segment generation failed", error); updateSegment(articleId, segmentId, { isLoading: false, hasError: true }); } finally { processingRef.current.delete(uniqueKey); } }, []); const manageBuffer = useCallback(async (article: Article) => { const currentIndex = article.currentSegmentIndex; const segmentsToBuffer = article.segments.slice(currentIndex, currentIndex + 5); for (const seg of segmentsToBuffer) { if (!seg.audioUrl && !seg.isLoading && !seg.hasError) { processSegmentAudio(article.id, seg.id, seg.text, playerState.selectedVoice); } } }, [playerState.selectedVoice, processSegmentAudio]); // -- Handlers -- const handleAddUrl = async () => { if (!inputUrl.trim()) return; const id = uuidv4(); const newArticle: Article = { id, url: inputUrl, title: 'Fetching info...', text: '', segments: [], currentSegmentIndex: 0, status: PlaybackStatus.LOADING_TEXT }; setQueue(prev => [...prev, newArticle]); setInputUrl(''); if (!playerState.isPlaying) setViewId(id); try { const { title, text } = await extractArticleContent(newArticle.url); const segments = segmentText(text); if (title) { const titleSegment = segmentText(title)[0]; if (titleSegment) segments.unshift(titleSegment); } updateArticle(id, { title, text, segments, status: PlaybackStatus.LOADING_AUDIO }); if (segments.length > 0) { const initialLoadCount = Math.min(segments.length, 5); for(let i = 0; i < initialLoadCount; i++) { processSegmentAudio(id, segments[i].id, segments[i].text, playerState.selectedVoice); } updateArticle(id, { status: PlaybackStatus.READY }); setQueue(prev => { const current = prev.find(a => a.id === id); if (current && !playerState.isPlaying) { playArticle(id); } return prev; }); } else { updateArticle(id, { status: PlaybackStatus.ERROR, errorMessage: "No readable text found." }); } } catch (error: any) { updateArticle(id, { status: PlaybackStatus.ERROR, errorMessage: error.message || "Failed to load article" }); } }; // -- Playback Control -- const playArticle = useCallback(async (id: string) => { setPlayerState(prev => ({ ...prev, currentArticleId: id, isPlaying: true })); setViewId(id); updateArticle(id, { status: PlaybackStatus.PLAYING }); }, []); const pausePlayback = useCallback(() => { audioRef.current.pause(); setPlayerState(prev => ({ ...prev, isPlaying: false })); if (playerState.currentArticleId) { updateArticle(playerState.currentArticleId, { status: PlaybackStatus.PAUSED }); } }, [playerState.currentArticleId]); const skipSegment = useCallback((direction: 'next' | 'prev') => { setQueue(prevQueue => { const currentId = playerState.currentArticleId; if (!currentId) return prevQueue; const article = prevQueue.find(a => a.id === currentId); if (!article) return prevQueue; let newIndex = direction === 'next' ? article.currentSegmentIndex + 1 : article.currentSegmentIndex - 1; // Boundary checks if (newIndex < 0) newIndex = 0; if (newIndex >= article.segments.length) { // If skipping past end, just stop for now (or go to next article logic) newIndex = article.segments.length - 1; } return prevQueue.map(a => a.id === currentId ? { ...a, currentSegmentIndex: newIndex } : a); }); }, [playerState.currentArticleId]); const handleSegmentSelect = useCallback((articleId: string, index: number) => { setPlayerState(prev => ({ ...prev, currentArticleId: articleId, isPlaying: true })); updateArticle(articleId, { currentSegmentIndex: index, status: PlaybackStatus.PLAYING }); }, []); // -- Keyboard Shortcuts -- useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { // Ignore shortcuts if typing in input if (document.activeElement instanceof HTMLInputElement || document.activeElement instanceof HTMLTextAreaElement) { return; } switch (e.code) { case 'Space': e.preventDefault(); if (playerState.isPlaying) pausePlayback(); else if (playerState.currentArticleId) playArticle(playerState.currentArticleId); else if (queue.length > 0) playArticle(queue[0].id); break; case 'ArrowRight': skipSegment('next'); break; case 'ArrowLeft': skipSegment('prev'); break; } }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [playerState.isPlaying, playerState.currentArticleId, queue, playArticle, pausePlayback, skipSegment]); // -- Effects -- useEffect(() => { const article = queue.find(a => a.id === playerState.currentArticleId); if (!article || !playerState.isPlaying) return; const currentSegment = article.segments[article.currentSegmentIndex]; if (!currentSegment) { updateArticle(article.id, { status: PlaybackStatus.COMPLETED }); setPlayerState(prev => ({ ...prev, isPlaying: false })); return; } if (currentSegment.audioUrl) { const audioEl = audioRef.current; const currentSrc = audioEl.getAttribute('data-current-src'); if (currentSrc !== currentSegment.audioUrl) { audioEl.src = currentSegment.audioUrl; audioEl.setAttribute('data-current-src', currentSegment.audioUrl); audioEl.playbackRate = playerState.playbackRate; audioEl.play().catch(e => console.warn("Playback interrupted", e)); } else if (audioEl.paused) { audioEl.play().catch(e => console.warn("Resume failed", e)); } } else { if (!currentSegment.isLoading && !currentSegment.hasError) { processSegmentAudio(article.id, currentSegment.id, currentSegment.text, playerState.selectedVoice); } } manageBuffer(article); }, [queue, playerState.currentArticleId, playerState.isPlaying, playerState.playbackRate, playerState.selectedVoice, manageBuffer, processSegmentAudio]); useEffect(() => { const audio = audioRef.current; const handleEnded = () => { const currentId = playerState.currentArticleId; setQueue(prevQueue => { const article = prevQueue.find(a => a.id === currentId); if (!article) return prevQueue; const nextIndex = article.currentSegmentIndex + 1; if (nextIndex < article.segments.length) { return prevQueue.map(a => a.id === currentId ? { ...a, currentSegmentIndex: nextIndex } : a); } else { const artIndex = prevQueue.findIndex(a => a.id === currentId); if (artIndex !== -1 && artIndex < prevQueue.length - 1) { setTimeout(() => playArticle(prevQueue[artIndex + 1].id), 100); return prevQueue.map(a => a.id === currentId ? { ...a, status: PlaybackStatus.COMPLETED } : a); } else { setPlayerState(ps => ({ ...ps, isPlaying: false })); return prevQueue.map(a => a.id === currentId ? { ...a, status: PlaybackStatus.COMPLETED } : a); } } }); }; audio.addEventListener('ended', handleEnded); return () => audio.removeEventListener('ended', handleEnded); }, [playerState.currentArticleId, playArticle]); const handleSpeedChange = (newSpeed: number) => { const speed = Math.max(MIN_SPEED, Math.min(MAX_SPEED, newSpeed)); setPlayerState(prev => ({ ...prev, playbackRate: speed })); if (audioRef.current) { audioRef.current.playbackRate = speed; } }; // -- Render -- const currentArticle = getCurrentArticle(); const viewingArticle = getViewingArticle(); return (
{/* Header */}

NewsCaster AI

setPlayerState(prev => ({ ...prev, selectedVoice: v }))} disabled={playerState.isPlaying} />
{/* Settings Menu */} {showSettings && (

Reader Preferences

{/* Theme */}
Theme
{/* Font Size */}
Text Size
{ const sizes = ['sm','base','lg','xl','2xl'] as const; setSettings(s => ({...s, fontSize: sizes[parseInt(e.target.value)]})); }} className="w-full h-2 bg-slate-200 dark:bg-slate-700 rounded-lg appearance-none cursor-pointer accent-blue-600" />
Aa Aa
{/* Font Family */}
Font Family
{['serif', 'sans', 'mono'].map((font) => ( ))}
{/* Shortcuts Hint */}

Shortcuts: Space (Play), Arrows (Skip)

)}
{/* Main Content */}
{/* Left Column: Controls & Queue */}
{/* Input */}
setInputUrl(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && handleAddUrl()} />
{/* Queue List */}

Up Next

{queue.length} articles
{queue.length === 0 ? (

No articles queued.

) : ( queue.map(article => (
setViewId(article.id)} className="cursor-pointer"> playArticle(article.id)} onPause={pausePlayback} onRemove={() => { if (playerState.currentArticleId === article.id) { pausePlayback(); setPlayerState(prev => ({ ...prev, currentArticleId: null })); } setQueue(prev => prev.filter(a => a.id !== article.id)); if (viewId === article.id) setViewId(null); }} />
)) )}
{/* Right Column: Reader View */}
setSettings(s => ({...s, autoScroll: !s.autoScroll}))} onSegmentSelect={(index) => viewingArticle && handleSegmentSelect(viewingArticle.id, index)} />
{viewingArticle && (

Article Reader

setSettings(s => ({...s, autoScroll: !s.autoScroll}))} onSegmentSelect={(index) => viewingArticle && handleSegmentSelect(viewingArticle.id, index)} />
)}
{/* Player Bar */}
{currentArticle ? (

{currentArticle.title}

{/* Progress Bar */}

Playing segment {currentArticle.currentSegmentIndex + 1} of {currentArticle.segments.length}

) : (
Ready to play
)}
{playerState.playbackRate.toFixed(1)}x
); }