"use client"; import { learningLibrary, libraryItemKey } from "@/lib/learning-library"; import { useCreateRequest } from "@/components/learning/create-request"; import { LearningEmptyState, LearningErrorState, LearningSkeleton, } from "@/components/learning/LearningShell"; import { readingCollectionRoute, readingFolderRoute } from "@/lib/learning-routes"; import Link from "next/link"; import InvidiousAccountFeedback from "./InvidiousAccountFeedback"; import { useRouter } from "next/navigation"; import { BookOpen, Loader2, MoreHorizontal, Palette, Search, Trash2, TriangleAlert, X, } from "lucide-react"; import { useCallback, useEffect, useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { deleteReadingWorkspace, retryReadingMaterial, type ReadingLibraryMaterial, type ReadingWorkspace, } from "@/lib/reading-workspace-api"; import { readingFailureMessage } from "@/lib/reading-failure"; import { FolderDialog } from "./FolderDialog"; import { FolderGlyph } from "./FolderGlyph"; import { LibraryShell } from "./LibraryShell"; import { relativeDate } from "./shared"; type SortMode = "recent" | "name"; type FolderDialogState = | { mode: "create" } | { mode: "edit"; collection: ReadingWorkspace }; export function ReadingLibraryPage() { const { t, i18n } = useTranslation(); const router = useRouter(); const [collections, setCollections] = useState([]); const [materials, setMaterials] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(""); const [search, setSearch] = useState(""); const [sort, setSort] = useState("recent"); const [folderDialog, setFolderDialog] = useState( null ); const [menuFor, setMenuFor] = useState(null); const [deleteTarget, setDeleteTarget] = useState( null ); const create = useCallback(() => setFolderDialog({ mode: "create" }), []); useCreateRequest(create); const refresh = useCallback(async () => { setError(""); try { const [collectionRows, library] = await Promise.all([ learningLibrary("reading"), learningLibrary("materials"), ]); setCollections(collectionRows.items.filter(row => !search || `${row.title} ${row.description}`.toLowerCase().includes(search.toLowerCase()))); setMaterials(library.items); } catch (caught) { // Keep whatever is on screen: an empty list would claim the user has no // collections, which is a different statement from "the request failed". setError( caught instanceof Error ? caught.message : t("Could not load your collections.") ); } finally { setLoading(false); } }, [search, t]); useEffect(() => { const timer = window.setTimeout(() => void refresh(), 140); return () => window.clearTimeout(timer); }, [refresh]); // Materials still being prepared, or that failed — the list page is where a // user looks first, so it has to say so here rather than only inside a // collection. const unsettled = useMemo( () => materials.filter( material => material.status === "processing" || material.status === "queued" || material.status === "failed" ), [materials] ); const rows = useMemo(() => { const sorted = [...collections]; sorted.sort((a, b) => sort === "name" ? a.title.localeCompare(b.title, i18n.language) : b.updated_at - a.updated_at ); return sorted; }, [collections, i18n.language, sort]); const menuKey = (collection: ReadingWorkspace) => libraryItemKey(collection, collection.workspace_id); return (
{t("{{count}} collections", { count: rows.length })}
setSort("recent")} /> setSort("name")} />
{error && ( void refresh()} /> )} {unsettled.map(material => ( void refresh()} /> ))} {loading ? ( ) : !rows.length ? ( // A failed request is not an empty library: showing the "no // collections yet" pitch on top of an error would state something we // do not know to be true. error ? null : ( ) ) : (
    {rows.map(collection => ( setMenuFor(current => current === menuKey(collection) ? null : menuKey(collection) ) } onCloseMenu={() => setMenuFor(null)} onEdit={() => { setMenuFor(null); setFolderDialog({ mode: "edit", collection }); }} onDelete={() => { setMenuFor(null); setDeleteTarget(collection); }} /> ))}
)} {folderDialog && ( setFolderDialog(null)} onSaved={async (collection) => { const created = folderDialog.mode === "create"; setFolderDialog(null); if (!created) { void refresh(); return; } router.push(readingFolderRoute(collection.workspace_id)); }} /> )} {deleteTarget && ( setDeleteTarget(null)} onDeleted={async () => { setDeleteTarget(null); await refresh(); }} /> )}
); } function SortButton({ label, active, onClick, }: { label: string; active: boolean; onClick: () => void; }) { return ( ); } function FolderTile({ collection, menuOpen, onToggleMenu, onCloseMenu, onEdit, onDelete, }: { collection: ReadingWorkspace; menuOpen: boolean; onToggleMenu: () => void; onCloseMenu: () => void; onEdit: () => void; onDelete: () => void; }) { const { t, i18n } = useTranslation(); const files = collection.tabs.length; const preparing = collection.tabs.some( tab => tab.material.status === "processing" || tab.material.status === "queued" ); const contentWorkspace = collection.content_workspace_id ?? ""; return (
  • {preparing && ( )} {collection.title} {[ files ? t("{{count}} files", { count: files }) : t("Empty"), relativeDate(collection.updated_at, i18n.language), ] .filter(Boolean) .join(" · ")} {menuOpen && ( <>
    {t("Start reading")}
    )}
  • ); } function UnsettledRow({ material, onRetried, }: { material: ReadingLibraryMaterial; onRetried: () => void; }) { const { t } = useTranslation(); const [retrying, setRetrying] = useState(false); const failed = material.status === "failed"; return (
    {failed ? ( ) : ( )} {failed ? t("{{title}} could not be prepared", { title: material.title }) : t("Preparing {{title}}", { title: material.title })} {failed && readingFailureMessage(material, t) ? ( {readingFailureMessage(material, t)} ) : null} {!failed && ( <> {material.progress}% )} {failed && ( )}
    ); } function EmptyCollections({ searching, onCreate, }: { searching: boolean; onCreate: () => void; }) { const { t } = useTranslation(); return ( {t("New collection")} ) } /> ); } export function DeleteCollectionDialog({ collection, onClose, onDeleted, }: { collection: ReadingWorkspace; onClose: () => void; onDeleted: () => Promise; }) { const { t } = useTranslation(); const [working, setWorking] = useState(false); const [error, setError] = useState(""); return (

    {t("Delete collection")}

    {t( "“{{title}}” and its reading conversations will be deleted. The {{count}} materials in it stay in your library.", { title: collection.title, count: collection.tabs.length, } )}

    {error && (

    {error}

    )}
    ); }