"use client"; import { LearningEmptyState, LearningErrorState, LearningSkeleton, } from "@/components/learning/LearningShell"; import { learningLibrary, libraryItemKey } from "@/lib/learning-library"; import { useCreateRequest, requestedLearningCreation } from "@/components/learning/create-request"; import { readingCollectionRoute, readingFolderRoute } from "@/lib/learning-routes"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { FolderPlus, Loader2, MoreHorizontal, Search, Star, Trash2, X, } from "lucide-react"; import { useCallback, useEffect, useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { addReadingWorkspaceMaterial, deleteReadingMaterial, retryReadingMaterial, type ReadingLibraryCounts, type ReadingLibraryFilter, type ReadingLibraryMaterial, type ReadingWorkspace, } from "@/lib/reading-workspace-api"; import { readingFailureMessage } from "@/lib/reading-failure"; import { AddMaterialsDialog } from "./AddMaterialsDialog"; import { LibraryShell } from "./LibraryShell"; import { displayUrl, formatBytes, formatDuration, formatTag, MaterialGlyph, relativeDate, sourceKindKey, } from "./shared"; /** * Grid template shared by the header and every row, so columns line up. * Membership earns its column before the type does: "which collections is this * in, and what is in none of them" is the question this view exists to answer. */ const GRID = "grid grid-cols-[28px_minmax(0,1fr)_72px_28px] items-center gap-x-3 " + "sm:grid-cols-[32px_minmax(0,1fr)_minmax(0,200px)_72px_28px] " + "lg:grid-cols-[32px_minmax(0,1fr)_104px_minmax(0,236px)_72px_28px]"; export function MaterialLibraryPage() { const { t, i18n } = useTranslation(); const [materials, setMaterials] = useState([]); const [counts, setCounts] = useState(null); const [collections, setCollections] = useState([]); const [filter, setFilter] = useState("all"); const [search, setSearch] = useState(""); const [loading, setLoading] = useState(true); const [error, setError] = useState(""); const router = useRouter(); const [showUpload, setShowUpload] = useState(requestedLearningCreation); useCreateRequest(() => setShowUpload(true)); const [menuFor, setMenuFor] = useState(null); const [assignFor, setAssignFor] = useState( null ); const [deleteFor, setDeleteFor] = useState( null ); const refresh = useCallback(async () => { setError(""); try { const [library, collectionRows] = await Promise.all([ learningLibrary("materials"), learningLibrary("reading"), ]); setMaterials(library.items); setCounts(null); setCollections(collectionRows.items); const requested = new URLSearchParams(window.location.search).get('assign'); if (requested) { setAssignFor(library.items.find(row => row.material_id === requested) ?? null); const next = new URLSearchParams(window.location.search); next.delete('assign'); router.replace(`${window.location.pathname}${next.size ? `?${next}` : ''}`, { scroll: false }); } } catch (caught) { setError( caught instanceof Error ? caught.message : t("Could not load your library.") ); } finally { setLoading(false); } }, [t, router]); useEffect(() => { const timer = window.setTimeout(() => void refresh(), 140); return () => window.clearTimeout(timer); }, [refresh]); // Until the server reports totals, derive what we can from the rows on // screen so the filter chips are never blank. const tally = useMemo(() => { if (counts) return counts; return { all: materials.length, unassigned: materials.filter(row => !(row.collections ?? []).length) .length, processing: materials.filter( row => row.status === "processing" || row.status === "queued" ).length, failed: materials.filter(row => row.status === "failed").length, by_kind: {}, } satisfies ReadingLibraryCounts; }, [counts, materials]); const visibleMaterials = materials.filter(row => (!search || `${row.title} ${row.filename}`.toLowerCase().includes(search.toLowerCase())) && (filter === "all" || (filter === "unassigned" ? !(row.collections ?? []).length : filter === "processing" ? ["processing", "queued"].includes(row.status) : row.status === "failed")) ); return ( setShowUpload(true)} >
{t("{{count}} materials", { count: tally.all })}
setFilter("all")} /> setFilter("unassigned")} /> setFilter("processing")} /> setFilter("failed")} />
{error && ( void refresh()} /> )}
{t("reading.column.material")} {t("Type")} {t("In collections")} {t("Added")}
{loading ? ( ) : !visibleMaterials.length ? ( // Same rule as the collections view: an error already said what // happened, and "nothing here" would contradict it. error ? null : ( ) ) : (
    {visibleMaterials.map(material => ( setMenuFor(current => current === libraryItemKey(material, material.material_id) ? null : libraryItemKey(material, material.material_id) ) } onAssign={() => { setMenuFor(null); setAssignFor(material); }} onDelete={() => { setMenuFor(null); setDeleteFor(material); }} onRetried={() => void refresh()} /> ))}
)} {showUpload && ( setShowUpload(false)} onDone={() => { setShowUpload(false); void refresh(); }} /> )} {assignFor && ( setAssignFor(null)} onAssigned={async () => { setAssignFor(null); await refresh(); }} /> )} {deleteFor && ( setDeleteFor(null)} onDeleted={async () => { setDeleteFor(null); await refresh(); }} /> )}
); } function FilterChip({ label, count, active, onClick, }: { label: string; count?: number; active: boolean; onClick: () => void; }) { return ( ); } function MaterialRow({ material, locale, menuOpen, onToggleMenu, onAssign, onDelete, onRetried, }: { material: ReadingLibraryMaterial; locale: string; menuOpen: boolean; onToggleMenu: () => void; onAssign: () => void; onDelete: () => void; onRetried: () => void; }) { const { t } = useTranslation(); const router = useRouter(); const collections = material.collections ?? []; const tag = formatTag(material); const duration = formatDuration(material.duration_seconds); const size = formatBytes(material.size_bytes); // Pages for a paginated document, sections for everything else: the word // has to match what the reader will actually scroll through. const extent = material.unit_count ? material.render_mode === "pdf" ? t("{{count}} pages", { count: material.unit_count }) : t("{{count}} sections", { count: material.unit_count }) : ""; const preparing = material.status === "processing" || material.status === "queued"; const failed = material.status === "failed"; const quizStars = material.quiz_stars ?? 0; // The file's own identity: what it is called on disk, or where it came from. const identity = material.source_kind === "web" || material.source_kind === "youtube" || material.source_kind === "bilibili" ? displayUrl(material.source_url) : material.filename; // What the file is called (or where it came from), plus its size. The type // facts follow only on narrow screens, where the type column is hidden. const secondary = [identity, size, extent].filter(Boolean).join(" · "); const typeTail = [tag, duration].filter(Boolean).join(" · "); const open = () => { const target = collections[0]; if (target) router.push(readingCollectionRoute(target.workspace_id, material.content_workspace_id ?? "")); else onAssign(); }; return (
  • {[tag || t(sourceKindKey[material.source_kind]), duration || extent] .filter(Boolean) .join(" · ")} {collections.length ? ( <> {/* The first chip may shrink into the room the second one leaves, so a long title truncates before the second is pushed out — but it never grows past its own title. */} {collections.slice(0, 2).map((row, index) => ( {row.title} ))} {collections.length > 2 && ( +{collections.length - 2} )} ) : ( )} {failed ? ( ) : ( relativeDate(material.created_at, locale) )}
    {menuOpen && (
    )}
  • ); } function AssignDialog({ material, collections, onClose, onAssigned, }: { material: ReadingLibraryMaterial; collections: ReadingWorkspace[]; onClose: () => void; onAssigned: () => Promise; }) { const { t } = useTranslation(); const router = useRouter(); const [working, setWorking] = useState(""); const [error, setError] = useState(""); const [creating, setCreating] = useState(false); const held = new Set( (material.collections ?? []).map(row => row.workspace_id) ); const available = collections.filter( collection => !held.has(collection.workspace_id) ); if (creating) { return ( setCreating(false)} onDone={({ workspace }) => { if (!workspace) { setCreating(false); return; } void addReadingWorkspaceMaterial( workspace.workspace_id, material.material_id, true ) .then(() => router.push(readingCollectionRoute(workspace.workspace_id)) ) .catch(() => setCreating(false)); }} /> ); } return (

    {t("Add to a collection")}

    {material.title}

    {available.length ? ( available.map(collection => ( )) ) : (

    {collections.length ? t("It is already in every collection you have.") : t("You have no collections yet.")}

    )}
    {error && (

    {error}

    )}
    ); } function DeleteMaterialDialog({ material, onClose, onDeleted, }: { material: ReadingLibraryMaterial; onClose: () => void; onDeleted: () => Promise; }) { const { t } = useTranslation(); const [working, setWorking] = useState(false); const [error, setError] = useState(""); const collections = material.collections ?? []; return (

    {t("Delete material")}

    {collections.length ? t( "“{{title}}” is used by {{where}}. Deleting it removes it from those collections, along with its annotations.", { title: material.title, where: collections.map(row => row.title).join("、"), } ) : t("“{{title}}” and its annotations will be deleted.", { title: material.title, })}

    {error && (

    {error}

    )}
    ); }