'use client' import { useTranslation } from 'react-i18next' import { OverviewRow, Status, matches } from './NavigationPrimitives' import { RecordGroups, RecordHeader, RecordTime, useRecordView } from './RecordList' import { useLayoutEffect, useState, type ReactNode } from 'react' import Link from 'next/link' import { usePathname, useSearchParams } from 'next/navigation' import { ChevronDown, ChevronRight, ClipboardList, LayoutGrid, ListFilter, MessageCircle, Plus } from 'lucide-react' import { learningSurface } from '@/components/learning/surfaces' import { allLearningActivity, fileLearningSessions, learningSessionKind, type LearningActivity, type LearningKind } from '@/lib/learning-dashboard' import { isNavigationTargetActive } from '@/lib/navigation' import { useLearningIndex } from '@/hooks/useLearningIndex' import { useSidebarDrawer } from '@/components/layout/AppShell' import { browserStorage } from '@/shared/storage' import { displaySessionTitle } from '@/lib/session-title' import type { SessionSummary } from '@/lib/session-api' const OPEN_KEY = 'deeptutor.navigation.learning.open' const FILTER_KINDS = ['mastery', 'practice', 'books', 'reading'] as const type KindFilter = 'all' | (typeof FILTER_KINDS)[number] function matchesKind(kind: LearningKind | null, filter: KindFilter) { return filter === 'all' || (kind === 'watching' ? 'reading' : kind) === filter } /** The learning conversations of this workspace, and how to draw a few of them. */ export interface LearningSessions { rows: readonly SessionSummary[] activeSessionId: string | null /** The shell's own conversation rows, actions included. */ render: (rows: SessionSummary[]) => ReactNode } type Row = | { kind: 'record'; key: string; record: LearningActivity; sessions: SessionSummary[]; time: number } | { kind: 'session'; key: string; session: SessionSummary; time: number } function readOpen(): Record { try { return JSON.parse(browserStorage.readRaw('local', OPEN_KEY) ?? '{}') as Record } catch { return {} } } /** * Learning records with their conversations underneath: a path, a collection * or a book is one entry, and what was said while studying it is filed inside * it rather than in a second list beside it. */ export function LearningNavigation({ sessions, active = true, }: { sessions?: LearningSessions active?: boolean }) { const { t } = useTranslation() const { sources, loading, failed, retry } = useLearningIndex(active) const view = useRecordView('deeptutor.navigation.learning.sort') const [kindFilter, setKindFilter] = useState('all') const pathname = usePathname() const query = new URLSearchParams(useSearchParams().toString()) // A collapsed practice set opens on the question being practised, the same // fallback that opens a record on the conversation being read — unless the // learner stored a collapse preference for it. const practicing = (record: LearningActivity) => (record.children ?? []).some(child => isNavigationTargetActive(child.href, pathname, query)) const [open, setOpen] = useState>({}) useLayoutEffect(() => { // Restore which records were left open before paint, without a mismatch. // eslint-disable-next-line react-hooks/set-state-in-effect setOpen(readOpen()) }, []) const { sort } = view const at = (session: SessionSummary) => (sort === 'createdAt' ? session.created_at : session.updated_at) const placeholder = t('New chat') const titleOf = (session: SessionSummary) => displaySessionTitle(session.title, placeholder) const records = allLearningActivity(sources) const filed = fileLearningSessions(records, sessions?.rows ?? []) const rows: Row[] = [ ...records.map(record => { const children = filed.children.get(record.key) ?? [] // A record is as recent as the latest conversation held in it. const time = sort === 'createdAt' ? record.createdAt : Math.max(record.updatedAt, ...children.map(at)) return { kind: 'record' as const, key: record.key, record, sessions: children, time } }), ...filed.loose.map(session => ({ kind: 'session' as const, key: session.session_id, session, time: at(session) })), ] // File conversations before filtering, so hidden parents do not leave loose chats behind. .filter(row => matchesKind(row.kind === 'record' ? row.record.kind : learningSessionKind(row.session), kindFilter)) .filter(row => row.kind === 'session' ? matches(titleOf(row.session), view.query) : matches(`${row.record.title} ${t(learningSurface(row.record.kind).title)}`, view.query) || row.sessions.some(session => matches(titleOf(session), view.query)) || (row.record.children ?? []).some(child => matches(child.title, view.query)) ) .sort((a, b) => b.time - a.time || a.key.localeCompare(b.key)) const toggle = (key: string, next: boolean) => setOpen(previous => { const value = { ...previous, [key]: next } browserStorage.writeRaw('local', OPEN_KEY, JSON.stringify(value)) return value }) return (
0} retry={retry} /> row.time} renderRow={row => row.kind === 'session' ? (
{sessions?.render([row.session])}
) : ( matches(child.title, view.query)) || row.sessions.some(session => matches(titleOf(session), view.query)))) || (open[row.key] ?? (row.sessions.some(session => session.session_id === sessions?.activeSessionId) || practicing(row.record))) } onToggle={next => toggle(row.key, next)} /> ) } empty={ !loading && !failed.length && (

{t(view.query || kindFilter !== 'all' ? 'navigation.noResults' : 'navigation.noLearning')}

) } />
) } /** One record; its icon turns into the disclosure for the conversations held in it. */ function RecordNode({ record, time, sessions, render, open, onToggle, }: { record: LearningActivity time: number sessions: SessionSummary[] render?: (rows: SessionSummary[]) => ReactNode open: boolean onToggle: (open: boolean) => void }) { const { t } = useTranslation() const drawer = useSidebarDrawer() const pathname = usePathname() const search = useSearchParams() const selected = isNavigationTargetActive(record.href, pathname, new URLSearchParams(search.toString())) const Icon = learningSurface(record.kind).icon const questions = record.children ?? [] const expandable = questions.length > 0 || (sessions.length > 0 && Boolean(render)) const expanded = expandable && open const toggleLabel = questions.length ? t(expanded ? 'navigation.hidePracticeQuestions' : 'navigation.showPracticeQuestions', { count: questions.length, }) : t(expanded ? 'navigation.hideRecordChats' : 'navigation.showRecordChats', { count: sessions.length, }) return (
{expandable ? ( ) : ( )} { if (!event.metaKey && !event.ctrlKey && !event.shiftKey) drawer?.close() }} className="dt-record-node-link" > {record.title} {questions.length > 0 && !expanded && ( {questions.length} )} {expandable && !expanded && questions.length === 0 && ( {sessions.length} )}
{expanded && (
{questions.map(question => ( ))} {render && sessions.length > 0 && render(sessions)}
)}
) } /** One question of a collapsed practice set, linking to its own practice turn. */ function PracticeQuestionNode({ question }: { question: LearningActivity }) { const drawer = useSidebarDrawer() const pathname = usePathname() const search = useSearchParams() const selected = isNavigationTargetActive( question.href, pathname, new URLSearchParams(search.toString()) ) return ( { if (!event.metaKey && !event.ctrlKey && !event.shiftKey) drawer?.close() }} className={`dt-record-question ${selected ? 'is-active' : ''}`} > {question.title} ) }