'use client' import Link from 'next/link' import { useEffect, useState } from 'react' import { useRouter } from 'next/navigation' import { useTranslation } from 'react-i18next' import { learningLibrary, libraryItemKey, type LearningOrigin } from '@/lib/learning-library' import { activeWorkspaceId, scopedUrl } from '@/lib/workspace-scope' import { watchingRoute } from '@/lib/learning-routes' import { LearningShell, LearningErrorState, LearningSkeleton, LearningEmptyState } from './LearningShell' import { useLearningCreation, useLibraryFilter, WorkspaceLabel } from './LibraryWorkspace' interface Activity extends LearningOrigin { id?: number session_id?: string title?: string question?: string updated_at?: number } /** All-account discovery; opening an item binds subsequent interaction to its store. */ export function ActivityLibrary({ kind, onCreate }: { kind: 'practice' | 'watching'; onCreate: () => void }) { const { t } = useTranslation() const router = useRouter() const [items, setItems] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState('') const [query, setQuery] = useState('') const [revision, setRevision] = useState(0) const { rows, control } = useLibraryFilter(items) const creation = useLearningCreation(onCreate) useEffect(() => { let alive = true void learningLibrary(kind).then(result => { if (!alive) return setItems(result.items) setError(result.unavailable_workspaces.length ? t('Some workspaces could not be loaded. Available content is shown.') : '') }).catch(() => { if (alive) setError(t('Could not load learning records.')) }) .finally(() => { if (alive) setLoading(false) }) return () => { alive = false } }, [kind, revision, t]) const matches = rows.filter(row => (row.title || row.question || '').toLowerCase().includes(query.toLowerCase())) const stores = [...new Map(rows.map(row => [row.content_workspace_id ?? '', row])).values()] return {t(kind === 'practice' ? 'Import questions' : 'New watching session')}}> {creation.dialog}
{control} setQuery(event.target.value)} className="h-9 min-w-0 rounded-lg border border-border bg-background px-3 text-sm" />
{kind === 'practice' &&
{stores.map(row => ยท {t('Review and manage')} )} {!stores.length && !loading && }
} {error && setRevision(value => value + 1)} />} {loading ? : !matches.length ? :
{matches.map(row =>
{row.title || row.question || t('New chat')}
)}
}
}