Files
DeepTutor/web/components/sidebar/UtilitySidebar.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

164 lines
5.8 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import { activeWorkspaceId, navigateTask } from "@/lib/workspace-scope";
import { sessionWorkspaceId } from "@/lib/session-api";
import { useCallback, useEffect, useRef, useState } from "react";
import { useRouter } from "next/navigation";
import { useTranslation } from "react-i18next";
import { LayoutSidebarShell } from "@/features/ui-layout/slots";
import { LogoutButton } from "@/components/auth/LogoutButton";
import { AdminLink } from "@/components/auth/AdminLink";
import { ProfileLink } from "@/components/auth/ProfileLink";
import { useAppShell } from "@/context/AppShellContext";
import {
deleteSession,
listAllSessions,
updateSessionOrganization,
updateSessionTitle,
type SessionOrganizationPatch,
type SessionSummary,
} from "@/lib/session-api";
import {
fetchReadingCollectionIndex,
type ReadingCollectionLabel,
} from "@/lib/reading-workspace-api";
import {
fetchMasteryTopicIndex,
type MasteryTopicLabel,
} from "@/lib/learning-api";
import { sessionRoute } from "@/lib/mastery-session";
import { subscribeSessionChanges } from "@/lib/session-events";
export default function UtilitySidebar() {
const { t } = useTranslation();
const router = useRouter();
const { activeSessionId, setActiveSessionId } = useAppShell();
const [sessions, setSessions] = useState<SessionSummary[]>([]);
const [masteryTopics, setMasteryTopics] = useState<MasteryTopicLabel[]>([]);
const [readingCollections, setReadingCollections] = useState<
ReadingCollectionLabel[]
>([]);
const [loadingSessions, setLoadingSessions] = useState(false);
const hasLoadedSessionsRef = useRef(false);
const refreshSessions = useCallback(async () => {
if (!hasLoadedSessionsRef.current) {
setLoadingSessions(true);
}
try {
// Labels only name a heading, so losing them costs grouping, not the list.
const [nextSessions, nextTopics, nextCollections] =
await Promise.all([
listAllSessions({ force: true }),
fetchMasteryTopicIndex().catch(() => [] as MasteryTopicLabel[]),
fetchReadingCollectionIndex().catch(() => [] as ReadingCollectionLabel[]),
]);
setSessions(nextSessions);
setMasteryTopics(nextTopics);
setReadingCollections(nextCollections);
hasLoadedSessionsRef.current = true;
} catch (error) {
console.error("Failed to load sessions", error);
} finally {
setLoadingSessions(false);
}
}, []);
useEffect(() => {
void refreshSessions();
}, [refreshSessions]);
// A conversation can be archived, restored or deleted from a route the
// sidebar knows nothing about — Settings › Archive being the reason this
// exists. Without it the list keeps hiding a conversation that was just
// restored two panes away.
useEffect(
() => subscribeSessionChanges(() => void refreshSessions()),
[refreshSessions],
);
// A study conversation opens on its own path — see ``sessionRoute``.
const handleSelectSession = useCallback(
async (sessionId: string) => {
setActiveSessionId(sessionId);
const session = sessions.find((item) => item.session_id === sessionId);
navigateTask(session ? sessionRoute(session) : `/chat/${sessionId}`, router.push);
},
[router, sessions, setActiveSessionId],
);
const handleRenameSession = useCallback(
async (sessionId: string, title: string) => {
const updated = await updateSessionTitle(sessionId, title, sessionWorkspaceId(sessions.find(item => item.session_id === sessionId)));
setSessions((prev) =>
prev.map((session) =>
session.session_id === sessionId
? {
...session,
title: updated.title,
updated_at: updated.updated_at,
}
: session,
),
);
},
[sessions],
);
const handleDeleteSession = useCallback(
async (sessionId: string) => {
if (!window.confirm(t("Permanently delete this chat and its tutor threads? This cannot be undone."))) return;
await deleteSession(sessionId, sessionWorkspaceId(sessions.find(item => item.session_id === sessionId)));
setSessions((prev) =>
prev.filter((session) => session.session_id !== sessionId),
);
if (activeSessionId === sessionId) {
setActiveSessionId(null);
}
},
[activeSessionId, setActiveSessionId, t, sessions],
);
const handleOrganizeSession = useCallback(
async (sessionId: string, patch: SessionOrganizationPatch) => {
const updated = await updateSessionOrganization(sessionId, patch, sessionWorkspaceId(sessions.find(item => item.session_id === sessionId)));
// A conversation moved to another workspace has left this list.
const movedAway = "workspace_id" in patch &&
(updated.preferences?.workspace_id || "") !== activeWorkspaceId();
setSessions((previous) =>
movedAway
? previous.filter((session) => session.session_id !== sessionId)
: previous.map((session) =>
session.session_id === sessionId
? { ...session, updated_at: updated.updated_at, preferences: updated.preferences }
: session,
),
);
},
[sessions],
);
return (
<LayoutSidebarShell
showSessions
sessions={sessions}
masteryTopics={masteryTopics}
readingCollections={readingCollections}
activeSessionId={activeSessionId}
loadingSessions={loadingSessions}
onNewChat={() => setActiveSessionId(null)}
onSelectSession={handleSelectSession}
onRenameSession={handleRenameSession}
onDeleteSession={handleDeleteSession}
onOrganizeSession={handleOrganizeSession}
footerSlot={(collapsed) => (
<>
<ProfileLink collapsed={collapsed} />
<AdminLink collapsed={collapsed} />
<LogoutButton collapsed={collapsed} />
</>
)}
/>
);
}