/* 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: 'netease' | 'qq' | 'kuwo'; 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; } 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/playlists'); if (response.ok) { const data = await response.json(); setPlaylists(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/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?.(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/playlists/songs', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ playlistId, song: { platform: song.platform, id: song.id, name: song.name, artist: song.artist, album: song.album, pic: song.pic, duration: song.duration || 0, }, }), }); if (response.ok) { onSuccess?.(); onClose(); } else { const data = await response.json(); onError?.(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" />