DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
"use client";
import { useEffect, useState } from "react";
import {
ArrowLeft,
Bookmark,
ChevronLeft,
ChevronRight,
Compass,
Download,
Loader2,
RotateCcw,
} from "lucide-react";
import { bookApi } from "@/lib/book-api";
import { useTranslation } from "react-i18next";
import { ActivityMark } from "@/components/activity";
import type { ActivityState, MarkTone } from "@/components/activity";
import type { Book, Page } from "@/lib/book-types";
import Tooltip from "@/shared/ui/Tooltip";
const STATUS_LABEL: Record<string, string> = {
pending: "Queued",
planning: "Planning",
generating: "Compiling",
ready: "Ready",
partial: "Partial",
error: "Failed",
};
/**
* A chapter's status as the product's activity vocabulary.
*
* The list used to end every row with an uppercase tracked pill spelling the
* status out — six characters of shouting per row, in a column 232px wide,
* repeated down the whole chapter list. The mark says the same thing in 12px
* and animates while the chapter is actually being written, which is the one
* state a reader watches for.
*
* `tone` is what separates the two resting states, which the four-state
* vocabulary alone cannot: a written chapter and a queued one are both "not
* running", and a reader scanning the list wants to know which is which at a
* glance. Written gets a solid blue dot, queued a hollow grey ring.
*/
const PAGE_MARK: Record<string, { state: ActivityState; tone: MarkTone }> = {
pending: { state: "done", tone: "muted" },
planning: { state: "running", tone: "muted" },
generating: { state: "running", tone: "muted" },
ready: { state: "done", tone: "accent" },
partial: { state: "error", tone: "muted" },
error: { state: "error", tone: "muted" },
};
const RESTING_MARK = { state: "done", tone: "muted" } as const;
export interface BookSidebarProps {
book: Book | null;
onBackToLibrary: () => void;
pages?: Page[];
selectedPageId?: string | null;
onSelectPage?: (id: string) => void;
onRebuild?: () => void;
rebuilding?: boolean;
visitedPageIds?: string[];
bookmarkedPageIds?: string[];
}
export default function BookSidebar({
book,
onBackToLibrary,
pages = [],
selectedPageId = null,
onSelectPage,
onRebuild,
rebuilding = false,
visitedPageIds,
bookmarkedPageIds,
}: BookSidebarProps) {
const { t } = useTranslation();
const [collapsed, setCollapsed] = useState(false);
const [confirmRebuild, setConfirmRebuild] = useState(false);
// Destructive controls forget: an armed state that outlives the interaction
// turns the next stray click into an unconfirmed rebuild.
useEffect(() => {
if (!confirmRebuild) return;
const timer = setTimeout(() => setConfirmRebuild(false), 3500);
return () => clearTimeout(timer);
}, [confirmRebuild]);
if (collapsed) {
return (
<aside className="flex h-full w-14 flex-col items-center gap-3 bg-[rgb(var(--dt-ink,55_53_47)/0.022)] px-2 py-4">
<Tooltip label={t("All books")}>
<button
onClick={onBackToLibrary}
aria-label={t("All books")}
className="inline-flex h-8 w-8 items-center justify-center rounded-md text-[var(--muted-foreground)] transition-colors hover:bg-[var(--muted)]/40 hover:text-[var(--foreground)]"
>
<ArrowLeft className="h-4 w-4" />
</button>
</Tooltip>
<Tooltip label={t("Expand chapters")}>
<button
onClick={() => setCollapsed(false)}
aria-label={t("Expand chapters")}
className="inline-flex h-8 w-8 items-center justify-center rounded-md text-[var(--muted-foreground)] transition-colors hover:bg-[var(--muted)]/40 hover:text-[var(--foreground)]"
>
<ChevronRight className="h-4 w-4" />
</button>
</Tooltip>
<div className="mt-1 h-px w-8 bg-[var(--border)]" />
<div className="flex flex-1 flex-col items-center gap-1 overflow-y-auto">
{pages.map((page, index) => {
const active = page.id === selectedPageId;
return (
<Tooltip key={page.id} label={page.title || t("Untitled")}>
<button
onClick={() => onSelectPage?.(page.id)}
aria-label={page.title || t("Untitled")}
className={`inline-flex h-8 w-8 items-center justify-center rounded-md text-[11px] font-semibold ${
active
? "bg-[var(--primary)]/15 text-[var(--foreground)]"
: "text-[var(--muted-foreground)] hover:bg-[var(--muted)]/40 hover:text-[var(--foreground)]"
}`}
>
{page.content_type === "overview" ? (
<Compass className="h-3.5 w-3.5" />
) : (
index + 1
)}
</button>
</Tooltip>
);
})}
</div>
</aside>
);
}
return (
<aside className="flex h-full w-[232px] flex-col gap-3 bg-[rgb(var(--dt-ink,55_53_47)/0.022)] px-3 py-4">
<div className="flex items-center justify-between gap-2">
<button
onClick={onBackToLibrary}
className="dt-section-back inline-flex items-center gap-1.5 self-start rounded-md px-2 py-1 text-xs font-medium text-[var(--muted-foreground)] transition-colors hover:bg-[var(--muted)]/40 hover:text-[var(--foreground)]"
>
<ArrowLeft className="h-3.5 w-3.5" /> {t("All books")}
</button>
<Tooltip label={t("Collapse chapters")}>
<button
onClick={() => setCollapsed(true)}
aria-label={t("Collapse chapters")}
className="inline-flex h-7 w-7 items-center justify-center rounded-md text-[var(--muted-foreground)] transition-colors hover:bg-[var(--muted)]/40 hover:text-[var(--foreground)]"
>
<ChevronLeft className="h-3.5 w-3.5" />
</button>
</Tooltip>
</div>
{book && (
<div className="px-1">
<div
className="line-clamp-2 text-sm font-semibold text-[var(--foreground)]"
title={book.title || t("Untitled book")}
>
{book.title || t("Untitled book")}
</div>
<div className="mt-0.5 text-[10px] uppercase tracking-wider text-[var(--muted-foreground)]">
{t(book.status)} ·{" "}
{t("{{count}} chapters", { count: book.chapter_count || 0 })}
</div>
</div>
)}
{book && (
<a
href={bookApi.exportUrl(book.id)}
download
className="dt-quiet-button h-8 justify-start text-xs font-medium"
>
<Download className="h-3.5 w-3.5" />
{t("Export Markdown")}
</a>
)}
{/* Rebuild deletes every page AND resets progress — reading position,
bookmarks, quiz history, notes and in-place edits all go. It needs the
same two-step confirmation the library uses for deleting a book. */}
{onRebuild && (
<div className="space-y-1">
<button
type="button"
onClick={() => {
if (confirmRebuild) {
setConfirmRebuild(false);
onRebuild();
} else {
setConfirmRebuild(true);
}
}}
onBlur={() => setConfirmRebuild(false)}
disabled={rebuilding}
className={`inline-flex w-full items-center justify-center gap-1.5 rounded-md border px-2 py-1.5 text-xs font-medium disabled:opacity-60 ${
confirmRebuild
? "border-rose-400/60 bg-rose-500/10 text-rose-600 dark:text-rose-300"
: "border-[var(--border)] bg-[var(--card)] text-[var(--muted-foreground)] hover:border-[var(--primary)]/40 hover:text-[var(--primary)]"
}`}
>
{rebuilding ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<RotateCcw className="h-3.5 w-3.5" />
)}
{rebuilding
? t("Rebuilding…")
: confirmRebuild
? t("Click again to rebuild")
: t("Rebuild book")}
</button>
{confirmRebuild && !rebuilding && (
<p className="px-1 text-[10px] leading-snug text-[var(--muted-foreground)]">
{t(
"Replaces every generated chapter and clears your reading progress, bookmarks, notes and edits.",
)}
</p>
)}
</div>
)}
<section className="flex-1 overflow-y-auto">
{pages.length === 0 ? (
<div className="rounded-xl bg-[var(--background)] px-3 py-3 text-xs text-[var(--muted-foreground)]">
{t("Pages will appear here once the spine is confirmed.")}
</div>
) : (
<>
<div className="mb-2 text-[11px] font-semibold uppercase tracking-[0.16em] text-[var(--muted-foreground)]">
{t("Chapters")}
</div>
<ul className="space-y-1">
{pages.map((page) => {
const active = page.id === selectedPageId;
const isOverview = page.content_type === "overview";
const bookmarked = bookmarkedPageIds?.includes(page.id) ?? false;
const hint = [
page.title || t("Untitled"),
t(STATUS_LABEL[page.status] || page.status),
bookmarked ? t("Bookmarked") : null,
]
.filter(Boolean)
.join(" · ");
return (
<li key={page.id} className="[&>span]:w-full">
<Tooltip label={hint}>
<button
onClick={() => onSelectPage?.(page.id)}
className={`flex w-full items-start justify-between gap-2 rounded-md py-1.5 pr-2 text-left text-xs ${
page.parent_page_id ? "pl-5" : "pl-2"
} ${
active
? "bg-[var(--primary)]/15 text-[var(--foreground)]"
: "text-[var(--muted-foreground)] hover:bg-[var(--muted)]/40 hover:text-[var(--foreground)]"
}`}
>
<span className="flex min-w-0 items-start gap-1.5">
{isOverview && (
<Compass className="mt-[1px] h-3 w-3 shrink-0 text-[var(--primary)]" />
)}
<span className="line-clamp-2">
{page.title || t("Untitled")}
</span>
</span>
<span className="flex shrink-0 items-center gap-1.5">
{bookmarked && (
<Bookmark
className="h-3 w-3 fill-[var(--primary)] text-[var(--primary)]"
aria-hidden="true"
/>
)}
{/* The row hint carries status and bookmark details;
the mark stays compact in the chapter list. */}
<span
className={`inline-flex items-center ${
page.status === "ready" &&
visitedPageIds?.includes(page.id)
? "opacity-45"
: ""
}`}
>
<ActivityMark
{...(PAGE_MARK[page.status] || RESTING_MARK)}
className="mt-[1px]"
/>
</span>
</span>
</button>
</Tooltip>
</li>
);
})}
</ul>
</>
)}
</section>
</aside>
);
}