fix(chat): speed up history switch
This commit is contained in:
@@ -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">
|
||||
|
||||
Reference in New Issue
Block a user