'use client'; import { useState, useEffect } from 'react'; interface Song { id: number; title: string; artist: string; filename: string; createdAt: string; } type SortField = 'title' | 'artist'; type SortDirection = 'asc' | 'desc'; export default function AdminPage() { const [password, setPassword] = useState(''); const [isAuthenticated, setIsAuthenticated] = useState(false); const [file, setFile] = useState(null); const [title, setTitle] = useState(''); const [artist, setArtist] = useState(''); const [message, setMessage] = useState(''); const [songs, setSongs] = useState([]); // Edit state const [editingId, setEditingId] = useState(null); const [editTitle, setEditTitle] = useState(''); const [editArtist, setEditArtist] = useState(''); // Sort state const [sortField, setSortField] = useState('artist'); const [sortDirection, setSortDirection] = useState('asc'); const handleLogin = async () => { const res = await fetch('/api/admin/login', { method: 'POST', body: JSON.stringify({ password }), }); if (res.ok) { setIsAuthenticated(true); fetchSongs(); } else { alert('Wrong password'); } }; const fetchSongs = async () => { const res = await fetch('/api/songs'); if (res.ok) { const data = await res.json(); setSongs(data); } }; const handleUpload = async (e: React.FormEvent) => { e.preventDefault(); if (!file) return; const formData = new FormData(); formData.append('file', file); if (title) formData.append('title', title); if (artist) formData.append('artist', artist); setMessage('Uploading...'); const res = await fetch('/api/songs', { method: 'POST', body: formData, }); if (res.ok) { setMessage('Song uploaded successfully!'); setTitle(''); setArtist(''); setFile(null); fetchSongs(); } else { setMessage('Upload failed.'); } }; const startEditing = (song: Song) => { setEditingId(song.id); setEditTitle(song.title); setEditArtist(song.artist); }; const cancelEditing = () => { setEditingId(null); setEditTitle(''); setEditArtist(''); }; const saveEditing = async (id: number) => { const res = await fetch('/api/songs', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id, title: editTitle, artist: editArtist }), }); if (res.ok) { setEditingId(null); fetchSongs(); } else { alert('Failed to update song'); } }; const handleSort = (field: SortField) => { if (sortField === field) { setSortDirection(sortDirection === 'asc' ? 'desc' : 'asc'); } else { setSortField(field); setSortDirection('asc'); } }; const sortedSongs = [...songs].sort((a, b) => { const valA = a[sortField].toLowerCase(); const valB = b[sortField].toLowerCase(); if (valA < valB) return sortDirection === 'asc' ? -1 : 1; if (valA > valB) return sortDirection === 'asc' ? 1 : -1; return 0; }); if (!isAuthenticated) { return (

Admin Login

setPassword(e.target.value)} className="form-input" style={{ marginBottom: '1rem', maxWidth: '300px' }} placeholder="Password" />
); } return (

Admin Dashboard

Upload New Song

setFile(e.target.files?.[0] || null)} className="form-input" required />
setTitle(e.target.value)} className="form-input" />
setArtist(e.target.value)} className="form-input" />
{message &&

{message}

}

Song Library

{sortedSongs.map(song => ( {editingId === song.id ? ( <> ) : ( <> )} ))} {songs.length === 0 && ( )}
ID handleSort('title')} > Title {sortField === 'title' && (sortDirection === 'asc' ? '↑' : '↓')} handleSort('artist')} > Artist {sortField === 'artist' && (sortDirection === 'asc' ? '↑' : '↓')} Filename Actions
{song.id} setEditTitle(e.target.value)} className="form-input" style={{ padding: '0.25rem' }} /> setEditArtist(e.target.value)} className="form-input" style={{ padding: '0.25rem' }} /> {song.filename}
{song.title} {song.artist} {song.filename}
No songs uploaded yet.
); }