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, Keyboard, Loader2 } 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 { createTrackedObjectUrl, revokeAllTrackedObjectUrls, revokeMultipleObjectUrls, revokeTrackedObjectUrl } from './services/objectUrlManager'; 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; }; const cleanupArticleAudio = (article?: Article) => { if (!article) return; revokeMultipleObjectUrls(article.segments.map(seg => seg.audioUrl)); }; // -- 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 ? (() => { if ('audioUrl' in updates && seg.audioUrl && updates.audioUrl !== seg.audioUrl) { revokeTrackedObjectUrl(seg.audioUrl); } return { ...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 = createTrackedObjectUrl(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) { // Only process if we don't have audio URL, and it's not currently loading. // This handles cases where we cleared the URL to force regeneration. if (!seg.audioUrl && !seg.isLoading && !seg.hasError) { processSegmentAudio(article.id, seg.id, seg.text, playerState.selectedVoice); } } }, [playerState.selectedVoice, processSegmentAudio]); // -- Handlers -- const handleVoiceChange = useCallback((newVoice: VoiceName) => { setPlayerState(prev => ({ ...prev, selectedVoice: newVoice })); // Force flush future buffer so new voice is applied immediately setQueue(prevQueue => prevQueue.map(article => { // If this is the currently active article if (article.id === playerState.currentArticleId) { return { ...article, segments: article.segments.map((seg, idx) => { // Keep the current segment (and past ones) to avoid cutting off mid-speech abruptly if (idx <= article.currentSegmentIndex) { return seg; } revokeTrackedObjectUrl(seg.audioUrl); // Invalidate all future segments return { ...seg, audioUrl: undefined, isLoading: false, hasError: false }; }) }; } // For inactive articles, invalidate everything return { ...article, segments: article.segments.map(seg => { revokeTrackedObjectUrl(seg.audioUrl); return { ...seg, audioUrl: undefined, isLoading: false, hasError: false }; }) }; })); }, [playerState.currentArticleId]); 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, don't overflow newIndex = article.segments.length - 1; } // If we are skipping, ensure we update state immediately 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) => { 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]); // -- Main Playback Effect -- useEffect(() => { const article = queue.find(a => a.id === playerState.currentArticleId); const audioEl = audioRef.current; // 1. Stop if paused or no article if (!article || !playerState.isPlaying) { if (!audioEl.paused) audioEl.pause(); return; } const currentSegment = article.segments[article.currentSegmentIndex]; // 2. Handle Article Completion if (!currentSegment) { updateArticle(article.id, { status: PlaybackStatus.COMPLETED }); setPlayerState(prev => ({ ...prev, isPlaying: false })); return; } // 3. Auto-Skip on Error if (currentSegment.hasError) { console.warn(`Segment ${currentSegment.id} failed. Skipping.`); skipSegment('next'); return; } // 4. Playback Logic if (currentSegment.audioUrl) { const currentSrc = audioEl.src; // Only switch source if it's actually different to avoid reloading/interrupting if (currentSrc !== currentSegment.audioUrl) { audioEl.src = currentSegment.audioUrl; audioEl.playbackRate = playerState.playbackRate; const playPromise = audioEl.play(); if (playPromise !== undefined) { playPromise.catch(error => { // AbortError is common when skipping fast, ignore it. if (error.name !== 'AbortError') { console.warn("Playback failed:", error); } }); } } else if (audioEl.paused) { // If src matches but paused (and we want to play), resume. audioEl.play().catch(e => console.warn("Resume failed", e)); } } else { // 5. Buffering State (Audio not ready) // Ensure generation is active if (!currentSegment.isLoading && !currentSegment.hasError) { processSegmentAudio(article.id, currentSegment.id, currentSegment.text, playerState.selectedVoice); } // We do not pause here explicitly; leaving the previous audio ended (paused) effectively means silence/buffering. // The UI will show buffering state based on !currentSegment.audioUrl } // 6. Lookahead Buffer manageBuffer(article); }, [queue, playerState.currentArticleId, playerState.isPlaying, playerState.playbackRate, playerState.selectedVoice, manageBuffer, processSegmentAudio, skipSegment]); // -- Audio Event Listeners -- useEffect(() => { const audio = audioRef.current; const handleEnded = () => { const currentId = playerState.currentArticleId; // We use functional update to get latest queue state setQueue(prevQueue => { const article = prevQueue.find(a => a.id === currentId); if (!article) return prevQueue; const nextIndex = article.currentSegmentIndex + 1; if (nextIndex < article.segments.length) { // Advance to next segment return prevQueue.map(a => a.id === currentId ? { ...a, currentSegmentIndex: nextIndex } : a); } else { // Article finished const artIndex = prevQueue.findIndex(a => a.id === currentId); // Auto-play next article if available if (artIndex !== -1 && artIndex < prevQueue.length - 1) { setTimeout(() => playArticle(prevQueue[artIndex + 1].id), 500); 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]); useEffect(() => { return () => { audioRef.current.pause(); audioRef.current.src = ''; revokeAllTrackedObjectUrls(); }; }, []); 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; } }; // -- Derived UI State -- const currentArticle = getCurrentArticle(); const viewingArticle = getViewingArticle(); const isBuffering = playerState.isPlaying && currentArticle && (!currentArticle.segments[currentArticle.currentSegmentIndex]?.audioUrl); return (
{/* Header */}

NewsCaster AI

{/* 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, isPlaying: false })); audioRef.current.src = ''; } setQueue(prev => { const target = prev.find(a => a.id === article.id); cleanupArticleAudio(target); return prev.filter(a => a.id !== article.id); }); setViewId(prev => prev === article.id ? null : prev); }} />
)) )}
{/* 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} {isBuffering && (Buffering...)}

{/* Progress Bar */}

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

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