"use client";
// Classic UI snapshot — web/components/sidebar/SidebarShell.tsx as it ships on
// dev (frozen at 3cbecb2). Do not evolve this copy; the modern shell lives
// in components/sidebar/SidebarShell.tsx.
import { navigateTask } from "@/lib/workspace-scope";
import Image from "next/image";
import dynamic from "next/dynamic";
import Link from "next/link";
import { usePathname, useRouter } from "next/navigation";
import {
useCallback,
useEffect,
useRef,
useState,
type ReactNode,
type CSSProperties,
} from "react";
import { useAppShell } from "@/context/AppShellContext";
import { PanelLeftClose, PanelLeftOpen } from "lucide-react";
import { useTranslation } from "react-i18next";
import { useChatWorkspaces } from "@/hooks/useChatWorkspaces";
import SessionList from "@/components/SessionList";
import { useSidebarDrawer } from "@/components/layout/AppShell";
import { useDevice } from "@/hooks/useDevice";
import { VersionBadge } from "@/components/sidebar/VersionBadge";
import type {
SessionOrganizationPatch,
SessionSummary,
} from "@/lib/session-api";
import type { MasteryTopicLabel } from "@/lib/learning-api";
import type { ReadingCollectionLabel } from "@/lib/reading-workspace-api";
import { useSidebarResize } from "@/hooks/useSidebarResize";
import { SidebarHome, SidebarNav } from "@/components/sidebar/SidebarNav";
import { SECONDARY_NAV, isNavActive } from "@/components/sidebar/nav-entries";
import Tooltip from "@/shared/ui/Tooltip";
import {
mergeManualOrder,
readSessionOrder,
writeSessionOrder,
} from "@/lib/sidebar-layout";
// Session data arrives after mount; defer its organization UI with it so
// every workspace route does not download it as part of the initial shell.
const OrganizedSessionList = dynamic(
() => import("@/components/sessions/OrganizedSessionList"),
{
ssr: false,
loading: () => (
{[1, 2, 3].map((i) => (
))}
),
},
);
export interface SidebarShellProps {
sessions?: SessionSummary[];
activeSessionId?: string | null;
/** Conversations the caller is streaming right now; they sort to the top. */
liveSessionIds?: ReadonlySet;
loadingSessions?: boolean;
showSessions?: boolean;
/** Clicking the Chat nav item resets to a fresh session via this handler. */
onNewChat?: () => void;
onSelectSession?: (sessionId: string) => void | Promise;
onRenameSession?: (sessionId: string, title: string) => void | Promise;
onDeleteSession?: (sessionId: string) => void | Promise;
/** Topic labels for grouping mastery study conversations under their path. */
masteryTopics?: MasteryTopicLabel[];
/** Collection labels for grouping reading conversations under their shelf. */
readingCollections?: ReadingCollectionLabel[];
onOrganizeSession?: (
sessionId: string,
patch: SessionOrganizationPatch,
) => void | Promise;
/**
* Footer content rendered below the nav. Pass a render function to receive
* the current ``collapsed`` state so footer items (e.g. Admin / Sign out) can
* switch to their icon-only variant when the rail is collapsed.
*/
footerSlot?: ReactNode | ((collapsed: boolean) => ReactNode);
}
export function ClassicSidebarShell({
sessions = [],
activeSessionId = null,
liveSessionIds,
loadingSessions = false,
showSessions = false,
onNewChat,
onSelectSession,
onRenameSession,
onDeleteSession,
masteryTopics = [],
readingCollections = [],
onOrganizeSession,
footerSlot,
}: SidebarShellProps) {
const pathname = usePathname();
const router = useRouter();
const { t } = useTranslation();
const { sidebarCollapsed, setSidebarCollapsed: setCollapsed } = useAppShell();
const { isMobile } = useDevice();
const drawer = useSidebarDrawer();
const recentsScrollRef = useRef(null);
// One load for the whole column: the workspace groups head their own
// section with it and the row menus offer it as a move destination.
const { workspaces } = useChatWorkspaces();
// Inside the mobile drawer the icon-only rail is pointless — the panel is
// already hidden when you don't want it, so it always opens fully expanded
// regardless of the persisted desktop preference.
const collapsed = sidebarCollapsed && !isMobile;
const resize = useSidebarResize(collapsed || isMobile);
/** Dismiss the drawer on nav clicks that actually navigate in-place. */
const closeDrawerOnNav = (event: React.MouseEvent) => {
if (event.metaKey || event.ctrlKey || event.shiftKey || event.button === 1)
return;
drawer?.close();
};
const renderedFooter =
typeof footerSlot === "function" ? footerSlot(collapsed) : footerSlot;
// The order the learner dragged the history region into — conversation ids
// and group ids in one list, since the two are peers there. Like the
// collapse preference above it is per-machine view state, hydrated after
// mount.
const [sessionOrder, setSessionOrder] = useState([]);
const sessionOrderRef = useRef([]);
useEffect(() => {
const stored = readSessionOrder();
sessionOrderRef.current = stored;
// eslint-disable-next-line react-hooks/set-state-in-effect
setSessionOrder(stored);
}, []);
// A drag only ever speaks for the entries on screen, so it is merged into
// the stored order rather than replacing it.
const handleReorderSessions = useCallback((nextIds: string[]) => {
const merged = mergeManualOrder(sessionOrderRef.current, nextIds);
sessionOrderRef.current = merged;
setSessionOrder(merged);
writeSessionOrder(merged);
}, []);
const handleResetSessionOrder = useCallback(() => {
sessionOrderRef.current = [];
setSessionOrder([]);
writeSessionOrder([]);
}, []);
const handleHomeClick = (event: React.MouseEvent) => {
// Always reset to a fresh session (mirrors the old "New Chat" affordance);
// let modifier-clicks fall through to default Link behavior so middle-click
// open-in-new-tab still works.
if (event.metaKey || event.ctrlKey || event.shiftKey || event.button === 1)
return;
event.preventDefault();
drawer?.close();
onNewChat?.();
navigateTask("/chat", router.push);
};
// Everything the learner has, minus the archived and minus the tutor threads
// that render nested under the conversation that spawned them.
//
// Keep the complete index here. OrganizedSessionList limits the initial
// rendering and exposes older conversations through Show more.
const visibleSessions = sessions.filter(
(session) =>
!session.preferences?.archived && !session.preferences?.parent_session_id,
);
/* ---- Collapsed state ---- */
if (collapsed) {
return (
);
}
/* ---- Expanded state ---- */
return (
);
}