import { useState, useEffect } from 'react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Play, Clock, User, TrendingUp } from 'lucide-react'; import { apiService } from '@/services/api'; interface PlayRecord { id: number; played_at: string; user: { id: number; name: string; email: string; } | null; sound: { id: number; name: string; filename: string; type: string; } | null; } interface UserStats { total_plays: number; unique_sounds: number; favorite_sound: { sound: any; play_count: number; } | null; first_play: string | null; last_play: string | null; } interface PopularSound { sound: any; play_count: number; last_played: string | null; } export function ActivityPage() { const [recentPlays, setRecentPlays] = useState([]); const [myStats, setMyStats] = useState(null); const [popularSounds, setPopularSounds] = useState([]); const [loading, setLoading] = useState(true); const [activeTab, setActiveTab] = useState<'recent' | 'mystats' | 'popular'>('recent'); useEffect(() => { fetchData(); }, []); const fetchData = async () => { try { setLoading(true); await Promise.all([ fetchRecentPlays(), fetchMyStats(), fetchPopularSounds(), ]); } catch (err) { console.error('Error fetching activity data:', err); } finally { setLoading(false); } }; const fetchRecentPlays = async () => { try { const response = await apiService.get('/api/soundboard/history?per_page=20'); const data = await response.json(); setRecentPlays(data.plays || []); } catch (err) { console.error('Error fetching recent plays:', err); } }; const fetchMyStats = async () => { try { const response = await apiService.get('/api/soundboard/my-stats'); const data = await response.json(); setMyStats(data); } catch (err) { console.error('Error fetching my stats:', err); } }; const fetchPopularSounds = async () => { try { const response = await apiService.get('/api/soundboard/popular?limit=10'); const data = await response.json(); setPopularSounds(data.popular_sounds || []); } catch (err) { console.error('Error fetching popular sounds:', err); } }; const formatRelativeTime = (dateString: string) => { const date = new Date(dateString); const now = new Date(); const diff = now.getTime() - date.getTime(); const minutes = Math.floor(diff / 60000); const hours = Math.floor(diff / 3600000); const days = Math.floor(diff / 86400000); if (minutes < 1) return 'Just now'; if (minutes < 60) return `${minutes}m ago`; if (hours < 24) return `${hours}h ago`; return `${days}d ago`; }; if (loading) { return (
Loading activity...
); } return (
{/* Tab Navigation */}
{/* Recent Activity Tab */} {activeTab === 'recent' && ( Recent Activity
{recentPlays.map((play) => (
{play.sound?.name || 'Unknown Sound'}
by {play.user?.name || 'Unknown User'}
{formatRelativeTime(play.played_at)}
))} {recentPlays.length === 0 && (
No recent activity found.
)}
)} {/* My Statistics Tab */} {activeTab === 'mystats' && myStats && (
Total Plays
{myStats.total_plays}
Unique Sounds
{myStats.unique_sounds}
{myStats.favorite_sound && ( Favorite Sound
{myStats.favorite_sound.sound.name}
{myStats.favorite_sound.play_count} plays
)}
)} {/* Popular Sounds Tab */} {activeTab === 'popular' && ( Popular Sounds
{popularSounds.map((item, index) => (
{index + 1}
{item.sound.name}
{item.play_count} plays
{item.last_played && (
Last: {formatRelativeTime(item.last_played)}
)}
))} {popularSounds.length === 0 && (
No popular sounds found.
)}
)}
); }