Files
DeepTutor/web/components/reading/library/ReadingLibrary.tsx
Bingxi Zhao (Frank) 28c11e2ae8 feat: update notes, chat interactions and workspace UI on preview
Remove course pages, APIs and the course_study capability. Add anchored note
threads and reviewed learning-block generation, queued and withdrawable chat
messages, quote attachments, side chats, and the calendar/favorites/navigation
updates.

Keep preview's existing backend architecture. Wire the new features through
its Python application and existing chat engine, retain legacy selected-text
requests, persist mid-turn messages in replay, and restore note context when
regenerating. Leave stored user data intact.

Validate frontend contracts, dependency boundaries, types, 1290 node tests,
840 rendered tests, lint, localization and the production build. Exercise
session/notes/API regressions, 53 chat-engine tests and 20 Python/native
integration tests with isolated runtime data.
2026-10-26 22:13:34 +08:00

546 lines
17 KiB
TypeScript

"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<ReadingWorkspace[]>([]);
const [materials, setMaterials] = useState<ReadingLibraryMaterial[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState("");
const [search, setSearch] = useState("");
const [sort, setSort] = useState<SortMode>("recent");
const [folderDialog, setFolderDialog] = useState<FolderDialogState | null>(
null
);
const [menuFor, setMenuFor] = useState<string | null>(null);
const [deleteTarget, setDeleteTarget] = useState<ReadingWorkspace | null>(
null
);
const create = useCallback(() => setFolderDialog({ mode: "create" }), []);
useCreateRequest(create);
const refresh = useCallback(async () => {
setError("");
try {
const [collectionRows, library] = await Promise.all([
learningLibrary<ReadingWorkspace>("reading"),
learningLibrary<ReadingLibraryMaterial>("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 (
<LibraryShell
view="collections"
collectionCount={rows.length}
materialCount={materials.length}
actionLabel={t("New collection")}
onAction={create}
>
<InvidiousAccountFeedback />
<div className="mt-5 flex flex-col gap-3 border-b border-[var(--border)] pb-3 sm:flex-row sm:items-center">
<label className="flex h-8 min-w-0 flex-1 items-center gap-2 rounded-lg border border-[var(--border)] px-2.5 sm:max-w-[330px]">
<Search
size={13}
className="shrink-0 text-[var(--muted-foreground)]"
/>
<input
value={search}
onChange={event => setSearch(event.target.value)}
placeholder={t("Search collections and materials")}
className="min-w-0 flex-1 bg-transparent text-[12px] outline-none placeholder:text-[var(--muted-foreground)]"
/>
{!!search && (
<button
type="button"
onClick={() => setSearch("")}
aria-label={t("Clear")}
>
<X size={12} className="text-[var(--muted-foreground)]" />
</button>
)}
</label>
<div className="flex items-center gap-2 sm:ml-auto">
<span className="text-[11px] text-[var(--muted-foreground)]">
{t("{{count}} collections", { count: rows.length })}
</span>
<div className="flex overflow-hidden rounded-md border border-[var(--border)]">
<SortButton
label={t("Recent")}
active={sort === "recent"}
onClick={() => setSort("recent")}
/>
<SortButton
label={t("Name")}
active={sort === "name"}
onClick={() => setSort("name")}
/>
</div>
</div>
</div>
{error && (
<LearningErrorState message={error} onRetry={() => void refresh()} />
)}
{unsettled.map(material => (
<UnsettledRow
key={libraryItemKey(material, material.material_id)}
material={material}
onRetried={() => void refresh()}
/>
))}
{loading ? (
<LearningSkeleton />
) : !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 : (
<EmptyCollections
searching={Boolean(search)}
onCreate={create}
/>
)
) : (
<ul className="mt-6 grid grid-cols-[repeat(auto-fill,minmax(148px,1fr))] gap-1">
{rows.map(collection => (
<FolderTile
key={menuKey(collection)}
collection={collection}
menuOpen={menuFor === menuKey(collection)}
onToggleMenu={() =>
setMenuFor(current =>
current === menuKey(collection) ? null : menuKey(collection)
)
}
onCloseMenu={() => setMenuFor(null)}
onEdit={() => {
setMenuFor(null);
setFolderDialog({ mode: "edit", collection });
}}
onDelete={() => {
setMenuFor(null);
setDeleteTarget(collection);
}}
/>
))}
</ul>
)}
{folderDialog && (
<FolderDialog
collection={folderDialog.mode === "edit" ? folderDialog.collection : undefined}
onClose={() => setFolderDialog(null)}
onSaved={async (collection) => {
const created = folderDialog.mode === "create";
setFolderDialog(null);
if (!created) {
void refresh();
return;
}
router.push(readingFolderRoute(collection.workspace_id));
}}
/>
)}
{deleteTarget && (
<DeleteCollectionDialog
collection={deleteTarget}
onClose={() => setDeleteTarget(null)}
onDeleted={async () => {
setDeleteTarget(null);
await refresh();
}}
/>
)}
</LibraryShell>
);
}
function SortButton({
label,
active,
onClick,
}: {
label: string;
active: boolean;
onClick: () => void;
}) {
return (
<button
type="button"
onClick={onClick}
className={`px-2.5 py-1 text-[11px] transition ${
active
? "bg-[var(--muted)] font-semibold text-[var(--foreground)]"
: "text-[var(--muted-foreground)]"
}`}
>
{label}
</button>
);
}
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 (
<li className="group relative">
<Link
href={readingFolderRoute(collection.workspace_id, contentWorkspace)}
className="flex h-full flex-col items-center rounded-xl px-2.5 pb-3 pt-4 text-center transition hover:bg-[var(--secondary)] focus-visible:bg-[var(--secondary)] focus-visible:outline-none"
>
<span className="relative">
<FolderGlyph color={collection.color} files={files} size={64} />
{preparing && (
<Loader2
size={13}
className="absolute bottom-1 right-1 animate-spin text-white"
aria-label={t("Preparing")}
/>
)}
</span>
<span className="mt-2.5 line-clamp-2 max-w-full break-words text-[13px] font-medium leading-snug text-[var(--foreground)]">
{collection.title}
</span>
<span className="mt-0.5 max-w-full truncate text-[11px] text-[var(--muted-foreground)]">
{[
files
? t("{{count}} files", { count: files })
: t("Empty"),
relativeDate(collection.updated_at, i18n.language),
]
.filter(Boolean)
.join(" · ")}
</span>
</Link>
<button
type="button"
onClick={onToggleMenu}
aria-label={t("Collection menu")}
aria-expanded={menuOpen}
className="absolute right-1.5 top-1.5 flex size-7 items-center justify-center rounded-md text-[var(--muted-foreground)] opacity-100 transition hover:bg-[var(--muted)] sm:opacity-0 sm:focus:opacity-100 sm:group-hover:opacity-100 aria-expanded:opacity-100"
>
<MoreHorizontal size={14} />
</button>
{menuOpen && (
<>
<div className="fixed inset-0 z-10" onClick={onCloseMenu} />
<div
role="menu"
className="absolute right-1.5 top-9 z-20 w-44 rounded-lg border border-[var(--border)] bg-[var(--card)] p-1 text-[12px] shadow-md dark:bg-[var(--popover)]"
>
<Link
role="menuitem"
href={readingCollectionRoute(collection.workspace_id, contentWorkspace)}
className="flex w-full items-center gap-2 rounded-md px-2.5 py-2 text-left hover:bg-[var(--muted)]"
>
<BookOpen size={12} />
{t("Start reading")}
</Link>
<button
type="button"
role="menuitem"
onClick={onEdit}
className="flex w-full items-center gap-2 rounded-md px-2.5 py-2 text-left hover:bg-[var(--muted)]"
>
<Palette size={12} />
{t("Rename or recolor")}
</button>
<button
type="button"
role="menuitem"
onClick={onDelete}
className="flex w-full items-center gap-2 rounded-md px-2.5 py-2 text-left text-[var(--destructive)] hover:bg-[var(--muted)]"
>
<Trash2 size={12} />
{t("Delete collection")}
</button>
</div>
</>
)}
</li>
);
}
function UnsettledRow({
material,
onRetried,
}: {
material: ReadingLibraryMaterial;
onRetried: () => void;
}) {
const { t } = useTranslation();
const [retrying, setRetrying] = useState(false);
const failed = material.status === "failed";
return (
<div className="mt-3 flex items-center gap-3 rounded-lg border border-[var(--border)] px-3 py-2.5">
{failed ? (
<TriangleAlert
size={13}
className="shrink-0 text-[var(--destructive)]"
/>
) : (
<Loader2
size={13}
className="shrink-0 animate-spin text-[var(--primary)]"
/>
)}
<span className="min-w-0 flex-1 truncate text-[11.5px]">
{failed
? t("{{title}} could not be prepared", { title: material.title })
: t("Preparing {{title}}", { title: material.title })}
{failed && readingFailureMessage(material, t) ? (
<span className="ml-1.5 text-[10.5px] text-[var(--muted-foreground)]">
{readingFailureMessage(material, t)}
</span>
) : null}
</span>
{!failed && (
<>
<span className="hidden h-[3px] w-[160px] shrink-0 overflow-hidden rounded-full bg-[var(--muted)] sm:block">
<span
className="block h-full bg-[var(--primary)]"
style={{ width: `${Math.max(4, material.progress)}%` }}
/>
</span>
<span className="shrink-0 text-[10.5px] tabular-nums text-[var(--muted-foreground)]">
{material.progress}%
</span>
</>
)}
{failed && (
<button
type="button"
disabled={retrying}
onClick={() => {
setRetrying(true);
void retryReadingMaterial(material.material_id, material.content_workspace_id ?? "")
.then(onRetried)
.catch(() => undefined)
.finally(() => setRetrying(false));
}}
className="shrink-0 text-[11px] font-semibold text-[var(--primary)] disabled:opacity-50"
>
{retrying ? t("Retrying…") : t("Retry")}
</button>
)}
</div>
);
}
function EmptyCollections({
searching,
onCreate,
}: {
searching: boolean;
onCreate: () => void;
}) {
const { t } = useTranslation();
return (
<LearningEmptyState
title={searching ? t("Nothing matches that.") : t("No collections yet")}
description={
!searching &&
t(
"A collection is one reading task: a paper with its survey, every lecture of a course, a few chapters of a book. Everything in it shares the same conversations and annotations."
)
}
action={
!searching && (
<button
type="button"
onClick={onCreate}
className="inline-flex h-8 items-center rounded-lg bg-[var(--primary)] px-3.5 text-xs font-semibold text-[var(--primary-foreground)]"
>
{t("New collection")}
</button>
)
}
/>
);
}
export function DeleteCollectionDialog({
collection,
onClose,
onDeleted,
}: {
collection: ReadingWorkspace;
onClose: () => void;
onDeleted: () => Promise<void>;
}) {
const { t } = useTranslation();
const [working, setWorking] = useState(false);
const [error, setError] = useState("");
return (
<div className="fixed inset-0 z-[110] flex items-center justify-center bg-[var(--overlay)] p-4">
<div className="w-full max-w-md rounded-xl border border-[var(--border)] bg-[var(--card)] p-5 shadow-lg dark:bg-[var(--popover)]">
<h2 className="font-serif text-[17px] font-semibold">
{t("Delete collection")}
</h2>
<p className="mt-2 text-[12px] leading-relaxed text-[var(--muted-foreground)]">
{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,
}
)}
</p>
{error && (
<p className="mt-3 text-[11px] text-[var(--destructive)]">{error}</p>
)}
<div className="mt-5 flex justify-end gap-2">
<button
type="button"
onClick={onClose}
className="h-8 rounded-lg px-3 text-[11.5px] text-[var(--muted-foreground)] hover:bg-[var(--muted)]"
>
{t("Cancel")}
</button>
<button
type="button"
disabled={working}
onClick={() => {
setWorking(true);
setError("");
void deleteReadingWorkspace(collection.workspace_id, collection.content_workspace_id ?? "")
.then(onDeleted)
.catch(caught =>
setError(
caught instanceof Error
? caught.message
: t("Delete failed")
)
)
.finally(() => setWorking(false));
}}
className="inline-flex h-8 items-center gap-1.5 rounded-lg bg-[var(--destructive)] px-3.5 text-[11.5px] font-semibold text-[var(--destructive-foreground)] disabled:opacity-50"
>
{working && <Loader2 size={12} className="animate-spin" />}
{t("Delete")}
</button>
</div>
</div>
</div>
);
}