/* eslint-disable @typescript-eslint/no-explicit-any */ 'use client'; import { useEffect, useState } from 'react'; interface Song { id: string; name: string; artist: string; album?: string; pic?: string; platform: 'wy' | 'tx' | 'kw' | 'kg' | 'mg'; duration?: number; } interface MusicPlaylist { id: string; username: string; name: string; description?: string; cover?: string; created_at: number; updated_at: number; } interface AddToPlaylistModalProps { song: Song | null; isOpen: boolean; onClose: () => void; onSuccess?: () => void; onError?: (message: string) => void; } function getApiErrorMessage(error: unknown, fallback: string): string { if (typeof error === 'string') return error; if (error && typeof error === 'object' && 'message' in error) { const message = (error as { message?: unknown }).message; if (typeof message === 'string' && message.trim()) return message; } return fallback; } function MusicLoadingIndicator({ text, size = 'md', className = '', }: { text?: string; size?: 'sm' | 'md'; className?: string; }) { const iconSize = size === 'sm' ? 'w-4 h-4' : 'w-5 h-5'; const textSize = size === 'sm' ? 'text-xs' : 'text-sm'; return ( <>
{[0, 1, 2].map((index) => ( ))}
{text ? {text} : null}
); } export default function AddToPlaylistModal({ song, isOpen, onClose, onSuccess, onError, }: AddToPlaylistModalProps) { const [playlists, setPlaylists] = useState([]); const [loading, setLoading] = useState(false); const [showCreateForm, setShowCreateForm] = useState(false); const [newPlaylistName, setNewPlaylistName] = useState(''); const [newPlaylistDescription, setNewPlaylistDescription] = useState(''); const [creating, setCreating] = useState(false); const [addingToPlaylistId, setAddingToPlaylistId] = useState(null); // 正在添加的歌单ID // 加载用户的歌单列表 useEffect(() => { if (isOpen) { loadPlaylists(); } }, [isOpen]); const loadPlaylists = async () => { try { setLoading(true); const response = await fetch('/api/music/v2/playlists'); if (response.ok) { const data = await response.json(); setPlaylists(data.data?.playlists || []); } } catch (error) { console.error('加载歌单失败:', error); } finally { setLoading(false); } }; const handleCreatePlaylist = async () => { if (!newPlaylistName.trim()) { onError?.('请输入歌单名称'); return; } try { setCreating(true); const response = await fetch('/api/music/v2/playlists', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: newPlaylistName.trim(), description: newPlaylistDescription.trim(), }), }); if (response.ok) { setNewPlaylistName(''); setNewPlaylistDescription(''); setShowCreateForm(false); await loadPlaylists(); } else { const data = await response.json(); onError?.(getApiErrorMessage(data.error, '创建歌单失败')); } } catch (error) { console.error('创建歌单失败:', error); onError?.('创建歌单失败'); } finally { setCreating(false); } }; const handleAddToPlaylist = async (playlistId: string) => { if (!song) return; try { setAddingToPlaylistId(playlistId); const response = await fetch(`/api/music/v2/playlists/${playlistId}/songs`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ song: { source: song.platform, songId: song.id, name: song.name, artist: song.artist, album: song.album, cover: song.pic, durationSec: song.duration || 0, }, }), }); if (response.ok) { onSuccess?.(); onClose(); } else { const data = await response.json(); onError?.(getApiErrorMessage(data.error, '添加失败')); } } catch (error) { console.error('添加到歌单失败:', error); onError?.('添加到歌单失败'); } finally { setAddingToPlaylistId(null); } }; if (!isOpen) return null; return (
e.stopPropagation()} > {/* Header */}

添加到歌单

{song && (
{song.name} - {song.artist}
)}
{/* Content */}
{/* Create New Playlist Button */} {!showCreateForm && ( )} {/* Create Form */} {showCreateForm && (
setNewPlaylistName(e.target.value)} className="w-full px-3 py-2 bg-zinc-800 text-white rounded-lg border border-white/10 focus:border-green-500 focus:outline-none mb-2" />