Files
DeepTutor/web/components/SessionList.tsx
Bingxi Zhao (Frank) 53436f8190 feat(reading): collections as colored folders; rework reader interactions
Collections vs workspaces: a workspace holds many collections, and a
collection's files and reading conversations live in the workspace it
was created in. The library now shows collections as folders grouped
under their workspace, and creating one asks which workspace it goes
into.

- Folder page at /learning/reading/folders/[folderId]: breadcrumb,
  files, the collection's conversations, and "Start reading" that opens
  the whole folder in the reader. The reader's back link returns to it.
- Collections carry a user-picked color (new `color` column, migrated);
  tones are --folder-* tokens with dark variants.
- One shared "New collection" / "Edit collection" dialog with consistent
  type sizes.
- Selection popover gains AI actions; results land as cards at the foot
  of the companion. Read-aloud is a single header toggle.
- The three ⋯ menus (collection, document, conversation) merge into one.
- Outline toggle moves to the left and starts closed; navigator has
  Outline / Annotations tabs.
- Reading conversations live in the main sidebar (with a page-shaped idle
  mark) instead of a history dropdown in the companion.
- Fix sidebar rows never showing a selected state
  (bg-[var(--background)]/60 compiled to nothing).
- Composer floats over the conversation (no divider, bottom fade); the
  toolbar wraps by content instead of splitting under 420px.
2026-09-23 18:59:17 +08:00

372 lines
14 KiB
TypeScript

"use client";
import { Check, Pencil, Trash2 } from "lucide-react";
import { useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { type SessionSummary } from "@/lib/session-api";
import { normalizeMessageContent, truncateText } from "@/lib/message-content";
import {
deriveSessionMark,
sessionKindOf,
SessionAvatar,
} from "@/components/sidebar/SessionAvatar";
import { useUnreadSessions } from "@/lib/session-unread";
import {
formatRelativeTime,
getDayGroupKey,
type DayGroupKey,
} from "@/lib/relative-time";
import { isPlaceholderSessionTitle } from "@/lib/session-title";
interface SessionListProps {
sessions: SessionSummary[];
activeSessionId: string | null;
loading?: boolean;
compact?: boolean;
onSelect: (sessionId: string) => void | Promise<void>;
onRename: (sessionId: string, title: string) => void | Promise<void>;
onDelete: (sessionId: string) => void | Promise<void>;
}
export default function SessionList({
sessions,
activeSessionId,
loading = false,
compact = false,
onSelect,
onRename,
onDelete,
}: SessionListProps) {
const { t, i18n } = useTranslation();
const [editingId, setEditingId] = useState<string | null>(null);
const [draftTitle, setDraftTitle] = useState("");
// Reads the set only. `WorkspaceSidebar` owns keeping it current, because
// it holds the runtime status map this is derived from and it outlives every
// list that renders from it.
const unread = useUnreadSessions();
// The sentinel the backend writes when a session is created and not
// yet renamed by the LLM title generator. We swap it for a localized
// "New chat" string with a breathing animation so the sidebar shows
// something "alive" while the title is being generated in the
// background instead of a literal English sentinel.
const placeholderLabel = t("New chat");
// The group-key tokens stay stable; only the translated labels change.
const groupLabels = useMemo<Record<DayGroupKey, string>>(
() => ({
today: t("Today"),
yesterday: t("Yesterday"),
last_7_days: t("Last 7 days"),
earlier: t("Earlier"),
}),
[t],
);
const grouped = useMemo(() => {
const buckets = new Map<DayGroupKey, SessionSummary[]>();
for (const session of sessions) {
const key = getDayGroupKey(session.updated_at);
const current = buckets.get(key) ?? [];
current.push(session);
buckets.set(key, current);
}
return Array.from(buckets.entries());
}, [sessions]);
const startEdit = (session: SessionSummary) => {
setEditingId(session.session_id);
setDraftTitle(session.title);
};
const commitEdit = async () => {
if (!editingId) return;
const nextTitle = draftTitle.trim();
if (!nextTitle) {
setEditingId(null);
setDraftTitle("");
return;
}
await onRename(editingId, nextTitle);
setEditingId(null);
setDraftTitle("");
};
if (loading) {
if (compact) {
return (
<div className="space-y-1.5 px-2 py-1">
{[1, 2, 3].map((i) => (
<div
key={i}
className="h-4 w-3/4 animate-pulse rounded bg-muted/40"
/>
))}
</div>
);
}
return (
<div className="space-y-2 px-1.5 py-2">
{[1, 2, 3].map((i) => (
<div
key={i}
className="h-10 animate-pulse rounded-md bg-muted/60"
/>
))}
</div>
);
}
if (sessions.length === 0) {
if (compact) {
return (
<div className="px-3 py-4 text-center text-[11px] text-muted-foreground/70">
{t("No conversations yet")}
</div>
);
}
return (
<div className="px-3 py-4 text-center text-[11px] text-muted-foreground/70">
{t("No conversations yet")}
</div>
);
}
/* ---- Compact sidebar style (standalone chat history region) ---- */
if (compact) {
return (
<div className="py-0.5">
{sessions.map((session) => {
const active = activeSessionId === session.session_id;
const isEditing = editingId === session.session_id;
return (
<div
key={session.session_id}
onClick={() => void onSelect(session.session_id)}
onKeyDown={(event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
void onSelect(session.session_id);
}
}}
role="button"
tabIndex={0}
className={`group flex items-center gap-2 rounded-lg px-2.5 py-1.5 transition-colors ${
active
? "bg-background/50 text-[var(--foreground)]"
: "text-[var(--foreground)] hover:bg-background/40"
}`}
>
<SessionAvatar
sessionId={session.session_id}
mark={deriveSessionMark(session, undefined, unread)}
kind={sessionKindOf(session)}
className={
session.status === "running" ? "opacity-100" : "opacity-80"
}
/>
{isEditing ? (
<input
value={draftTitle}
autoFocus
onChange={(event) => setDraftTitle(event.target.value)}
onBlur={() => void commitEdit()}
onKeyDown={(event) => {
if (event.key === "Enter") void commitEdit();
if (event.key === "Escape") {
setEditingId(null);
setDraftTitle("");
}
}}
onClick={(event) => event.stopPropagation()}
className="min-w-0 flex-1 rounded border border-[var(--border)] bg-[var(--background)] px-1.5 py-px text-[12px] text-[var(--foreground)] outline-none focus:ring-1 focus:ring-primary/40"
/>
) : isPlaceholderSessionTitle(session.title) ? (
<span
className={`dt-breathing-text min-w-0 flex-1 truncate text-[13px] italic text-[var(--foreground)] ${active ? "font-medium" : ""}`}
>
{placeholderLabel}
</span>
) : (
<span
className={`min-w-0 flex-1 truncate text-[13px] ${active ? "font-medium" : ""}`}
>
{session.title}
</span>
)}
<div className="flex shrink-0 items-center gap-px opacity-0 transition-opacity group-hover:opacity-100">
{isEditing ? (
<button
onClick={(event) => {
event.stopPropagation();
void commitEdit();
}}
className="rounded p-0.5 text-[var(--muted-foreground)] hover:text-[var(--foreground)]"
aria-label={t("Save title")}
>
<Check size={10} />
</button>
) : (
<button
onClick={(event) => {
event.stopPropagation();
startEdit(session);
}}
className="rounded p-0.5 text-[var(--muted-foreground)] hover:text-[var(--foreground)]"
aria-label={t("Rename chat")}
>
<Pencil size={10} />
</button>
)}
<button
onClick={(event) => {
event.stopPropagation();
void onDelete(session.session_id);
}}
className="rounded p-0.5 text-[var(--muted-foreground)] hover:text-[var(--destructive)]"
aria-label={t("Delete chat")}
>
<Trash2 size={10} />
</button>
</div>
</div>
);
})}
</div>
);
}
/* ---- Classic style ---- */
return (
<div className="space-y-4">
{grouped.map(([key, items]) => (
<div key={key}>
<div className="mb-1.5 px-2 text-[11px] font-semibold uppercase tracking-widest text-[var(--muted-foreground)]">
{groupLabels[key]}
</div>
<div className="divide-y divide-border/45 overflow-hidden rounded-lg border border-border/45 bg-card/50">
{items.map((session) => {
const active = activeSessionId === session.session_id;
const isEditing = editingId === session.session_id;
return (
<div
key={session.session_id}
onClick={() => void onSelect(session.session_id)}
onKeyDown={(event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
void onSelect(session.session_id);
}
}}
role="button"
tabIndex={0}
className={`group relative w-full px-3 py-2.5 text-left transition-colors duration-150 ${
active
? "bg-background/70 text-[var(--foreground)]"
: "text-[var(--foreground)] hover:bg-background/50"
}`}
>
{active && (
<span className="absolute left-0 top-1/2 h-5 w-[3px] -translate-y-1/2 rounded-r-full bg-[var(--primary)]" />
)}
<div className="flex items-start gap-1.5">
<div className="min-w-0 flex-1">
{isEditing ? (
<input
value={draftTitle}
autoFocus
onChange={(event) =>
setDraftTitle(event.target.value)
}
onBlur={() => void commitEdit()}
onKeyDown={(event) => {
if (event.key === "Enter") void commitEdit();
if (event.key === "Escape") {
setEditingId(null);
setDraftTitle("");
}
}}
onClick={(event) => event.stopPropagation()}
className="w-full rounded border border-[var(--border)] bg-[var(--background)] px-2 py-0.5 text-[12px] text-[var(--foreground)] outline-none focus:ring-1 focus:ring-primary/40"
/>
) : (
<div className="flex items-center">
{isPlaceholderSessionTitle(session.title) ? (
<span
className={`dt-breathing-text line-clamp-1 min-w-0 flex-1 text-[12px] italic leading-snug text-[var(--foreground)] ${
active ? "font-medium" : "font-normal"
}`}
>
{placeholderLabel}
</span>
) : (
<span
className={`line-clamp-1 min-w-0 flex-1 text-[12px] leading-snug ${
active ? "font-medium" : "font-normal"
}`}
>
{session.title}
</span>
)}
</div>
)}
{!isEditing && (
<div className="mt-0.5 line-clamp-1 text-[11px] leading-tight text-[var(--muted-foreground)]">
{truncateText(
normalizeMessageContent(session.last_message),
120,
) ||
formatRelativeTime(
session.updated_at,
i18n.language,
)}
</div>
)}
</div>
<div className="flex shrink-0 items-center gap-0.5 pt-px opacity-0 transition-opacity group-hover:opacity-100">
{isEditing ? (
<button
onClick={(event) => {
event.stopPropagation();
void commitEdit();
}}
className="rounded p-0.5 text-[var(--muted-foreground)] hover:bg-[var(--background)] hover:text-[var(--foreground)]"
aria-label={t("Save title")}
>
<Check size={12} />
</button>
) : (
<button
onClick={(event) => {
event.stopPropagation();
startEdit(session);
}}
className="rounded p-0.5 text-[var(--muted-foreground)] hover:bg-[var(--background)] hover:text-[var(--foreground)]"
aria-label={t("Rename chat")}
>
<Pencil size={11} />
</button>
)}
<button
onClick={(event) => {
event.stopPropagation();
void onDelete(session.session_id);
}}
className="rounded p-0.5 text-[var(--muted-foreground)] hover:bg-[var(--background)] hover:text-[var(--destructive)]"
aria-label={t("Delete chat")}
>
<Trash2 size={11} />
</button>
</div>
</div>
</div>
);
})}
</div>
</div>
))}
</div>
);
}