import { useEffect, useState } from 'react'; import Link from 'next/link'; import { useRouter } from 'next/router'; import Layout from '../components/Layout'; import CardPreview from '../components/designer/CardFrame'; import { gameLabel } from '../lib/designer-games.js'; import { Button } from '../components/ui'; import { useAuth } from '../lib/use-auth'; export default function MyDesigns() { const router = useRouter(); const { user, loading: authLoading } = useAuth(); const [designs, setDesigns] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { if (!authLoading && !user) { router.push('/login'); } }, [authLoading, user, router]); useEffect(() => { if (!user) return undefined; const loadDesigns = async () => { try { const token = localStorage.getItem('auth_token'); const response = await fetch('/api/custom-cards', { headers: token ? { Authorization: `Bearer ${token}` } : {}, }); if (response.ok) { const data = await response.json(); setDesigns(data.designs); } else { setError('Failed to load your designs.'); } } catch { setError('Failed to load your designs.'); } finally { setLoading(false); } }; loadDesigns(); return undefined; }, [user]); const handleDelete = async (id) => { if (!window.confirm('Delete this design? The card will remain in your collection.')) return; try { const token = localStorage.getItem('auth_token'); const response = await fetch(`/api/custom-cards/${id}`, { method: 'DELETE', headers: token ? { Authorization: `Bearer ${token}` } : {}, }); if (response.ok) { setDesigns((prev) => prev.filter((d) => d.id !== id)); } else { setError('Delete failed.'); } } catch { setError('Delete failed.'); } }; if (loading) { return (
); } return (
{/* Header */}

My Designs

{designs.length} custom card{designs.length === 1 ? '' : 's'} · also visible in your collection ·{' '} My Games

{error && (
{error}
)} {/* Designs grid */} {!loading && designs.length === 0 && !error && (

No designs yet

Create your first custom card in the designer.

)}
{designs.map((design) => (
router.push(`/designer?id=${design.id}`)} role="link" tabIndex={0} onKeyDown={(e) => e.key === 'Enter' && router.push(`/designer?id=${design.id}`)} >

{design.name}

{design.card_type || 'No type'} {design.mana_cost ? `· ${design.mana_cost}` : ''}

{design.custom_game_name ? design.custom_game_name : design.game_target && design.game_target !== 'custom' ? gameLabel(design.game_target) : 'Standalone'}

))}
); }