Files
news-reader-actions-test/App.tsx
Anthony 78f1e0e93c feat: Segment article text for improved playback
Splits article content into smaller audio segments. This allows for more granular control over playback, faster processing, and improved user experience by enabling auto-scrolling to the currently read segment.

Updates `types.ts` to include `AudioSegment` interface and modify `Article` to hold `segments`, `currentSegmentIndex`, and `audioUrl` per segment.

Introduces `segmentText` utility in `services/textUtils.ts` for robust text segmentation logic.

Modifies `App.tsx` to utilize the new segmentation approach for fetching and processing audio.

Enhances `components/ReaderView.tsx` to display and auto-scroll through segmented text, highlighting the current segment during playback.
2025-11-19 19:57:43 +08:00

475 lines
20 KiB
TypeScript

import React, { useState, useRef, useEffect, useCallback } from 'react';
import { v4 as uuidv4 } from 'uuid';
import { Plus, Play, Pause, SkipForward, SkipBack, Volume2, Gauge, Layout } from 'lucide-react';
import { Article, PlaybackStatus, PlayerState, VoiceName, AudioSegment } from './types';
import { AVAILABLE_VOICES, 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<Article[]>([]);
// Selected article for viewing text (separate from playing)
const [viewId, setViewId] = useState<string | null>(null);
const [playerState, setPlayerState] = useState<PlayerState>({
isPlaying: false,
playbackRate: 1.0,
currentArticleId: null,
selectedVoice: VoiceName.Puck,
});
// -- Refs --
const audioRef = useRef<HTMLAudioElement>(new Audio());
// Track active processing to prevent duplicate fetch calls
const processingRef = useRef<Set<string>>(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<Article>) => {
setQueue(prev => prev.map(item => item.id === id ? { ...item, ...updates } : item));
};
const updateSegment = (articleId: string, segmentId: string, updates: Partial<AudioSegment>) => {
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 --
/**
* Fetches audio for a specific segment.
*/
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);
}
}, []);
/**
* Manages the buffer. ensure current segment + next 2 are ready.
*/
const manageBuffer = useCallback(async (article: Article) => {
const currentIndex = article.currentSegmentIndex;
const segmentsToBuffer = article.segments.slice(currentIndex, currentIndex + 3);
for (const seg of segmentsToBuffer) {
if (!seg.audioUrl && !seg.isLoading && !seg.hasError) {
// No await here - we want them to fire in parallel/background
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);
// 1. Split text into segments immediately
const segments = segmentText(text);
updateArticle(id, {
title,
text,
segments,
status: PlaybackStatus.LOADING_AUDIO
});
// 2. Trigger audio for the first segment immediately
if (segments.length > 0) {
// We manually call the processor for the first one to ensure fast start
const firstSeg = segments[0];
await processSegmentAudio(id, firstSeg.id, firstSeg.text, playerState.selectedVoice);
// Once first segment is ready, we are effectively ready to play
updateArticle(id, { status: PlaybackStatus.READY });
// If nothing else is playing, auto-play this
setQueue(prev => {
const current = prev.find(a => a.id === id);
if (current && current.segments[0].audioUrl && !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 });
// The actual audio switching is handled by the useEffect monitoring currentArticleId + currentSegmentIndex
}, []);
const pausePlayback = useCallback(() => {
audioRef.current.pause();
setPlayerState(prev => ({ ...prev, isPlaying: false }));
if (playerState.currentArticleId) {
updateArticle(playerState.currentArticleId, { status: PlaybackStatus.PAUSED });
}
}, [playerState.currentArticleId]);
// -- Effects --
// 1. Audio Player Loop
// This effect reacts to changes in article/segment index and loads the correct audio source
useEffect(() => {
const article = queue.find(a => a.id === playerState.currentArticleId);
if (!article || !playerState.isPlaying) return;
const currentSegment = article.segments[article.currentSegmentIndex];
// If finished all segments
if (!currentSegment) {
updateArticle(article.id, { status: PlaybackStatus.COMPLETED });
setPlayerState(prev => ({ ...prev, isPlaying: false }));
return;
}
// Check if audio is ready
if (currentSegment.audioUrl) {
// Only switch src if we aren't already playing it
// We use a custom attribute on the audio element to track current url to avoid state race conditions
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 {
// Buffer stalled?
// If it's loading, show loading state. If not loading and no url, trigger load.
if (!currentSegment.isLoading && !currentSegment.hasError) {
processSegmentAudio(article.id, currentSegment.id, currentSegment.text, playerState.selectedVoice);
}
}
// Always try to buffer ahead
manageBuffer(article);
}, [queue, playerState.currentArticleId, playerState.isPlaying, playerState.playbackRate, playerState.selectedVoice, manageBuffer, processSegmentAudio]);
// 2. Handle 'Ended' event to advance segment
useEffect(() => {
const audio = audioRef.current;
const handleEnded = () => {
const currentId = playerState.currentArticleId;
// We need to get the *latest* queue state to find the current index
// Since we can't easily access 'queue' inside this event listener without recreating the listener constantly,
// we rely on the state setters functional update or a ref.
// However, for simplicity in this specific React structure, let's use the state update pattern.
setQueue(prevQueue => {
const article = prevQueue.find(a => a.id === currentId);
if (!article) return prevQueue;
const nextIndex = article.currentSegmentIndex + 1;
// If we have a next segment, advance index
if (nextIndex < article.segments.length) {
return prevQueue.map(a => a.id === currentId ? { ...a, currentSegmentIndex: nextIndex } : a);
} else {
// Article finished
// Try to play next article in queue
const artIndex = prevQueue.findIndex(a => a.id === currentId);
if (artIndex !== -1 && artIndex < prevQueue.length - 1) {
// Queue next article
setTimeout(() => playArticle(prevQueue[artIndex + 1].id), 100); // Small delay to let state settle
return prevQueue.map(a => a.id === currentId ? { ...a, status: PlaybackStatus.COMPLETED } : a);
} else {
// End of queue
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]); // removed 'queue' from dependency to avoid re-attaching listener on every segment update
// 3. Handle Speed Change
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 (
<div className="min-h-screen flex flex-col bg-slate-50 pb-32">
{/* Header */}
<header className="bg-white border-b border-slate-200 px-6 py-4 sticky top-0 z-20 shadow-sm">
<div className="max-w-7xl mx-auto flex items-center justify-between">
<div className="flex items-center gap-2">
<div className="bg-blue-600 text-white p-2 rounded-lg">
<Volume2 className="w-6 h-6" />
</div>
<h1 className="text-xl font-bold text-slate-900 tracking-tight hidden sm:block">NewsCaster AI</h1>
</div>
<VoiceSelector
selectedVoice={playerState.selectedVoice}
onVoiceChange={(v) => setPlayerState(prev => ({ ...prev, selectedVoice: v }))}
disabled={playerState.isPlaying}
/>
</div>
</header>
{/* Main Content */}
<main className="flex-grow px-4 py-6 max-w-7xl mx-auto w-full grid grid-cols-1 lg:grid-cols-12 gap-8">
{/* Left Column: Controls & Queue */}
<div className="lg:col-span-5 space-y-6">
{/* Input */}
<div className="bg-white p-1 rounded-2xl shadow-sm border border-slate-200 flex gap-2 items-center pl-4">
<input
type="url"
placeholder="Paste article URL here..."
className="flex-grow py-3 outline-none text-slate-700 bg-transparent placeholder:text-slate-400 min-w-0"
value={inputUrl}
onChange={(e) => setInputUrl(e.target.value)}
onKeyDown={(e) => e.key === 'Enter' && handleAddUrl()}
/>
<button
onClick={handleAddUrl}
disabled={!inputUrl.trim()}
className="bg-slate-900 hover:bg-slate-800 disabled:bg-slate-300 text-white px-4 sm:px-6 py-3 rounded-xl font-medium transition-all flex items-center gap-2 flex-shrink-0"
>
<Plus className="w-4 h-4" />
<span className="hidden sm:inline">Queue</span>
</button>
</div>
{/* Queue List */}
<div className="space-y-4">
<div className="flex items-center justify-between px-1">
<h2 className="text-sm font-semibold text-slate-500 uppercase tracking-wider">Up Next</h2>
<span className="text-xs bg-slate-100 text-slate-600 px-2 py-1 rounded-full">{queue.length} articles</span>
</div>
<div className="space-y-3">
{queue.length === 0 ? (
<div className="text-center py-12 border-2 border-dashed border-slate-200 rounded-2xl text-slate-400 bg-white">
<p>No articles queued.</p>
</div>
) : (
queue.map(article => (
<div key={article.id} onClick={() => setViewId(article.id)} className="cursor-pointer">
<QueueItem
article={article}
isActive={article.id === playerState.currentArticleId}
isPlaying={playerState.isPlaying}
onPlay={() => 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);
}}
/>
</div>
))
)}
</div>
</div>
</div>
{/* Right Column: Reader View */}
<div className="lg:col-span-7 h-full hidden lg:block">
<ReaderView article={viewingArticle} />
</div>
<div className="lg:hidden block">
{viewingArticle && (
<div className="mt-8">
<h3 className="text-sm font-semibold text-slate-500 uppercase tracking-wider mb-2">Article Reader</h3>
<div className="h-[500px]">
<ReaderView article={viewingArticle} />
</div>
</div>
)}
</div>
</main>
{/* Player Bar */}
<div className="fixed bottom-0 left-0 right-0 bg-white/90 backdrop-blur-lg border-t border-slate-200 p-4 pb-6 shadow-[0_-4px_20px_rgba(0,0,0,0.05)] z-30">
<div className="max-w-7xl mx-auto flex flex-col sm:flex-row items-center gap-4 sm:gap-8">
<div className="flex-grow w-full sm:w-auto min-w-0 text-center sm:text-left">
{currentArticle ? (
<div>
<h4 className="font-bold text-slate-900 truncate">{currentArticle.title}</h4>
{/* Progress Bar for current segment */}
<div className="w-full h-1 bg-slate-200 rounded-full mt-2 overflow-hidden">
<div
className="h-full bg-blue-500 transition-all duration-300"
style={{ width: `${((currentArticle.currentSegmentIndex + 1) / Math.max(1, currentArticle.segments.length)) * 100}%`}}
/>
</div>
<p className="text-xs text-slate-500 truncate mt-1">
Playing segment {currentArticle.currentSegmentIndex + 1} of {currentArticle.segments.length}
</p>
</div>
) : (
<div className="text-slate-400 text-sm font-medium">Ready to play</div>
)}
</div>
<div className="flex items-center gap-6">
<div className="hidden sm:flex items-center gap-2 group relative">
<Gauge className="w-4 h-4 text-slate-400" />
<div className="flex items-center gap-2 bg-slate-100 rounded-lg p-1">
<button
className="w-6 h-6 flex items-center justify-center hover:bg-white rounded text-xs font-bold text-slate-600 transition-colors"
onClick={() => handleSpeedChange(playerState.playbackRate - SPEED_STEP)}
>-</button>
<span className="text-xs font-mono w-8 text-center font-bold text-blue-600">{playerState.playbackRate.toFixed(1)}x</span>
<button
className="w-6 h-6 flex items-center justify-center hover:bg-white rounded text-xs font-bold text-slate-600 transition-colors"
onClick={() => handleSpeedChange(playerState.playbackRate + SPEED_STEP)}
>+</button>
</div>
</div>
<div className="flex items-center gap-4">
<button
className="p-2 text-slate-400 hover:text-slate-600 transition-colors"
onClick={() => {
if (currentArticle && currentArticle.currentSegmentIndex > 0) {
// Go back one segment
setQueue(prev => prev.map(a => a.id === currentArticle.id ? { ...a, currentSegmentIndex: a.currentSegmentIndex - 1 } : a));
} else {
// Prev article
const idx = queue.findIndex(a => a.id === playerState.currentArticleId);
if (idx > 0) playArticle(queue[idx - 1].id);
}
}}
>
<SkipBack className="w-5 h-5" />
</button>
<button
className="w-12 h-12 rounded-full bg-slate-900 hover:bg-slate-800 text-white flex items-center justify-center shadow-lg hover:shadow-xl hover:scale-105 transition-all active:scale-95"
onClick={() => {
if (playerState.isPlaying) pausePlayback();
else if (playerState.currentArticleId) playArticle(playerState.currentArticleId);
else if (queue.length > 0) playArticle(queue[0].id);
}}
disabled={queue.length === 0}
>
{playerState.isPlaying ? <Pause className="w-5 h-5 fill-current" /> : <Play className="w-5 h-5 fill-current ml-1" />}
</button>
<button
className="p-2 text-slate-400 hover:text-slate-600 transition-colors"
onClick={() => {
if (currentArticle && currentArticle.currentSegmentIndex < currentArticle.segments.length - 1) {
// Next segment
setQueue(prev => prev.map(a => a.id === currentArticle.id ? { ...a, currentSegmentIndex: a.currentSegmentIndex + 1 } : a));
} else {
// Next article
const idx = queue.findIndex(a => a.id === playerState.currentArticleId);
if (idx !== -1 && idx < queue.length - 1) playArticle(queue[idx + 1].id);
}
}}
>
<SkipForward className="w-5 h-5" />
</button>
</div>
</div>
</div>
</div>
</div>
);
}