import { useState, useEffect, useCallback } from 'react' import { ttsService, type TTSResponse } from '@/lib/api/services/tts' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' import { Skeleton } from '@/components/ui/skeleton' import { TTSRow } from './TTSRow' import { RefreshCw, Search } from 'lucide-react' export function TTSList() { const [ttsHistory, setTTSHistory] = useState([]) const [isLoading, setIsLoading] = useState(true) const [error, setError] = useState(null) const [searchQuery, setSearchQuery] = useState('') const [sortBy, setSortBy] = useState('created_at') const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('desc') const [limit, setLimit] = useState(50) const fetchTTSHistory = useCallback(async () => { try { setIsLoading(true) setError(null) const data = await ttsService.getTTSHistory({ limit }) setTTSHistory(data.tts) } catch (err) { setError('Failed to load TTS history') console.error('Failed to fetch TTS history:', err) } finally { setIsLoading(false) } }, [limit]) useEffect(() => { fetchTTSHistory() }, [fetchTTSHistory]) // Listen for TTS generation events to refresh the list useEffect(() => { const handleTTSGenerated = () => { fetchTTSHistory() } window.addEventListener('tts-generated', handleTTSGenerated) return () => { window.removeEventListener('tts-generated', handleTTSGenerated) } }, [fetchTTSHistory]) const filteredHistory = ttsHistory.filter((tts) => tts.text.toLowerCase().includes(searchQuery.toLowerCase()) || tts.provider.toLowerCase().includes(searchQuery.toLowerCase()) ) const sortedHistory = [...filteredHistory].sort((a, b) => { let aValue: any = a[sortBy as keyof TTSResponse] let bValue: any = b[sortBy as keyof TTSResponse] // Convert dates to timestamps for comparison if (sortBy === 'created_at') { aValue = new Date(aValue).getTime() bValue = new Date(bValue).getTime() } if (sortOrder === 'asc') { return aValue > bValue ? 1 : -1 } else { return aValue < bValue ? 1 : -1 } }) if (error) { return (
Failed to load TTS history. Please try again.
) } return (
{/* Search and filters */} TTS History
setSearchQuery(e.target.value)} className="pl-9" />
{/* Results */}
{isLoading ? (
{Array.from({ length: 5 }).map((_, i) => (
{/* Badges row */}
{/* Text content */}
{/* Date and metadata */}
{/* Play button */}
))}
) : sortedHistory.length === 0 ? (
{searchQuery ? 'No TTS generations match your search.' : 'No TTS generations yet. Create your first one!'}
) : ( <>
{sortedHistory.length} generation{sortedHistory.length !== 1 ? 's' : ''}
{sortedHistory.map((tts) => ( ))}
)}
) }