'use client'; import { useState, useEffect } from 'react'; import Image from 'next/image'; import Link from 'next/link'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Separator } from '@/components/ui/separator'; import { Button } from '@/components/ui/button'; import { Tabs, TabsContent } from '@/components/ui/tabs'; import { useAudioPlayer } from '@/app/components/AudioPlayerContext'; import { getNavidromeAPI } from '@/lib/navidrome'; import { Play, Plus, User, Disc, History, Trash2 } from 'lucide-react'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from '@/components/ui/alert-dialog'; export default function HistoryPage() { const { playedTracks, clearHistory, playTrack, addToQueue, currentTrack } = useAudioPlayer(); const [groupedHistory, setGroupedHistory] = useState<{ [date: string]: typeof playedTracks }>({}); const api = getNavidromeAPI(); useEffect(() => { // Group tracks by date const grouped = playedTracks.reduce((acc, track, index) => { // Since we don't have timestamps, we'll group by position in array // More recent tracks will be at the end of the array const now = new Date(); const daysAgo = Math.floor(index / 10); // Roughly group every 10 tracks as a different day const date = new Date(now.getTime() - (daysAgo * 24 * 60 * 60 * 1000)); const dateKey = date.toLocaleDateString(); if (!acc[dateKey]) { acc[dateKey] = []; } acc[dateKey].unshift(track); // Add to beginning to show most recent first return acc; }, {} as { [date: string]: typeof playedTracks }); setGroupedHistory(grouped); }, [playedTracks]); const handlePlayClick = (track: typeof playedTracks[0]) => { if (!api) { console.error('Navidrome API not available'); return; } playTrack(track); }; const handleAddToQueue = (track: typeof playedTracks[0]) => { if (!api) { console.error('Navidrome API not available'); return; } addToQueue(track); }; const isCurrentlyPlaying = (track: typeof playedTracks[0]): boolean => { return currentTrack?.id === track.id; }; const formatDuration = (duration: number): string => { const minutes = Math.floor(duration / 60); const seconds = duration % 60; return `${minutes}:${seconds.toString().padStart(2, '0')}`; }; const totalTracks = playedTracks.length; const uniqueTracks = new Set(playedTracks.map(track => track.id)).size; return (

Listening History

{totalTracks} total plays • {uniqueTracks} unique tracks

Clear Listening History This will permanently delete your entire listening history. This action cannot be undone. Cancel Clear History
{playedTracks.length === 0 ? (

No listening history yet

Start playing music to build your listening history

) : (
{Object.entries(groupedHistory) .sort(([a], [b]) => new Date(b).getTime() - new Date(a).getTime()) .map(([date, tracks]) => (

{date}

{tracks.map((track, index) => (
handlePlayClick(track)} > {/* Play Indicator */}
{isCurrentlyPlaying(track) ? (
) : ( )}
{/* Album Art */}
{track.album}
{/* Song Info */}

{track.name}

e.stopPropagation()} > {track.artist}
{track.album && (
e.stopPropagation()} > {track.album}
)}
{/* Duration */}
{formatDuration(track.duration)}
{/* Actions */}
))}
))}
)}
); }