-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathSidebarShell.tsx
More file actions
426 lines (408 loc) · 14.9 KB
/
Copy pathSidebarShell.tsx
File metadata and controls
426 lines (408 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
"use client";
import Image from "next/image";
import Link from "next/link";
import { usePathname, useRouter } from "next/navigation";
import { useEffect, useState, type ReactNode } from "react";
import { useAppShell } from "@/context/AppShellContext";
import {
BookOpen,
Bot,
BookText,
Brain,
ChevronDown,
Github,
GraduationCap,
LayoutGrid,
Library,
MessageSquare,
PanelLeftClose,
PanelLeftOpen,
PenLine,
Settings,
type LucideIcon,
} from "lucide-react";
import { useTranslation } from "react-i18next";
import SessionList from "@/components/SessionList";
import { VersionBadge } from "@/components/sidebar/VersionBadge";
import type { SessionSummary } from "@/lib/session-api";
import { Tooltip } from "@/components/ui/Tooltip";
interface NavEntry {
href: string;
label: string;
icon: LucideIcon;
tooltipKey?: string;
}
const PRIMARY_NAV: NavEntry[] = [
{
href: "/chat",
label: "Chat",
icon: MessageSquare,
tooltipKey: "Chat tooltip",
},
{
href: "/agents",
label: "TutorBot",
icon: Bot,
tooltipKey: "TutorBot tooltip",
},
{
href: "/co-writer",
label: "Co-Writer",
icon: PenLine,
tooltipKey: "Co-Writer tooltip",
},
{ href: "/book", label: "Book", icon: Library, tooltipKey: "Book tooltip" },
{
href: "/knowledge",
label: "Knowledge",
icon: BookOpen,
tooltipKey: "Knowledge tooltip",
},
{
href: "/learning",
label: "Learning",
icon: GraduationCap,
tooltipKey: "Learning tooltip",
},
{
href: "/space",
label: "Space",
icon: LayoutGrid,
tooltipKey: "Space tooltip",
},
{
href: "/memory",
label: "Memory",
icon: Brain,
tooltipKey: "Memory tooltip",
},
];
const SECONDARY_NAV: NavEntry[] = [
{ href: "/settings", label: "Settings", icon: Settings },
];
const GITHUB_REPO_URL = "https://github.com/HKUDS/DeepTutor";
const DOCS_URL = "https://deeptutor.info/";
const RECENTS_COLLAPSED_KEY = "deeptutor.sidebar.recentsCollapsed";
interface SidebarShellProps {
sessions?: SessionSummary[];
activeSessionId?: string | null;
loadingSessions?: boolean;
showSessions?: boolean;
/** Clicking the Chat nav item resets to a fresh session via this handler. */
onNewChat?: () => void;
onSelectSession?: (sessionId: string) => void | Promise<void>;
onRenameSession?: (sessionId: string, title: string) => void | Promise<void>;
onDeleteSession?: (sessionId: string) => void | Promise<void>;
/**
* 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 SidebarShell({
sessions = [],
activeSessionId = null,
loadingSessions = false,
showSessions = false,
onNewChat,
onSelectSession,
onRenameSession,
onDeleteSession,
footerSlot,
}: SidebarShellProps) {
const pathname = usePathname();
const router = useRouter();
const { t } = useTranslation();
const { sidebarCollapsed: collapsed, setSidebarCollapsed: setCollapsed } =
useAppShell();
const renderedFooter =
typeof footerSlot === "function" ? footerSlot(collapsed) : footerSlot;
const [recentsCollapsed, setRecentsCollapsed] = useState(false);
// Hydrate Recents collapse from localStorage after first render to stay SSR-safe.
useEffect(() => {
if (typeof window === "undefined") return;
// eslint-disable-next-line react-hooks/set-state-in-effect
setRecentsCollapsed(
window.localStorage.getItem(RECENTS_COLLAPSED_KEY) === "1",
);
}, []);
const toggleRecents = () => {
setRecentsCollapsed((prev) => {
const next = !prev;
if (typeof window !== "undefined") {
window.localStorage.setItem(RECENTS_COLLAPSED_KEY, next ? "1" : "0");
}
return next;
});
};
const handleChatClick = (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();
onNewChat?.();
router.push("/chat");
};
/* ---- Collapsed state ---- */
if (collapsed) {
return (
<aside className="group/sb relative flex h-screen w-[60px] shrink-0 flex-col items-center bg-[var(--secondary)] py-3 transition-all duration-200">
{/* Header: logo + collapse toggle (toggle replaces logo on hover) */}
<div className="relative mb-2 flex h-9 w-9 items-center justify-center">
<Link
href="/"
aria-label="DeepTutor"
className="flex items-center justify-center transition-opacity duration-150 group-hover/sb:opacity-0"
>
<Image
src="/logo.png"
alt="DeepTutor"
width={22}
height={22}
className="h-[22px] w-[22px] rounded-md"
/>
</Link>
<button
onClick={() => setCollapsed(false)}
className="absolute inset-0 flex items-center justify-center rounded-lg text-[var(--muted-foreground)] opacity-0 transition-all duration-150 hover:bg-[var(--background)]/60 hover:text-[var(--foreground)] group-hover/sb:opacity-100"
aria-label={t("Expand sidebar")}
>
<PanelLeftOpen size={16} />
</button>
</div>
{/* Primary nav */}
<nav className="mt-1 flex w-full flex-col items-center gap-1 px-1.5">
{PRIMARY_NAV.map((item) => {
const active = pathname.startsWith(item.href);
const description = item.tooltipKey
? t(item.tooltipKey)
: undefined;
return (
<Tooltip
key={item.href}
label={t(item.label)}
description={description}
side="right"
>
<Link
href={item.href}
onClick={item.href === "/chat" ? handleChatClick : undefined}
aria-label={t(item.label)}
className={`relative flex h-9 w-9 items-center justify-center rounded-xl transition-all duration-150 ${
active
? "bg-[var(--background)]/80 text-[var(--foreground)] shadow-sm"
: "text-[var(--muted-foreground)] hover:bg-[var(--background)]/50 hover:text-[var(--foreground)]"
}`}
>
{active && (
<span className="absolute -left-1.5 top-1/2 h-5 w-[3px] -translate-y-1/2 rounded-full bg-[var(--foreground)]/80" />
)}
<item.icon size={18} strokeWidth={active ? 2 : 1.6} />
</Link>
</Tooltip>
);
})}
</nav>
<div className="flex-1" />
{/* Secondary nav + footer */}
<div className="flex w-full flex-col items-center gap-1 px-1.5">
<div className="my-1 h-px w-7 bg-[var(--border)]/40" />
{SECONDARY_NAV.map((item) => {
const active = pathname.startsWith(item.href);
return (
<Link
key={item.href}
href={item.href}
title={t(item.label) as string}
className={`relative flex h-9 w-9 items-center justify-center rounded-xl transition-all duration-150 ${
active
? "bg-[var(--background)]/80 text-[var(--foreground)] shadow-sm"
: "text-[var(--muted-foreground)] hover:bg-[var(--background)]/50 hover:text-[var(--foreground)]"
}`}
>
{active && (
<span className="absolute -left-1.5 top-1/2 h-5 w-[3px] -translate-y-1/2 rounded-full bg-[var(--foreground)]/80" />
)}
<item.icon size={18} strokeWidth={active ? 2 : 1.6} />
</Link>
);
})}
{renderedFooter}
<a
href={DOCS_URL}
target="_blank"
rel="noreferrer noopener"
title={t("Docs") as string}
aria-label={t("Docs") as string}
className="mt-1 flex h-9 w-9 items-center justify-center rounded-xl text-[var(--muted-foreground)]/70 transition-colors hover:bg-[var(--background)]/50 hover:text-[var(--foreground)]"
>
<BookText size={15} strokeWidth={1.6} />
</a>
<a
href={GITHUB_REPO_URL}
target="_blank"
rel="noreferrer noopener"
title="GitHub"
aria-label="GitHub"
className="flex h-9 w-9 items-center justify-center rounded-xl text-[var(--muted-foreground)]/70 transition-colors hover:bg-[var(--background)]/50 hover:text-[var(--foreground)]"
>
<Github size={15} strokeWidth={1.6} />
</a>
<VersionBadge collapsed />
</div>
</aside>
);
}
/* ---- Expanded state ---- */
return (
<aside className="flex w-[220px] h-screen shrink-0 flex-col bg-[var(--secondary)] transition-all duration-200">
{/* Header: logo + collapse toggle */}
<div className="flex h-14 items-center justify-between px-4">
<Link href="/" className="group flex items-center gap-1.5">
<Image
src="/logo.png"
alt="DeepTutor"
width={22}
height={22}
className="h-[22px] w-[22px] transition-transform duration-200 group-hover:scale-105"
/>
<Image
src="/banner.png"
alt="DeepTutor"
width={897}
height={236}
priority
className="h-[22px] w-auto transition-transform duration-200 group-hover:scale-105"
/>
</Link>
<button
onClick={() => setCollapsed(true)}
className="rounded-md p-1 text-[var(--muted-foreground)] transition-colors hover:text-[var(--foreground)]"
aria-label={t("Collapse sidebar")}
>
<PanelLeftClose size={15} />
</button>
</div>
{/* Primary nav */}
<nav className="px-2 pt-1">
<div className="space-y-px">
{PRIMARY_NAV.map((item) => {
const active = pathname.startsWith(item.href);
return (
<Link
key={item.href}
href={item.href}
onClick={item.href === "/chat" ? handleChatClick : undefined}
className={`flex items-center gap-2.5 rounded-lg px-3 py-2 text-[13.5px] transition-colors ${
active
? "bg-[var(--background)]/70 font-medium text-[var(--foreground)]"
: "text-[var(--muted-foreground)] hover:bg-[var(--background)]/50 hover:text-[var(--foreground)]"
}`}
>
<item.icon size={16} strokeWidth={active ? 1.9 : 1.5} />
<span>{t(item.label)}</span>
</Link>
);
})}
</div>
</nav>
{/* Chat history — its own region below the nav, takes remaining height */}
{showSessions && onSelectSession && onRenameSession && onDeleteSession ? (
<section
className={`mt-4 flex min-h-0 flex-col ${
recentsCollapsed ? "" : "flex-1"
}`}
>
<button
type="button"
onClick={toggleRecents}
className="group/recents mx-2 flex items-center justify-between rounded-md px-2 py-1 text-left text-[11.5px] font-normal text-[var(--muted-foreground)]/60 transition-colors hover:bg-[var(--background)]/40 hover:text-[var(--muted-foreground)]"
aria-expanded={!recentsCollapsed}
aria-label={
recentsCollapsed
? (t("Show recents") as string)
: (t("Hide recents") as string)
}
>
<span>{t("Recents")}</span>
<ChevronDown
size={13}
strokeWidth={1.7}
className={`transition-all duration-200 ${
recentsCollapsed
? "-rotate-90 opacity-60"
: "rotate-0 opacity-0 group-hover/recents:opacity-60"
}`}
/>
</button>
{!recentsCollapsed && (
<div className="min-h-0 flex-1 overflow-y-auto px-2 pb-2 pt-0.5">
<SessionList
sessions={sessions}
activeSessionId={activeSessionId}
loading={loadingSessions}
onSelect={onSelectSession}
onRename={onRenameSession}
onDelete={onDeleteSession}
compact
/>
</div>
)}
</section>
) : null}
{/* When recents is collapsed or unavailable, fill the gap above the footer. */}
{(!showSessions ||
!onSelectSession ||
!onRenameSession ||
!onDeleteSession ||
recentsCollapsed) && <div className="flex-1" />}
{/* Secondary nav + footer */}
<div className="border-t border-[var(--border)]/40 px-2 py-2">
{SECONDARY_NAV.map((item) => {
const active = pathname.startsWith(item.href);
return (
<Link
key={item.href}
href={item.href}
className={`flex items-center gap-2.5 rounded-lg px-3 py-2 text-[13.5px] transition-colors ${
active
? "bg-[var(--background)]/70 font-medium text-[var(--foreground)]"
: "text-[var(--muted-foreground)] hover:bg-[var(--background)]/50 hover:text-[var(--foreground)]"
}`}
>
<item.icon size={16} strokeWidth={active ? 1.9 : 1.5} />
<span>{t(item.label)}</span>
</Link>
);
})}
{renderedFooter}
<div className="mt-0.5 flex items-center gap-0.5">
<VersionBadge />
<a
href={DOCS_URL}
target="_blank"
rel="noreferrer noopener"
title={t("Docs") as string}
aria-label={t("Docs") as string}
className="flex h-7 w-7 shrink-0 items-center justify-center rounded-md text-[var(--muted-foreground)]/55 transition-colors hover:bg-[var(--background)]/50 hover:text-[var(--muted-foreground)]"
>
<BookText size={13} strokeWidth={1.7} />
</a>
<a
href={GITHUB_REPO_URL}
target="_blank"
rel="noreferrer noopener"
title="GitHub"
aria-label="GitHub"
className="flex h-7 w-7 shrink-0 items-center justify-center rounded-md text-[var(--muted-foreground)]/55 transition-colors hover:bg-[var(--background)]/50 hover:text-[var(--muted-foreground)]"
>
<Github size={13} strokeWidth={1.7} />
</a>
</div>
</div>
</aside>
);
}