ConversationList.tsx
5.99 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
/** 会话栏:新对话、搜索、按时间分组的对话列表,以及改名 / 置顶 / 删除。 */
import { MessageSquarePlus, Pencil, Pin, Trash2 } from "lucide-react";
import { useState } from "react";
import { Confirm, Modal } from "@/components/ui";
import { errText, onto } from "@/lib/onto";
import { cn } from "@/lib/utils";
import { toast } from "@/store/useAppStore";
import type { ConvBrief } from "./askTypes";
/** 对话列表按时间分组:置顶 / 今天 / 昨天 / 7 天内 / 更早;带搜索。 */
function groupConvs(convs: ConvBrief[], query: string): [string, ConvBrief[]][] {
const q = query.trim().toLowerCase();
const list = q ? convs.filter((c) => (c.title || "").toLowerCase().includes(q)) : convs;
const day = (d: Date) => new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
const today = day(new Date());
const groups: Record<string, ConvBrief[]> = { 置顶: [], 今天: [], 昨天: [], "7 天内": [], 更早: [] };
for (const c of list) {
const age = (today - day(new Date(c.updated_at))) / 86400000;
groups[c.pinned ? "置顶" : age <= 0 ? "今天" : age <= 1 ? "昨天" : age <= 7 ? "7 天内" : "更早"].push(c);
}
return Object.entries(groups).filter(([, v]) => v.length > 0);
}
export function ConversationList({ convs, current, answering, onNew, onOpen, onChanged, onDeleted }: {
convs: ConvBrief[];
/** 当前打开的对话 */
current: string;
/** 正在生成回答的那个对话(没有就是空串) */
answering: string;
onNew: () => void; onOpen: (id: string) => void;
/** 改名 / 置顶之后重新拉列表 */
onChanged: () => void; onDeleted: (id: string) => void;
}) {
const [convQuery, setConvQuery] = useState("");
const [renaming, setRenaming] = useState<{ id: string; title: string } | null>(null);
const [deleting, setDeleting] = useState<ConvBrief | null>(null);
return (
<>
<aside className="card hidden w-56 shrink-0 flex-col lg:flex">
<div className="border-b border-line p-2">
<button className="btn w-full justify-center" onClick={onNew}>
<MessageSquarePlus className="h-4 w-4" /> 新对话
</button>
</div>
<div className="border-b border-line p-2">
<input className="input h-8 w-full" placeholder="搜索对话" value={convQuery} onChange={(e) => setConvQuery(e.target.value)} />
</div>
<div className="min-h-0 flex-1 overflow-auto p-1.5">
{convs.length === 0 && <p className="p-3 text-center text-label text-muted">还没有对话</p>}
{groupConvs(convs, convQuery).map(([label, list]) => (
<div key={label} className="mb-1.5">
<p className="px-2 py-1 text-caption text-muted">{label}</p>
{list.map((c) => (
<div key={c.id} role="button" tabIndex={0}
className={cn("group flex cursor-pointer items-center gap-1 rounded-md px-2 py-1.5 text-label",
c.id === current ? "bg-brand/10 text-brand" : "hover:bg-ink/5")}
onClick={() => onOpen(c.id)}
onKeyDown={(e) => { if (e.key === "Enter") onOpen(c.id); }}>
{c.pinned && <Pin className="h-3 w-3 shrink-0 text-muted" />}
<span className="min-w-0 flex-1 truncate" title={c.title}>{c.title || "未命名"}</span>
{answering === c.id && <span className="shrink-0 text-caption text-warn">回答中</span>}
<span className="hidden shrink-0 items-center gap-1 group-hover:flex">
<button className="text-muted hover:text-brand" title={c.pinned ? "取消置顶" : "置顶"} aria-label="置顶"
onClick={(e) => { e.stopPropagation(); void onto.saveConversation(c.id, { pinned: !c.pinned }).then(onChanged).catch((err) => toast("error", "操作失败", errText(err))); }}>
<Pin className="h-3.5 w-3.5" />
</button>
<button className="text-muted hover:text-brand" title="改名" aria-label="改名"
onClick={(e) => { e.stopPropagation(); setRenaming({ id: c.id, title: c.title }); }}>
<Pencil className="h-3.5 w-3.5" />
</button>
<button className="text-muted hover:text-bad" title="删除对话" aria-label="删除对话"
onClick={(e) => { e.stopPropagation(); setDeleting(c); }}>
<Trash2 className="h-3.5 w-3.5" />
</button>
</span>
</div>
))}
</div>
))}
</div>
</aside>
<Modal open={!!renaming} onClose={() => setRenaming(null)} title="给对话改个名字"
footer={<><button className="btn" onClick={() => setRenaming(null)}>取消</button>
<button className="btn btn-primary" disabled={!renaming?.title.trim()} onClick={() => {
if (!renaming) return;
void onto.saveConversation(renaming.id, { title: renaming.title.trim() }).then(() => { setRenaming(null); onChanged(); })
.catch((err) => toast("error", "改名失败", errText(err)));
}}>保存</button></>}>
<input className="input h-8 w-full" autoFocus value={renaming?.title || ""} onChange={(e) => setRenaming((r) => (r ? { ...r, title: e.target.value } : r))} />
</Modal>
<Confirm open={!!deleting} danger title="删除这个对话?" confirmText="删除"
body={`「${deleting?.title || "未命名"}」里的 ${deleting?.turns ?? 0} 轮问答和推理记录会一起删掉,不能恢复。`}
onClose={() => setDeleting(null)}
onConfirm={() => {
const c = deleting;
setDeleting(null);
if (!c) return;
void onto.deleteConversation(c.id).then(() => onDeleted(c.id))
.catch((err) => toast("error", "删除失败", errText(err)));
}} />
</>
);
}