import React, { useState, useEffect } from 'react'; import axios from 'axios'; import { Card, Button, Typography, Space, Spin, message, Tooltip as AntTooltip, Collapse, Empty } from 'antd'; import { PlayCircleOutlined, ReloadOutlined, HistoryOutlined, InfoCircleOutlined, CustomerServiceOutlined, CalendarOutlined, DownOutlined } from '@ant-design/icons'; import Tooltip from './Tooltip'; import TrackList from './TrackList'; const { Title, Text, Paragraph } = Typography; const PlaylistsSection = () => { const [loading, setLoading] = useState(true); const [refreshing, setRefreshing] = useState({ sixHour: false, daily: false }); const [playlists, setPlaylists] = useState(null); const [history, setHistory] = useState([]); const [loadingHistory, setLoadingHistory] = useState(false); const [showHistory, setShowHistory] = useState(false); const fetchPlaylists = async () => { try { const response = await axios.get('/api/playlists'); setPlaylists(response.data); } catch (error) { console.error('Failed to fetch playlists:', error); message.error('Failed to load playlist metadata'); } finally { setLoading(false); } }; const fetchHistory = async () => { if (loadingHistory) return; setLoadingHistory(true); try { const response = await axios.get('/api/playlists/history'); setHistory(response.data.history || []); } catch (error) { console.error('Failed to fetch playlist history:', error); message.error('Failed to load playlist history'); } finally { setLoadingHistory(false); } }; useEffect(() => { fetchPlaylists(); }, []); useEffect(() => { if (showHistory && history.length === 0) { fetchHistory(); } }, [showHistory]); const handleRefresh = async (type) => { const isSixHour = type === 'six-hour'; setRefreshing(prev => ({ ...prev, [isSixHour ? 'sixHour' : 'daily']: true })); try { const endpoint = isSixHour ? '/api/playlists/refresh/six-hour' : '/api/playlists/refresh/daily'; await axios.post(endpoint); message.success(`${isSixHour ? '6-Hour' : 'Daily'} playlist refreshed!`); await fetchPlaylists(); if (showHistory) fetchHistory(); } catch (error) { console.error(`Refresh failed for ${type}:`, error); message.error(`Failed to refresh ${type} playlist`); } finally { setRefreshing(prev => ({ ...prev, [isSixHour ? 'sixHour' : 'daily']: false })); } }; if (loading) return
; const historyItems = history.map((item) => ({ key: item.id, label: (
{new Date(item.date).toLocaleDateString(undefined, { month: 'short', day: 'numeric' })} {item.theme} {item.period_label || '6h'}
{item.composition?.length || 0} tracks
), children: (
"{item.reasoning}"
), })); return (
<CustomerServiceOutlined className="mr-2 text-blue-400" /> AI Curated Playlists
{/* 6-Hour Playlist */} Short & Sweet (6h)} extra={
{/* Daily Playlist */} Proof of Commitment (24h)} extra={
{showHistory && (
{loadingHistory ? (
) : history.length > 0 ? ( ) : ( No playlist history available yet} /> )}
)}
); }; export default PlaylistsSection;