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, History as HistoryIcon, Waves } from 'lucide-react'; import { Article, PlaybackStatus, PlayerState, VoiceName, AudioSegment, ReaderSettings, VoiceMood, ReadingStats, Bookmark } from './types'; import { MIN_SPEED, MAX_SPEED, SPEED_STEP, SMART_SPEED_MULTIPLIERS } from './constants'; import { extractArticleContent, generateSpeechFromText, generateArticleSummary, analyzeTextComplexity } from './services/geminiService'; import { base64ToUint8Array, createWavBlob } from './services/audioUtils'; import { createTrackedObjectUrl, revokeAllTrackedObjectUrls, revokeMultipleObjectUrls, revokeTrackedObjectUrl } from './services/objectUrlManager'; import { segmentText } from './services/textUtils'; import { getStats, saveBookmark, getBookmarkForUrl, updateStatsOnArticleComplete } from './services/storageService'; import { exportAndDownloadArticle } from './services/audioExportService'; import { QueueItem } from './components/QueueItem'; import { VoiceSelector, VoicePanel } from './components/VoiceSelector'; import { ReaderView } from './components/ReaderView'; import { StatsPanel } from './components/StatsPanel'; import { RSSManager } from './components/RSSManager'; import { BookmarksPanel } from './components/BookmarksPanel'; import { VoiceMoodSelector } from './components/VoiceMoodSelector'; import { FeatureToolbar } from './components/FeatureToolbar'; import { SummaryCard } from './components/SummaryCard'; export default function App() { // -- State -- const [inputUrl, setInputUrl] = useState(''); const [queue, setQueue] = useState([]); const [history, setHistory] = useState([]); const [viewId, setViewId] = useState(null); const [showSettings, setShowSettings] = useState(false); const [activeList, setActiveList] = useState<'queue' | 'history'>('queue'); const [draggingId, setDraggingId] = useState(null); const [playerState, setPlayerState] = useState({ isPlaying: false, playbackRate: 1.0, currentArticleId: null, selectedVoice: VoiceName.Puck, voiceMood: 'neutral', smartSpeedEnabled: false, }); // New feature states const [showStatsPanel, setShowStatsPanel] = useState(false); const [showRSSManager, setShowRSSManager] = useState(false); const [showBookmarksPanel, setShowBookmarksPanel] = useState(false); const [readingStats, setReadingStats] = useState(getStats()); const [isExporting, setIsExporting] = useState(false); const [settings, setSettings] = useState({ isDarkMode: false, fontSize: 'lg', lineHeight: 'relaxed', fontFamily: 'serif', autoScroll: true, readingTone: 'clean', pageWidth: 'standard', zenMode: false }); // -- Refs -- const audioRef = useRef(new Audio()); const processingRef = useRef>(new Set()); const voiceGenerationRef = useRef(0); // Track voice changes to invalidate old requests // -- 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; // Capture the current generation - if voice changes, this will be outdated const generation = voiceGenerationRef.current; processingRef.current.add(uniqueKey); updateSegment(articleId, segmentId, { isLoading: true }); try { const base64Audio = await generateSpeechFromText(text, voice); // Check if voice generation changed while we were generating if (generation !== voiceGenerationRef.current) { console.log(`Discarding segment ${segmentId} - voice changed during generation`); return; // Discard this result, voice has changed } 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) => { // Increment generation counter to invalidate all in-flight requests voiceGenerationRef.current += 1; setPlayerState(prev => ({ ...prev, selectedVoice: newVoice })); // Force flush ALL audio buffers so new voice is applied immediately setQueue(prevQueue => prevQueue.map(article => { return { ...article, segments: article.segments.map(seg => { if (seg.audioUrl) { revokeTrackedObjectUrl(seg.audioUrl); } // Invalidate ALL segments to force regeneration with new voice return { ...seg, audioUrl: undefined, isLoading: false, hasError: false }; }) }; })); }, []); // -- New Feature Handlers -- const handleMoodChange = useCallback((mood: VoiceMood, voice: VoiceName, speed: number) => { setPlayerState(prev => ({ ...prev, voiceMood: mood, selectedVoice: voice, playbackRate: speed })); // Also trigger voice change to regenerate audio handleVoiceChange(voice); }, [handleVoiceChange]); const handleSmartSpeedToggle = useCallback(() => { setPlayerState(prev => ({ ...prev, smartSpeedEnabled: !prev.smartSpeedEnabled })); }, []); const handleExportAudio = useCallback(async () => { const article = getCurrentArticle(); if (!article) return; setIsExporting(true); try { await exportAndDownloadArticle(article); } catch (e) { console.error('Export failed:', e); } finally { setIsExporting(false); } }, []); const handleResumeFromBookmark = useCallback(async (url: string, segmentIndex: number) => { // First enqueue the article await enqueueArticle(url, { autoPlay: false, pinView: true }); // Then set the segment index once loaded setTimeout(() => { setQueue(prev => { const article = prev.find(a => a.url === url); if (article) { return prev.map(a => a.url === url ? { ...a, currentSegmentIndex: segmentIndex } : a ); } return prev; }); }, 1000); }, []); const handleRSSArticleSelect = useCallback((url: string) => { enqueueArticle(url, { autoPlay: true, pinView: true }); }, []); // Save bookmark when pausing const saveCurrentBookmark = useCallback(() => { const article = getCurrentArticle(); if (!article || article.segments.length === 0) return; const progress = Math.round( ((article.currentSegmentIndex + 1) / article.segments.length) * 100 ); const bookmark: Bookmark = { articleId: article.id, url: article.url, title: article.title, segmentIndex: article.currentSegmentIndex, savedAt: Date.now(), progress }; saveBookmark(bookmark); }, []); const enqueueArticle = async (targetUrl: string, options: { autoPlay?: boolean; pinView?: boolean; resumeSegment?: number } = {}) => { const normalizedUrl = targetUrl.trim(); if (!normalizedUrl) return; const existing = queue.find(item => item.url === normalizedUrl); if (existing) { setViewId(existing.id); if (options.autoPlay) playArticle(existing.id); return; } const id = uuidv4(); const newArticle: Article = { id, url: normalizedUrl, title: 'Fetching info...', text: '', segments: [], currentSegmentIndex: 0, status: PlaybackStatus.LOADING_TEXT, addedAt: Date.now() }; setQueue(prev => [...prev, newArticle]); if (!playerState.isPlaying || options.pinView) 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); } // Analyze complexity for Smart Speed const { complexity, wordCount, estimatedReadTime } = analyzeTextComplexity(text); updateArticle(id, { title, text, segments, status: PlaybackStatus.LOADING_AUDIO, complexity, wordCount, estimatedReadTime, isSummaryLoading: true }); // Generate summary in background generateArticleSummary(text, title).then(summary => { updateArticle(id, { summary, isSummaryLoading: false }); }).catch(e => { console.warn('Summary generation failed:', e); updateArticle(id, { isSummaryLoading: false }); }); 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 || options.autoPlay)) { 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" }); } }; const handleAddUrl = async () => { await enqueueArticle(inputUrl, { autoPlay: !playerState.isPlaying, pinView: true }); setInputUrl(''); }; // -- 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 }); // Save bookmark on pause saveCurrentBookmark(); } }, [playerState.currentArticleId, saveCurrentBookmark]); 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 handleReorder = useCallback((sourceId: string, targetId: string) => { if (sourceId === targetId) return; setQueue(prev => { const sourceIndex = prev.findIndex(a => a.id === sourceId); const targetIndex = prev.findIndex(a => a.id === targetId); if (sourceIndex === -1 || targetIndex === -1) return prev; const updated = [...prev]; const [moved] = updated.splice(sourceIndex, 1); updated.splice(targetIndex, 0, moved); return updated; }); }, []); const handlePlayNext = useCallback((articleId: string) => { setQueue(prev => { const sourceIndex = prev.findIndex(a => a.id === articleId); if (sourceIndex === -1) return prev; const updated = [...prev]; const [picked] = updated.splice(sourceIndex, 1); const currentId = playerState.currentArticleId; const currentIndex = currentId ? updated.findIndex(a => a.id === currentId) : -1; const insertIndex = currentIndex === -1 ? 0 : currentIndex + 1; updated.splice(insertIndex, 0, picked); return updated; }); setActiveList('queue'); }, [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(); }; }, []); useEffect(() => { setHistory(prev => { const seen = new Set(prev.map(h => h.id)); const completed = queue.filter(item => item.status === PlaybackStatus.COMPLETED && !seen.has(item.id)); if (completed.length === 0) return prev; return [...prev, ...completed]; }); }, [queue]); const handleSpeedChange = (newSpeed: number) => { // Round to nearest 0.5 to keep clean values const rounded = Math.round(newSpeed * 2) / 2; const speed = Math.max(MIN_SPEED, Math.min(MAX_SPEED, rounded)); 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) => ( ))}
{/* Ambient Tone */}
Ambient mode
{[ { key: 'clean', label: 'Clean' }, { key: 'sepia', label: 'Sepia' }, { key: 'night', label: 'Night Light' } ].map(option => ( ))}
{/* Page Width */}
Page width {settings.pageWidth === 'cozy' ? 'Cozy' : settings.pageWidth === 'wide' ? 'Wide' : 'Comfort'}
{ const options = ['cozy','standard','wide'] as const; setSettings(s => ({...s, pageWidth: options[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" />
{/* Zen Mode */}

Zen mode

Hide chrome for distraction-free reading.

{/* Shortcuts Hint */}

Shortcuts: Space (Play), Arrows (Skip)

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

Paste any article URL to start listening.

{/* Voice Mood Selector */} {/* Voice Panel */} {/* Feature Toolbar */} setShowStatsPanel(true)} onOpenRSS={() => setShowRSSManager(true)} onOpenBookmarks={() => setShowBookmarksPanel(true)} onExportAudio={handleExportAudio} smartSpeedEnabled={playerState.smartSpeedEnabled} onToggleSmartSpeed={handleSmartSpeedToggle} canExport={!!getCurrentArticle()?.segments.some(s => s.audioUrl)} isExporting={isExporting} /> {/* Summary Card for viewing article */} {viewingArticle && (viewingArticle.summary || viewingArticle.isSummaryLoading) && ( viewingArticle && playArticle(viewingArticle.id)} /> )} {/* Queue List */}
{([['queue','Queue'], ['history','History']] as const).map(([key,label]) => ( ))}
{activeList === 'queue' ? `${queue.length} queued` : `${history.length} played`}
{activeList === 'queue' ? (
{queue.length === 0 ? (

No articles queued.

Tap a vibe above to get started.

) : ( queue.map(article => (
setViewId(article.id)} className="cursor-pointer" > playArticle(article.id)} onPause={pausePlayback} onPlayNext={() => handlePlayNext(article.id)} draggable onDragStart={() => setDraggingId(article.id)} onDragOver={(e) => e.preventDefault()} onDrop={(e) => { e.preventDefault(); if (draggingId) handleReorder(draggingId, article.id); setDraggingId(null); }} onDragEnd={() => setDraggingId(null)} 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); }} />
)) )}
) : (
{history.length === 0 ? (

No history yet.

Finished listens will show up here automatically.

) : ( history.map(item => (
{item.title || item.url}

{item.url}

Completed
)) )}
)}
{/* 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
{/* Modal Panels */} {showStatsPanel && ( setShowStatsPanel(false)} /> )} {showRSSManager && ( setShowRSSManager(false)} /> )} {showBookmarksPanel && ( setShowBookmarksPanel(false)} /> )}
); }