import { useEffect, useState } from 'react'; import { useRouter } from 'next/router'; import Layout from '../../components/Layout'; import CardPreview from '../../components/designer/CardFrame'; import { Button, Input } from '../../components/ui'; import { useAuth } from '../../lib/use-auth'; export default function GameSpace() { const router = useRouter(); const { id } = router.query; const { user, loading: authLoading } = useAuth(); const [game, setGame] = useState(null); const [designs, setDesigns] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [editing, setEditing] = useState(false); const [name, setName] = useState(''); const [description, setDescription] = useState(''); const [saving, setSaving] = useState(false); useEffect(() => { if (!authLoading && !user) { router.push('/login'); } }, [authLoading, user, router]); useEffect(() => { if (!user || !id) return undefined; const loadGame = async () => { setLoading(true); try { const token = localStorage.getItem('auth_token'); const response = await fetch(`/api/custom-games/${id}`, { headers: token ? { Authorization: `Bearer ${token}` } : {}, }); const data = await response.json(); if (response.ok) { setGame(data.game); setDesigns(data.designs); setName(data.game.name); setDescription(data.game.description || ''); } else { setError(data.error || 'Game not found.'); } } catch { setError('Failed to load the game.'); } finally { setLoading(false); } }; loadGame(); return undefined; }, [user, id]); const handleSave = async () => { if (!name.trim()) return; setSaving(true); try { const token = localStorage.getItem('auth_token'); const response = await fetch(`/api/custom-games/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json', ...(token ? { Authorization: `Bearer ${token}` } : {}) }, body: JSON.stringify({ name, description }), }); const data = await response.json(); if (response.ok) { setGame((prev) => ({ ...prev, name: data.game.name, description: data.game.description })); setEditing(false); } else { setError(data.error || 'Save failed.'); } } catch { setError('Save failed.'); } finally { setSaving(false); } }; const handleDelete = async () => { if (!window.confirm(`Delete "${game?.name}"? Cards keep existing but leave the game.`)) return; try { const token = localStorage.getItem('auth_token'); const response = await fetch(`/api/custom-games/${id}`, { method: 'DELETE', headers: token ? { Authorization: `Bearer ${token}` } : {}, }); if (response.ok) { router.push('/games'); } else { setError('Delete failed.'); } } catch { setError('Delete failed.'); } }; if (loading) { return ( ); } if (error && !game) { return ( {error} router.push('/games')}>Back to My Games ); } return ( {/* Header */} {editing ? ( setName(e.target.value)} maxLength={100} /> setDescription(e.target.value)} placeholder="Description (optional)" maxLength={300} /> {saving ? 'Saving…' : 'Save'} setEditing(false)}>Cancel ) : ( {game.name} {game.description || 'Custom game system'} · {designs.length} card{designs.length === 1 ? '' : 's'} )} router.push('/games')}>My Games setEditing(true)}>Edit Delete router.push(`/designer?game=${game.id}`)}> + New Card {error && ( {error} )} {/* Designs grid */} {designs.length === 0 ? ( No cards in this game yet Design the first card for {game.name}. router.push(`/designer?game=${game.id}`)}> Open Card 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}` : ''} router.push(`/designer?id=${design.id}`)}> Edit ))} )} ); }
{game.description || 'Custom game system'} · {designs.length} card{designs.length === 1 ? '' : 's'}
Design the first card for {game.name}.
{design.name}
{design.card_type || 'No type'} {design.mana_cost ? `· ${design.mana_cost}` : ''}