'use client' import { useCallback, useEffect, useRef, useState } from 'react' import Link from 'next/link' import { useTranslation } from 'react-i18next' import { ArrowRight, Check, ChevronDown, FileUp, Loader2, Library } from 'lucide-react' import { useChatWorkspaces } from '@/hooks/useChatWorkspaces' import { activeWorkspaceId, scopedUrl } from '@/lib/workspace-scope' import { getPracticeQueue, getPracticeSummary, type PracticeRef, type PracticeSummary, } from '@/lib/practice-api' import { LearningShell, LearningErrorState } from '../LearningShell' import { useLearningCreation } from '../LibraryWorkspace' import { PracticeSession } from './PracticeSession' import { PracticeImport } from './PracticeImport' import { PracticeInsights } from './PracticeInsights' export function ReviewHome() { const { t } = useTranslation() const { workspaces } = useChatWorkspaces() const [scope, setScope] = useState('*') const [summary, setSummary] = useState(null) const [error, setError] = useState('') const [revision, setRevision] = useState(0) const [session, setSession] = useState(null) const [starting, setStarting] = useState(false) const [importing, setImporting] = useState(false) const [notice, setNotice] = useState('') const sequence = useRef(0) const creation = useLearningCreation(() => setImporting(true)) const refresh = useCallback(async () => { const request = ++sequence.current try { const next = await getPracticeSummary('', scope) if (request !== sequence.current) return setSummary(next) setError( next.unavailable_workspaces?.length ? t('Some workspaces could not be loaded. Available content is shown.') : '' ) } catch (err) { if (request === sequence.current) setError(err instanceof Error ? err.message : String(err)) } }, [scope, t]) useEffect(() => { void refresh() const timer = window.setInterval(() => void refresh(), 60000) window.addEventListener('focus', refresh) return () => { // Invalidate requests when changing the review scope. // eslint-disable-next-line react-hooks/exhaustive-deps sequence.current++ window.clearInterval(timer) window.removeEventListener('focus', refresh) } }, [refresh]) const start = async () => { if (starting) return setStarting(true) try { const rows = await getPracticeQueue('', scope) if (rows.length) setSession( rows.map(row => ({ id: row.entry.id, content_workspace_id: row.content_workspace_id, content_workspace_name: row.content_workspace_name, })) ) else { setNotice(t('You are caught up. Come back when your next reviews are due.')) void refresh() } } catch (err) { setError(err instanceof Error ? err.message : String(err)) } finally { setStarting(false) } } const completed = () => { setSession(null) setRevision(value => value + 1) void refresh() } return ( {t('Import questions')} ) } > {creation.dialog} {session ? (
) : (
{error && void refresh()} />} {notice && (

{notice}

)} {importing && ( setImporting(false)} onImported={message => { setNotice(message) setImporting(false) setRevision(value => value + 1) void refresh() }} /> )}

{t("Today's review")}

{summary?.due ?? '—'} {t('Ready to review')}

{t( 'Mastered questions leave this round and return when your next review is due.' )}

{[ ['Reviewed today', summary?.reviewed_today], ['Scheduled for later', summary ? summary.total - summary.due : undefined], ['Question Bank', summary?.total], ].map(([label, count]) => (
{count ?? '—'} {t(String(label))}
))}

{t('Recall first, then check. Forgotten questions come back in this round.')}

{t('Manage question bank')}
{t('Practice analytics')}
)}
) }