import {useEffect, useState} from 'react'; import {useNavigate} from 'react-router-dom'; import {useTranslation} from 'react-i18next'; import {toast} from 'sonner'; import {ListTodo, Loader2, Star} from 'lucide-react'; import {Button} from '@/components/ui/button'; import {Card} from '@/components/ui/card'; import {Label} from '@/components/ui/label'; import {Select, SelectContent, SelectItem, SelectTrigger, SelectValue} from '@/components/ui/select'; import {Textarea} from '@/components/ui/textarea'; import {cn} from '@/lib/utils'; import {STATUS_META} from './GameStatusBadge.jsx'; import {GAME_PRIORITIES, GAME_STATUSES, NO_PRIORITY, saveBacklogEntry, toBacklogRequest} from '../utils/backlog.js'; import useSessionExpired from '../hooks/useSessionExpired.js'; import {hasSession} from '../utils/auth.js'; const NOTES_MAX = 2000; const toForm = (entry) => ({ status: entry.status ?? 'BACKLOG', rating: entry.rating ?? 0, notes: entry.notes ?? '', priority: entry.priority ?? NO_PRIORITY, }); export default function BacklogCard({game, onSaved}) { const {t} = useTranslation(); const navigate = useNavigate(); const expireSession = useSessionExpired(); const [saved, setSaved] = useState(() => toForm(game)); const [form, setForm] = useState(saved); // The PlayingHint can change the status from outside; keep the other unsaved edits. useEffect(() => { setSaved((current) => (current.status === game.status ? current : {...current, status: game.status})); setForm((current) => (current.status === game.status ? current : {...current, status: game.status})); }, [game.status]); const [busy, setBusy] = useState(false); const dirty = JSON.stringify(form) !== JSON.stringify(saved); const set = (field) => (value) => setForm((f) => ({...f, [field]: value})); const save = async () => { if (!hasSession()) { navigate('/login'); return; } setBusy(true); try { const response = await saveBacklogEntry(game.appid, toBacklogRequest(form)); if (response.status === 401) { expireSession(); return; } if (!response.ok) { toast.error(t('BACKLOG.SAVE_FAILED')); return; } const entry = await response.json(); const next = toForm(entry); setSaved(next); onSaved?.(entry); setForm(next); toast.success(t('BACKLOG.SAVED')); } catch (err) { console.error('backlog save error:', err); toast.error(t('INFO.ERROR.NETWORK_FAILURE')); } finally { setBusy(false); } }; return (

{t('BACKLOG.TITLE')}

{[1, 2, 3, 4, 5].map((n) => ( ))}
{form.notes.length} / {NOTES_MAX}