import { useState, useEffect } from 'react'; import { useRouter } from 'next/router'; import Layout from '../components/Layout'; import PermissionIndicator from '../components/PermissionIndicator'; import { useAuth } from '../lib/use-auth'; import Link from 'next/link'; import { VOCAB, collectionDisplayName } from '../lib/collection-vocabulary.js'; export default function Dashboard() { const router = useRouter(); const { user, loading: authLoading } = useAuth(); const [collections, setCollections] = useState([]); const [loading, setLoading] = useState(true); // Redirect to login if not authenticated useEffect(() => { if (!authLoading && !user) { router.push('/login'); } }, [authLoading, user, router]); const fetchCollections = async () => { try { const token = localStorage.getItem('auth_token'); const headers = { 'Content-Type': 'application/json', }; if (token) { headers.Authorization = `Bearer ${token}`; } const response = await fetch('/api/collections', { headers }); if (response.ok) { const data = await response.json(); // Fetch thumbnails for each collection const collectionsWithThumbnails = await Promise.all( data.map(async (collection) => { try { const identifier = collection.slug || collection.id; const thumbnailResponse = await fetch(`/api/collections/${identifier}/thumbnails`, { headers }); if (thumbnailResponse.ok) { const thumbnailData = await thumbnailResponse.json(); return { ...collection, thumbnails: thumbnailData.thumbnails }; } return { ...collection, thumbnails: [] }; } catch (error) { console.error(`Error fetching thumbnails for collection ${collection.id}:`, error); return { ...collection, thumbnails: [] }; } }) ); setCollections(collectionsWithThumbnails); } else { console.error('Failed to fetch collections'); setCollections([]); } } catch (error) { console.error('Error fetching collections:', error); setCollections([]); } finally { setLoading(false); } } useEffect(() => { if (user) { // eslint-disable-next-line react-hooks/set-state-in-effect -- load dashboard lists when user is available fetchCollections(); } }, [user]); ; return ( {/* Header */}

{VOCAB.MY_COLLECTION}

Overview of your lists and owned cards

{/* Content */}
{loading ? (
) : (
{/* Stats Cards */}

{collections.length}

Lists

{collections.reduce((total, col) => total + (col.cardCount || 0), 0)}

Total Cards

${collections.reduce((total, col) => total + (col.value || 0), 0).toLocaleString()}

Total Value

{/* Collections Grid */}

Recent Lists

{collections.length === 0 ? (

No Lists Yet

Create your first list to start organizing your cards

) : (
{collections.slice(0, 6).map((collection) => (
{collection.name?.charAt(0)?.toUpperCase() || 'C'}

{collectionDisplayName(collection)}

{collection.cardCount || 0} cards

{collection.description && (

{collection.description}

)}
${(collection.value || 0).toLocaleString()}
))}
)}
{collections.length > 6 && (
)}
)}
); }