fix(chat): speed up history switch

This commit is contained in:
john
2026-06-29 11:15:40 +08:00
parent b7af5b00dc
commit 394d3640d1
2 changed files with 40 additions and 13 deletions
+27 -2
View File
@@ -67,10 +67,12 @@ export function HistorySidebar({
}: HistorySidebarProps) {
const bodyRef = useRef<HTMLDivElement>(null);
const [pendingDeleteId, setPendingDeleteId] = useState<string | null>(null);
const [pendingSelectId, setPendingSelectId] = useState<string | null>(null);
useEffect(() => {
if (open) {
setPendingDeleteId(null);
setPendingSelectId(null);
}
}, [open, sessions.length]);
@@ -84,6 +86,7 @@ export function HistorySidebar({
}, [hasMore, loading, loadingMore, onLoadMore]);
const handleDeleteClick = useCallback((sessionId: string) => {
setPendingSelectId(null);
setPendingDeleteId(sessionId);
}, []);
@@ -99,6 +102,24 @@ export function HistorySidebar({
setPendingDeleteId(null);
}, []);
const handleSelectClick = useCallback(
(sessionId: string) => {
if (pendingDeleteId === sessionId) return;
if (activeSessionId === sessionId) {
setPendingSelectId(null);
onSelect(sessionId);
return;
}
if (pendingSelectId === sessionId) {
setPendingSelectId(null);
onSelect(sessionId);
return;
}
setPendingSelectId(sessionId);
},
[activeSessionId, onSelect, pendingDeleteId, pendingSelectId],
);
if (!open) return null;
const sessionGroups = groupSessionsByDate(sessions);
@@ -145,17 +166,21 @@ export function HistorySidebar({
{group.sessions.map((item) => {
const label = getSessionListLabel(item);
const isPending = pendingDeleteId === item.id;
const isPendingSelect = pendingSelectId === item.id;
return (
<li key={item.id} className={`sidebar-item-wrap${isPending ? ' sidebar-item-confirming' : ''}`}>
<button
type="button"
className={`sidebar-item ${item.id === activeSessionId ? 'sidebar-item-active' : ''}`}
onClick={() => { if (!isPending) onSelect(item.id); }}
className={`sidebar-item ${item.id === activeSessionId ? 'sidebar-item-active' : ''}${isPendingSelect ? ' sidebar-item-pending' : ''}`}
onClick={() => handleSelectClick(item.id)}
>
<span className="sidebar-item-title">{label}</span>
<span className="sidebar-item-meta">
{formatSessionTime(item.updated_at ?? item.created_at)}
</span>
{isPendingSelect && item.id !== activeSessionId ? (
<span className="sidebar-item-hint"></span>
) : null}
</button>
{isPending ? (
<div className="sidebar-item-confirm">