SessionSidebar.tsx
4.2 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
"use client";
import { useState } from "react";
import { MessageSquare, SquarePen, Trash2 } from "lucide-react";
import { cn } from "@/lib/utils";
import type { SessionItem } from "@/lib/auth";
interface SessionSidebarProps {
sessions: SessionItem[];
currentId: string | null;
loading?: boolean;
onSelect: (id: string) => void;
onCreate: () => void;
onDelete: (session: SessionItem) => void;
}
/** 相对时间展示,避免侧栏出现冗长的绝对时间戳。 */
function relativeTime(iso: string | null): string {
if (!iso) return "";
const then = new Date(iso).getTime();
if (Number.isNaN(then)) return "";
const diff = Date.now() - then;
const minute = 60_000;
const hour = 60 * minute;
const day = 24 * hour;
if (diff < minute) return "刚刚";
if (diff < hour) return `${Math.floor(diff / minute)} 分钟前`;
if (diff < day) return `${Math.floor(diff / hour)} 小时前`;
if (diff < 2 * day) return "昨天";
if (diff < 7 * day) return `${Math.floor(diff / day)} 天前`;
return new Date(iso).toLocaleDateString("zh-CN", { month: "numeric", day: "numeric" });
}
export default function SessionSidebar({
sessions,
currentId,
loading = false,
onSelect,
onCreate,
onDelete,
}: SessionSidebarProps) {
const [hoveredId, setHoveredId] = useState<string | null>(null);
return (
<aside className="w-[220px] shrink-0 bg-slate-50/80 border-r border-slate-200 flex flex-col">
<div className="p-3 border-b border-slate-200">
<button
onClick={onCreate}
className="w-full flex items-center justify-center gap-1.5 bg-blue-600 hover:bg-blue-700 text-white text-xs font-semibold py-2 rounded-lg transition"
>
<SquarePen className="w-3.5 h-3.5" />
新建会话
</button>
</div>
<div className="flex-1 overflow-y-auto scrollbar-thin p-2 space-y-0.5">
{loading && (
<div className="text-[11px] text-slate-400 text-center py-4">加载中…</div>
)}
{!loading && sessions.length === 0 && (
<div className="text-center py-8 px-3">
<MessageSquare className="w-8 h-8 mx-auto text-slate-300 mb-2" />
<p className="text-[11px] text-slate-400 leading-relaxed">
还没有会话
<br />
点上方按钮开始提问
</p>
</div>
)}
{sessions.map((session) => {
const active = session.id === currentId;
return (
<div
key={session.id}
onMouseEnter={() => setHoveredId(session.id)}
onMouseLeave={() => setHoveredId(null)}
onClick={() => onSelect(session.id)}
className={cn(
"group relative px-2.5 py-2 rounded-lg cursor-pointer transition",
active ? "bg-blue-50 text-blue-700" : "hover:bg-slate-200/60 text-slate-700"
)}
>
<div className="flex items-center gap-1.5 pr-5">
<MessageSquare
className={cn("w-3.5 h-3.5 shrink-0", active ? "text-blue-600" : "text-slate-400")}
/>
<span className="text-xs font-medium truncate">{session.title}</span>
</div>
<div className="flex items-center gap-2 mt-0.5 pl-5">
<span className="text-[10px] text-slate-400">{relativeTime(session.updatedAt)}</span>
{session.totalTurns > 0 && (
<span className="text-[10px] text-slate-400">{session.totalTurns} 轮</span>
)}
</div>
{/* 删除图标平时隐藏,hover 才浮出,避免误触 */}
{hoveredId === session.id && (
<button
onClick={(e) => {
e.stopPropagation();
onDelete(session);
}}
title="删除会话"
className="absolute right-1.5 top-1/2 -translate-y-1/2 p-1 rounded-md text-slate-400 hover:text-red-600 hover:bg-white transition"
>
<Trash2 className="w-3.5 h-3.5" />
</button>
)}
</div>
);
})}
</div>
</aside>
);
}