import { useEffect, useState } from 'react'; import Link from 'next/link'; import { useRouter } from 'next/router'; import Layout from '../../components/Layout'; import { Button, Input } from '../../components/ui'; import { useAuth } from '../../lib/use-auth'; export default function MyGames() { const router = useRouter(); const { user, loading: authLoading } = useAuth(); const [games, setGames] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [name, setName] = useState(''); const [description, setDescription] = useState(''); const [creating, setCreating] = useState(false); useEffect(() => { if (!authLoading && !user) { router.push('/login'); } }, [authLoading, user, router]); useEffect(() => { if (!user) return undefined; const loadGames = async () => { try { const token = localStorage.getItem('auth_token'); const response = await fetch('/api/custom-games', { headers: token ? { Authorization: `Bearer ${token}` } : {}, }); if (response.ok) { const data = await response.json(); setGames(data.games); } else { setError('Failed to load your games.'); } } catch { setError('Failed to load your games.'); } finally { setLoading(false); } }; loadGames(); return undefined; }, [user]); const handleCreate = async (e) => { e.preventDefault(); if (!name.trim()) return; setCreating(true); setError(null); try { const token = localStorage.getItem('auth_token'); const response = await fetch('/api/custom-games', { method: 'POST', headers: { 'Content-Type': 'application/json', ...(token ? { Authorization: `Bearer ${token}` } : {}) }, body: JSON.stringify({ name, description }), }); const data = await response.json(); if (response.ok) { setGames((prev) => [...prev, data.game].sort((a, b) => a.name.localeCompare(b.name)) ); setName(''); setDescription(''); } else { setError(data.error || 'Could not create the game.'); } } catch { setError('Could not create the game.'); } finally { setCreating(false); } }; if (loading) { return (
); } return (

My Games

Custom game systems you've created — private to you.

{error && (
{error}
)} {/* Create form */}

New Game

setName(e.target.value)} placeholder="Game name (e.g. Aetherfall)" maxLength={100} required /> setDescription(e.target.value)} placeholder="Short description (optional)" maxLength={300} />
{/* Games grid */} {games.length === 0 ? (

No custom games yet

Create your first game above, then target designs toward it in the{' '} Card Designer.

) : (
{games.map((game) => (
router.push(`/games/${game.id}`)} role="link" tabIndex={0} onKeyDown={(e) => e.key === 'Enter' && router.push(`/games/${game.id}`)} >

{game.name}

{game.description || 'No description'}

{game.card_count} card{game.card_count === 1 ? '' : 's'} →
))}
)}
); }