fix: 修复历史记录列表重复显示问题

- 添加去重逻辑,防止相同ID的历史记录被重复添加
- 防止滚动事件监听器重复注册
- 添加请求ID防止并发加载
- 修正offset计算,使用实际数组长度
This commit is contained in:
XiaoFeng
2026-01-12 18:10:01 +08:00
parent fd11eadc19
commit 5a5d82eef8

View File

@ -303,6 +303,7 @@ export function getConversationHistoryBarScript(): string {
let totalHistory = 0; let totalHistory = 0;
let hasMoreHistory = false; let hasMoreHistory = false;
let isLoadingHistory = false; let isLoadingHistory = false;
let currentLoadRequestId = 0; // 请求 ID用于防止并发加载
const HISTORY_PAGE_SIZE = 10; const HISTORY_PAGE_SIZE = 10;
const MAX_HISTORY_ITEMS = 100; const MAX_HISTORY_ITEMS = 100;
@ -346,11 +347,15 @@ export function getConversationHistoryBarScript(): string {
return; return;
} }
// 生成新的请求 ID用于防止并发加载
const requestId = ++currentLoadRequestId;
isLoadingHistory = true; isLoadingHistory = true;
vscode.postMessage({ vscode.postMessage({
command: 'loadConversationHistory', command: 'loadConversationHistory',
offset: currentOffset, offset: currentOffset,
limit: HISTORY_PAGE_SIZE limit: HISTORY_PAGE_SIZE,
requestId: requestId
}); });
} }
@ -362,11 +367,19 @@ export function getConversationHistoryBarScript(): string {
return; return;
} }
// 追加新数据 // 追加新数据(去重)
conversationHistory = conversationHistory.concat(data.items); const existingIds = new Set(conversationHistory.map(item => item.id));
const newItems = [];
for (const item of data.items) {
if (!existingIds.has(item.id)) {
existingIds.add(item.id);
newItems.push(item);
}
}
conversationHistory = conversationHistory.concat(newItems);
totalHistory = data.total; totalHistory = data.total;
hasMoreHistory = data.hasMore; hasMoreHistory = data.hasMore;
currentOffset += data.items.length; currentOffset = conversationHistory.length;
const historyList = document.getElementById('historyList'); const historyList = document.getElementById('historyList');
if (!historyList) { if (!historyList) {
@ -454,9 +467,10 @@ export function getConversationHistoryBarScript(): string {
}); });
} }
// 监听下拉菜单滚动事件 // 监听下拉菜单滚动事件(防止重复注册)
const historyDropdownMenu = document.getElementById('historyDropdownMenu'); const historyDropdownMenu = document.getElementById('historyDropdownMenu');
if (historyDropdownMenu) { if (historyDropdownMenu && !historyDropdownMenu._scrollListenerAdded) {
historyDropdownMenu._scrollListenerAdded = true;
historyDropdownMenu.addEventListener('scroll', () => { historyDropdownMenu.addEventListener('scroll', () => {
const menu = historyDropdownMenu; const menu = historyDropdownMenu;
const scrollTop = menu.scrollTop; const scrollTop = menu.scrollTop;