mirror of
https://github.com/Ed1s0nZ/CyberStrikeAI.git
synced 2026-06-10 08:13:59 +02:00
Add files via upload
This commit is contained in:
+199
-18
@@ -2939,6 +2939,8 @@ function createConversationListItem(conversation) {
|
||||
// 处理历史记录搜索
|
||||
let conversationSearchTimer = null;
|
||||
function handleConversationSearch(query) {
|
||||
conversationsPagination.page = 1;
|
||||
conversationsSearchQuery = query || '';
|
||||
// 防抖处理,避免频繁请求
|
||||
if (conversationSearchTimer) {
|
||||
clearTimeout(conversationSearchTimer);
|
||||
@@ -2972,6 +2974,8 @@ function clearConversationSearch() {
|
||||
clearBtn.style.display = 'none';
|
||||
}
|
||||
|
||||
conversationsPagination.page = 1;
|
||||
conversationsSearchQuery = '';
|
||||
loadConversations('');
|
||||
}
|
||||
|
||||
@@ -5608,6 +5612,168 @@ let groupsCache = [];
|
||||
let conversationGroupMappingCache = {};
|
||||
let pendingGroupMappings = {}; // 待保留的分组映射(用于处理后端API延迟的情况)
|
||||
let conversationsListLoadSeq = 0; // 对话列表加载序号,避免并发请求导致重复渲染
|
||||
const CONVERSATIONS_PAGE_SIZE_KEY = 'cyberstrike.conversations_page_size';
|
||||
|
||||
function getConversationsPageSize() {
|
||||
try {
|
||||
const saved = parseInt(localStorage.getItem(CONVERSATIONS_PAGE_SIZE_KEY), 10);
|
||||
if ([20, 50, 100].includes(saved)) return saved;
|
||||
} catch (e) { /* ignore */ }
|
||||
return 50;
|
||||
}
|
||||
|
||||
let conversationsPagination = { page: 1, pageSize: getConversationsPageSize(), total: 0 };
|
||||
let conversationsSearchQuery = '';
|
||||
|
||||
function parseListTotalValue(raw, itemsLength) {
|
||||
if (typeof raw === 'number' && Number.isFinite(raw) && raw >= 0) return raw;
|
||||
if (raw != null && raw !== '') {
|
||||
const n = parseInt(String(raw), 10);
|
||||
if (Number.isFinite(n) && n >= 0) return n;
|
||||
}
|
||||
return itemsLength;
|
||||
}
|
||||
|
||||
function parseListOffsetValue(raw) {
|
||||
if (typeof raw === 'number' && Number.isFinite(raw) && raw >= 0) return raw;
|
||||
if (raw != null && raw !== '') {
|
||||
const n = parseInt(String(raw), 10);
|
||||
if (Number.isFinite(n) && n >= 0) return n;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function parseConversationsListResponse(data) {
|
||||
if (Array.isArray(data)) {
|
||||
return { items: data, total: data.length, limit: data.length, offset: 0, isLegacyArray: true };
|
||||
}
|
||||
const items = data.conversations || data.items || [];
|
||||
const arr = Array.isArray(items) ? items : [];
|
||||
return {
|
||||
items: arr,
|
||||
total: parseListTotalValue(data.total, arr.length),
|
||||
limit: parseListTotalValue(data.limit, arr.length) || arr.length,
|
||||
offset: parseListOffsetValue(data.offset),
|
||||
isLegacyArray: false,
|
||||
};
|
||||
}
|
||||
|
||||
async function resolveConversationsListTotal(params, parsed, pageSize, offset) {
|
||||
const serverTotal = parsed.total;
|
||||
if (!parsed.isLegacyArray && serverTotal > offset + parsed.items.length) {
|
||||
return serverTotal;
|
||||
}
|
||||
if (parsed.items.length < pageSize) {
|
||||
return Math.max(serverTotal, offset + parsed.items.length);
|
||||
}
|
||||
const probe = new URLSearchParams(params);
|
||||
probe.set('offset', String(offset + pageSize));
|
||||
probe.set('limit', '1');
|
||||
try {
|
||||
const res = await apiFetch(`/api/conversations?${probe}`);
|
||||
if (!res.ok) return Math.max(serverTotal, offset + parsed.items.length);
|
||||
const probeParsed = parseConversationsListResponse(await res.json());
|
||||
if (probeParsed.total > serverTotal) return probeParsed.total;
|
||||
if (probeParsed.items.length > 0) {
|
||||
return Math.max(serverTotal, offset + pageSize + 1);
|
||||
}
|
||||
} catch (e) { /* ignore */ }
|
||||
return Math.max(serverTotal, offset + parsed.items.length);
|
||||
}
|
||||
|
||||
async function fetchAllConversations(searchQuery) {
|
||||
let all = [];
|
||||
const pageSize = 200;
|
||||
let offset = 0;
|
||||
let total = Infinity;
|
||||
const search = (searchQuery || '').trim();
|
||||
while (all.length < total) {
|
||||
const params = new URLSearchParams({ limit: String(pageSize), offset: String(offset) });
|
||||
if (search) params.set('search', search);
|
||||
const res = await apiFetch(`/api/conversations?${params}`);
|
||||
if (!res.ok) throw new Error('load conversations failed');
|
||||
const parsed = parseConversationsListResponse(await res.json());
|
||||
all = all.concat(parsed.items);
|
||||
total = parsed.total;
|
||||
if (!parsed.items.length) break;
|
||||
offset += parsed.items.length;
|
||||
}
|
||||
return all;
|
||||
}
|
||||
|
||||
function getConversationListEmptyHtml() {
|
||||
return '<div class="conversations-list-empty" data-i18n="chat.noHistoryConversations"></div>';
|
||||
}
|
||||
|
||||
function renderConversationsPagination(visibleCount) {
|
||||
const el = document.getElementById('conversations-pagination');
|
||||
if (!el) return;
|
||||
const { page, pageSize, total } = conversationsPagination;
|
||||
const count = typeof visibleCount === 'number' ? visibleCount : (conversationsPagination.visibleCount || 0);
|
||||
conversationsPagination.visibleCount = count;
|
||||
|
||||
if (count === 0 || total === 0) {
|
||||
el.innerHTML = '';
|
||||
el.hidden = true;
|
||||
return;
|
||||
}
|
||||
|
||||
const totalPages = Math.max(1, Math.ceil(total / pageSize) || 1);
|
||||
const navDisabled = totalPages <= 1;
|
||||
el.hidden = false;
|
||||
const start = (page - 1) * pageSize + 1;
|
||||
const end = Math.min(page * pageSize, total);
|
||||
const tFn = typeof window.t === 'function' ? window.t.bind(window) : null;
|
||||
const infoText = tFn
|
||||
? tFn('chat.paginationRange', { start, end, total })
|
||||
: `${start}-${end}/${total}`;
|
||||
const pageText = tFn
|
||||
? tFn('chat.paginationPage', { page, total: totalPages })
|
||||
: `${page}/${totalPages}`;
|
||||
const perPageLabel = tFn ? tFn('chat.paginationPerPage') : 'Per page';
|
||||
const prevLabel = tFn ? tFn('chat.paginationPrev') : 'Prev';
|
||||
const nextLabel = tFn ? tFn('chat.paginationNext') : 'Next';
|
||||
el.innerHTML = `
|
||||
<div class="sidebar-list-pagination-inner sidebar-list-pagination-inner--compact">
|
||||
<span class="pagination-info">${escapeHtml(infoText)}</span>
|
||||
<div class="pagination-controls">
|
||||
<button type="button" class="btn-icon-pagination" onclick="goConversationsPage(${page - 1})" ${page <= 1 || navDisabled ? 'disabled' : ''} title="${escapeHtml(prevLabel)}" aria-label="${escapeHtml(prevLabel)}">‹</button>
|
||||
<span class="pagination-page">${escapeHtml(pageText)}</span>
|
||||
<button type="button" class="btn-icon-pagination" onclick="goConversationsPage(${page + 1})" ${page >= totalPages || navDisabled ? 'disabled' : ''} title="${escapeHtml(nextLabel)}" aria-label="${escapeHtml(nextLabel)}">›</button>
|
||||
</div>
|
||||
<label class="pagination-page-size">
|
||||
${escapeHtml(perPageLabel)}
|
||||
<select id="conversations-page-size-pagination" onchange="changeConversationsPageSize()">
|
||||
<option value="20" ${pageSize === 20 ? 'selected' : ''}>20</option>
|
||||
<option value="50" ${pageSize === 50 ? 'selected' : ''}>50</option>
|
||||
<option value="100" ${pageSize === 100 ? 'selected' : ''}>100</option>
|
||||
</select>
|
||||
</label>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function goConversationsPage(page) {
|
||||
const totalPages = Math.max(1, Math.ceil((conversationsPagination.total || 0) / conversationsPagination.pageSize) || 1);
|
||||
const next = Math.min(Math.max(1, page), totalPages);
|
||||
if (next === conversationsPagination.page) return;
|
||||
conversationsPagination.page = next;
|
||||
loadConversationsWithGroups(conversationsSearchQuery);
|
||||
}
|
||||
|
||||
function changeConversationsPageSize() {
|
||||
const sel = document.getElementById('conversations-page-size-pagination');
|
||||
const newSize = sel ? parseInt(sel.value, 10) : 50;
|
||||
if (![20, 50, 100].includes(newSize)) return;
|
||||
try {
|
||||
localStorage.setItem(CONVERSATIONS_PAGE_SIZE_KEY, String(newSize));
|
||||
} catch (e) { /* ignore */ }
|
||||
conversationsPagination.pageSize = newSize;
|
||||
conversationsPagination.page = 1;
|
||||
loadConversationsWithGroups(conversationsSearchQuery);
|
||||
}
|
||||
|
||||
window.goConversationsPage = goConversationsPage;
|
||||
window.changeConversationsPageSize = changeConversationsPageSize;
|
||||
|
||||
// 加载分组列表
|
||||
async function loadGroups() {
|
||||
@@ -5704,12 +5870,17 @@ async function loadGroups() {
|
||||
async function loadConversationsWithGroups(searchQuery = '') {
|
||||
const loadSeq = ++conversationsListLoadSeq;
|
||||
try {
|
||||
// 并行加载分组列表、分组映射和对话列表(消除串行等待)
|
||||
const limit = (searchQuery && searchQuery.trim()) ? 100 : 100;
|
||||
let url = `/api/conversations?limit=${limit}`;
|
||||
conversationsSearchQuery = searchQuery || '';
|
||||
conversationsPagination.pageSize = getConversationsPageSize();
|
||||
const pageSize = conversationsPagination.pageSize;
|
||||
const offset = (conversationsPagination.page - 1) * pageSize;
|
||||
const convParams = new URLSearchParams({ limit: String(pageSize), offset: String(offset) });
|
||||
if (searchQuery && searchQuery.trim()) {
|
||||
url += '&search=' + encodeURIComponent(searchQuery.trim());
|
||||
convParams.set('search', searchQuery.trim());
|
||||
} else {
|
||||
convParams.set('exclude_grouped', 'true');
|
||||
}
|
||||
const url = `/api/conversations?${convParams}`;
|
||||
const [,, response] = await Promise.all([
|
||||
loadGroups(),
|
||||
loadConversationGroupMapping(),
|
||||
@@ -5726,23 +5897,26 @@ async function loadConversationsWithGroups(searchQuery = '') {
|
||||
const sidebarContent = listContainer.closest('.sidebar-content');
|
||||
const savedScrollTop = sidebarContent ? sidebarContent.scrollTop : 0;
|
||||
|
||||
const emptyStateHtml = '<div style="padding: 20px; text-align: center; color: var(--text-muted); font-size: 0.875rem;" data-i18n="chat.noHistoryConversations"></div>';
|
||||
const emptyStateHtml = getConversationListEmptyHtml();
|
||||
listContainer.innerHTML = '';
|
||||
|
||||
// 如果响应不是200,显示空状态(友好处理,不显示错误)
|
||||
if (!response.ok) {
|
||||
listContainer.innerHTML = emptyStateHtml;
|
||||
if (typeof window.applyTranslations === 'function') window.applyTranslations(listContainer);
|
||||
renderConversationsPagination(0);
|
||||
return;
|
||||
}
|
||||
|
||||
const conversations = await response.json();
|
||||
const data = await response.json();
|
||||
if (loadSeq !== conversationsListLoadSeq) return;
|
||||
const parsed = parseConversationsListResponse(data);
|
||||
conversationsPagination.total = await resolveConversationsListTotal(convParams, parsed, pageSize, offset);
|
||||
|
||||
// 双重保险:后端或并发情况下若出现重复ID,前端按ID去重
|
||||
const uniqueConversations = [];
|
||||
const seenConversationIds = new Set();
|
||||
(Array.isArray(conversations) ? conversations : []).forEach(conv => {
|
||||
parsed.items.forEach(conv => {
|
||||
if (!conv || !conv.id || seenConversationIds.has(conv.id)) {
|
||||
return;
|
||||
}
|
||||
@@ -5753,6 +5927,7 @@ async function loadConversationsWithGroups(searchQuery = '') {
|
||||
if (uniqueConversations.length === 0) {
|
||||
listContainer.innerHTML = emptyStateHtml;
|
||||
if (typeof window.applyTranslations === 'function') window.applyTranslations(listContainer);
|
||||
renderConversationsPagination(0);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -5863,15 +6038,29 @@ async function loadConversationsWithGroups(searchQuery = '') {
|
||||
fragment.appendChild(section);
|
||||
});
|
||||
|
||||
const visibleCount = pinnedConvs.length + Object.values(groups).reduce((n, arr) => n + (arr ? arr.length : 0), 0);
|
||||
conversationsPagination.visibleCount = visibleCount;
|
||||
|
||||
if (!hasSearchQuery && visibleCount === 0 && parsed.items.length > 0) {
|
||||
const totalPages = Math.max(1, Math.ceil(parsed.total / pageSize));
|
||||
if (conversationsPagination.page < totalPages) {
|
||||
conversationsPagination.page += 1;
|
||||
loadConversationsWithGroups(searchQuery);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (fragment.children.length === 0) {
|
||||
listContainer.innerHTML = emptyStateHtml;
|
||||
if (typeof window.applyTranslations === 'function') window.applyTranslations(listContainer);
|
||||
renderConversationsPagination(0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (loadSeq !== conversationsListLoadSeq) return;
|
||||
listContainer.appendChild(fragment);
|
||||
updateActiveConversation();
|
||||
renderConversationsPagination(visibleCount);
|
||||
|
||||
// 恢复滚动位置
|
||||
if (sidebarContent) {
|
||||
@@ -5888,9 +6077,9 @@ async function loadConversationsWithGroups(searchQuery = '') {
|
||||
// 错误时显示空状态,而不是错误提示(更友好的用户体验)
|
||||
const listContainer = document.getElementById('conversations-list');
|
||||
if (listContainer) {
|
||||
const emptyStateHtml = '<div style="padding: 20px; text-align: center; color: var(--text-muted); font-size: 0.875rem;" data-i18n="chat.noHistoryConversations"></div>';
|
||||
listContainer.innerHTML = emptyStateHtml;
|
||||
listContainer.innerHTML = getConversationListEmptyHtml();
|
||||
if (typeof window.applyTranslations === 'function') window.applyTranslations(listContainer);
|
||||
renderConversationsPagination(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7004,15 +7193,7 @@ function updateBatchManageTitle(count) {
|
||||
|
||||
async function showBatchManageModal() {
|
||||
try {
|
||||
const response = await apiFetch('/api/conversations?limit=1000');
|
||||
|
||||
// 如果响应不是200,使用空数组(友好处理,不显示错误)
|
||||
if (!response.ok) {
|
||||
allConversationsForBatch = [];
|
||||
} else {
|
||||
const data = await response.json();
|
||||
allConversationsForBatch = Array.isArray(data) ? data : [];
|
||||
}
|
||||
allConversationsForBatch = await fetchAllConversations('');
|
||||
|
||||
const modal = document.getElementById('batch-manage-modal');
|
||||
updateBatchManageTitle(allConversationsForBatch.length);
|
||||
|
||||
+218
-20
@@ -3,6 +3,7 @@
|
||||
*/
|
||||
let projectsCache = [];
|
||||
let projectsCacheAll = [];
|
||||
const PROJECTS_LIST_PAGE_SIZE_KEY = 'cyberstrike.projects_list_page_size';
|
||||
let currentProjectId = null;
|
||||
let currentProjectTab = 'facts';
|
||||
const projectNameById = {};
|
||||
@@ -167,23 +168,128 @@ function rebuildProjectNameMap(list) {
|
||||
});
|
||||
}
|
||||
|
||||
async function fetchProjectsList(includeArchived) {
|
||||
function getProjectsListPageSize() {
|
||||
try {
|
||||
const saved = parseInt(localStorage.getItem(PROJECTS_LIST_PAGE_SIZE_KEY), 10);
|
||||
if ([20, 50, 100].includes(saved)) return saved;
|
||||
} catch (e) { /* ignore */ }
|
||||
return 50;
|
||||
}
|
||||
|
||||
let projectsListPagination = { page: 1, pageSize: getProjectsListPageSize(), total: 0 };
|
||||
let projectsListSearch = '';
|
||||
let _projectsListSearchDebounce = null;
|
||||
|
||||
function parseListTotalValue(raw, itemsLength) {
|
||||
if (typeof raw === 'number' && Number.isFinite(raw) && raw >= 0) return raw;
|
||||
if (raw != null && raw !== '') {
|
||||
const n = parseInt(String(raw), 10);
|
||||
if (Number.isFinite(n) && n >= 0) return n;
|
||||
}
|
||||
return itemsLength;
|
||||
}
|
||||
|
||||
function parseListOffsetValue(raw) {
|
||||
if (typeof raw === 'number' && Number.isFinite(raw) && raw >= 0) return raw;
|
||||
if (raw != null && raw !== '') {
|
||||
const n = parseInt(String(raw), 10);
|
||||
if (Number.isFinite(n) && n >= 0) return n;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function parseProjectsListResponse(data) {
|
||||
if (Array.isArray(data)) {
|
||||
return { items: data, total: data.length, limit: data.length, offset: 0, isLegacyArray: true };
|
||||
}
|
||||
const items = data.projects || data.items || [];
|
||||
const arr = Array.isArray(items) ? items : [];
|
||||
return {
|
||||
items: arr,
|
||||
total: parseListTotalValue(data.total, arr.length),
|
||||
limit: parseListTotalValue(data.limit, arr.length) || arr.length,
|
||||
offset: parseListOffsetValue(data.offset),
|
||||
isLegacyArray: false,
|
||||
};
|
||||
}
|
||||
|
||||
async function resolveProjectsListTotal(params, parsed, pageSize, offset) {
|
||||
const serverTotal = parsed.total;
|
||||
// 服务端 total 明确大于当前页末尾 → 直接信任
|
||||
if (!parsed.isLegacyArray && serverTotal > offset + parsed.items.length) {
|
||||
return serverTotal;
|
||||
}
|
||||
// 不足一页 → 已是最后一页
|
||||
if (parsed.items.length < pageSize) {
|
||||
return Math.max(serverTotal, offset + parsed.items.length);
|
||||
}
|
||||
// 满页但 total 可能被误算为 items.length → 探测下一页
|
||||
const probe = new URLSearchParams(params);
|
||||
probe.set('offset', String(offset + pageSize));
|
||||
probe.set('limit', '1');
|
||||
try {
|
||||
const res = await apiFetch(`/api/projects?${probe}`);
|
||||
if (!res.ok) return Math.max(serverTotal, offset + parsed.items.length);
|
||||
const probeParsed = parseProjectsListResponse(await res.json());
|
||||
if (probeParsed.total > serverTotal) return probeParsed.total;
|
||||
if (probeParsed.items.length > 0) {
|
||||
return Math.max(serverTotal, offset + pageSize + 1);
|
||||
}
|
||||
} catch (e) { /* ignore */ }
|
||||
return Math.max(serverTotal, offset + parsed.items.length);
|
||||
}
|
||||
|
||||
async function fetchAllProjects(includeArchived) {
|
||||
const showArchived = includeArchived || document.getElementById('projects-show-archived')?.checked;
|
||||
const url = showArchived ? '/api/projects?limit=200' : '/api/projects?status=active&limit=200';
|
||||
const res = await apiFetch(url);
|
||||
let all = [];
|
||||
const pageSize = 200;
|
||||
let offset = 0;
|
||||
let total = Infinity;
|
||||
while (all.length < total) {
|
||||
const params = new URLSearchParams({ limit: String(pageSize), offset: String(offset) });
|
||||
if (!showArchived) params.set('status', 'active');
|
||||
const res = await apiFetch(`/api/projects?${params}`);
|
||||
if (!res.ok) throw new Error(tp('projects.loadProjectsFailed'));
|
||||
const parsed = parseProjectsListResponse(await res.json());
|
||||
all = all.concat(parsed.items);
|
||||
total = parsed.total;
|
||||
if (!parsed.items.length) break;
|
||||
offset += parsed.items.length;
|
||||
}
|
||||
return all;
|
||||
}
|
||||
|
||||
async function fetchProjectsList(includeArchived, opts = {}) {
|
||||
const showArchived = includeArchived || document.getElementById('projects-show-archived')?.checked;
|
||||
const page = opts.page ?? projectsListPagination.page;
|
||||
const pageSize = opts.pageSize ?? getProjectsListPageSize();
|
||||
const search = opts.search !== undefined ? opts.search : projectsListSearch;
|
||||
projectsListSearch = search;
|
||||
const offset = (page - 1) * pageSize;
|
||||
const params = new URLSearchParams({ limit: String(pageSize), offset: String(offset) });
|
||||
if (search) params.set('search', search);
|
||||
if (!showArchived) params.set('status', 'active');
|
||||
const res = await apiFetch(`/api/projects?${params}`);
|
||||
if (!res.ok) throw new Error(tp('projects.loadProjectsFailed'));
|
||||
const data = await res.json();
|
||||
projectsCache = Array.isArray(data) ? data : [];
|
||||
rebuildProjectNameMap(projectsCache);
|
||||
_projectsListReady = true;
|
||||
const parsed = parseProjectsListResponse(await res.json());
|
||||
const total = await resolveProjectsListTotal(params, parsed, pageSize, offset);
|
||||
projectsCache = parsed.items;
|
||||
projectsListPagination = { page, pageSize: pageSize, total };
|
||||
rebuildProjectNameMap(projectsCacheAll.length ? projectsCacheAll : projectsCache);
|
||||
return projectsCache;
|
||||
}
|
||||
|
||||
/** 对话页等项目选择器:确保列表已拉取(去重并发请求) */
|
||||
/** 对话页等项目选择器:确保全量列表已拉取(去重并发请求) */
|
||||
async function ensureProjectsLoaded(force) {
|
||||
if (!force && _projectsListReady) return projectsCache;
|
||||
if (!force && _projectsListReady) return projectsCacheAll;
|
||||
if (!force && _projectsFetchPromise) return _projectsFetchPromise;
|
||||
_projectsFetchPromise = fetchProjectsList(false)
|
||||
_projectsFetchPromise = fetchAllProjects(false)
|
||||
.then((list) => {
|
||||
projectsCacheAll = list;
|
||||
rebuildProjectNameMap(projectsCacheAll);
|
||||
_projectsListReady = true;
|
||||
return projectsCacheAll;
|
||||
})
|
||||
.catch((e) => {
|
||||
_projectsListReady = false;
|
||||
throw e;
|
||||
@@ -204,9 +310,10 @@ async function ensureDefaultActiveProjectForNewChat() {
|
||||
await ensureProjectsLoaded();
|
||||
const cur = getActiveProjectId();
|
||||
if (cur && isActiveChatProjectId(cur)) return cur;
|
||||
const source = projectsCacheAll.length ? projectsCacheAll : projectsCache;
|
||||
const first =
|
||||
projectsCache.find((p) => p.pinned && p.status !== 'archived') ||
|
||||
projectsCache.find((p) => p.status !== 'archived');
|
||||
source.find((p) => p.pinned && p.status !== 'archived') ||
|
||||
source.find((p) => p.status !== 'archived');
|
||||
if (first) {
|
||||
setActiveProjectId(first.id);
|
||||
return first.id;
|
||||
@@ -238,6 +345,8 @@ async function initProjectsPage() {
|
||||
initProjectsModalEscape();
|
||||
syncProjectsModalBodyLock();
|
||||
updateProjectsDetailVisibility();
|
||||
projectsListPagination.pageSize = getProjectsListPageSize();
|
||||
renderProjectsPagination();
|
||||
await loadProjectsList();
|
||||
if (!currentProjectId && projectsCache.length) {
|
||||
const fromHash = new URLSearchParams(window.location.hash.split('?')[1] || '').get('id');
|
||||
@@ -250,8 +359,19 @@ async function initProjectsPage() {
|
||||
}
|
||||
|
||||
async function loadProjectsList() {
|
||||
_projectsListReady = false;
|
||||
projectsCacheAll = [];
|
||||
projectsListPagination.pageSize = getProjectsListPageSize();
|
||||
await fetchProjectsList();
|
||||
renderProjectsSidebar();
|
||||
renderProjectsPagination();
|
||||
try {
|
||||
projectsCacheAll = await fetchAllProjects();
|
||||
rebuildProjectNameMap(projectsCacheAll);
|
||||
_projectsListReady = true;
|
||||
} catch (e) {
|
||||
console.warn(e);
|
||||
}
|
||||
if (typeof refreshChatProjectSelector === 'function') {
|
||||
refreshChatProjectSelector();
|
||||
}
|
||||
@@ -277,7 +397,7 @@ function updateProjectsDetailVisibility() {
|
||||
|
||||
function updateProjectsListCount() {
|
||||
const el = document.getElementById('projects-list-count');
|
||||
if (el) el.textContent = String(projectsCache.length);
|
||||
if (el) el.textContent = String(projectsListPagination.total || projectsCache.length);
|
||||
}
|
||||
|
||||
/** 事实分类 → 徽章样式(与 fact_template.go 常量对齐) */
|
||||
@@ -385,26 +505,97 @@ function getProjectsListFilter() {
|
||||
}
|
||||
|
||||
function filterProjectsList() {
|
||||
renderProjectsSidebar();
|
||||
if (_projectsListSearchDebounce) clearTimeout(_projectsListSearchDebounce);
|
||||
_projectsListSearchDebounce = setTimeout(() => {
|
||||
_projectsListSearchDebounce = null;
|
||||
const q = getProjectsListFilter();
|
||||
projectsListPagination.page = 1;
|
||||
fetchProjectsList(undefined, { page: 1, search: q })
|
||||
.then(() => {
|
||||
renderProjectsSidebar();
|
||||
renderProjectsPagination();
|
||||
})
|
||||
.catch((e) => console.warn(e));
|
||||
}, 280);
|
||||
}
|
||||
|
||||
function goProjectsPage(page) {
|
||||
const totalPages = Math.max(1, Math.ceil((projectsListPagination.total || 0) / projectsListPagination.pageSize) || 1);
|
||||
const next = Math.min(Math.max(1, page), totalPages);
|
||||
if (next === projectsListPagination.page) return;
|
||||
fetchProjectsList(undefined, { page: next })
|
||||
.then(() => {
|
||||
renderProjectsSidebar();
|
||||
renderProjectsPagination();
|
||||
const listEl = document.getElementById('projects-list');
|
||||
if (listEl) listEl.scrollTop = 0;
|
||||
})
|
||||
.catch((e) => console.warn(e));
|
||||
}
|
||||
|
||||
function changeProjectsPageSize() {
|
||||
const sel = document.getElementById('projects-page-size-pagination');
|
||||
const newSize = sel ? parseInt(sel.value, 10) : 50;
|
||||
if (![20, 50, 100].includes(newSize)) return;
|
||||
try {
|
||||
localStorage.setItem(PROJECTS_LIST_PAGE_SIZE_KEY, String(newSize));
|
||||
} catch (e) { /* ignore */ }
|
||||
projectsListPagination.pageSize = newSize;
|
||||
projectsListPagination.page = 1;
|
||||
fetchProjectsList(undefined, { page: 1, pageSize: newSize })
|
||||
.then(() => {
|
||||
renderProjectsSidebar();
|
||||
renderProjectsPagination();
|
||||
})
|
||||
.catch((e) => console.warn(e));
|
||||
}
|
||||
|
||||
function renderProjectsPagination() {
|
||||
const el = document.getElementById('projects-pagination');
|
||||
if (!el) return;
|
||||
const { page, pageSize, total } = projectsListPagination;
|
||||
const totalPages = Math.max(1, Math.ceil(total / pageSize) || 1);
|
||||
const navDisabled = total === 0 || totalPages <= 1;
|
||||
el.hidden = false;
|
||||
const start = total === 0 ? 0 : (page - 1) * pageSize + 1;
|
||||
const end = total === 0 ? 0 : Math.min(page * pageSize, total);
|
||||
const infoText = tpFmt('projects.paginationRange', `${start}-${end}/${total}`, { start, end, total });
|
||||
const pageText = tpFmt('projects.paginationPage', `${page}/${totalPages}`, { page, total: totalPages });
|
||||
el.innerHTML = `
|
||||
<div class="sidebar-list-pagination-inner sidebar-list-pagination-inner--compact">
|
||||
<span class="pagination-info">${escapeHtml(infoText)}</span>
|
||||
<div class="pagination-controls">
|
||||
<button type="button" class="btn-icon-pagination" onclick="goProjectsPage(${page - 1})" ${page <= 1 || navDisabled ? 'disabled' : ''} title="${escapeHtml(tp('projects.paginationPrev'))}" aria-label="${escapeHtml(tp('projects.paginationPrev'))}">‹</button>
|
||||
<span class="pagination-page">${escapeHtml(pageText)}</span>
|
||||
<button type="button" class="btn-icon-pagination" onclick="goProjectsPage(${page + 1})" ${page >= totalPages || navDisabled ? 'disabled' : ''} title="${escapeHtml(tp('projects.paginationNext'))}" aria-label="${escapeHtml(tp('projects.paginationNext'))}">›</button>
|
||||
</div>
|
||||
<label class="pagination-page-size">
|
||||
${escapeHtml(tp('projects.paginationPerPage'))}
|
||||
<select id="projects-page-size-pagination" onchange="changeProjectsPageSize()">
|
||||
<option value="20" ${pageSize === 20 ? 'selected' : ''}>20</option>
|
||||
<option value="50" ${pageSize === 50 ? 'selected' : ''}>50</option>
|
||||
<option value="100" ${pageSize === 100 ? 'selected' : ''}>100</option>
|
||||
</select>
|
||||
</label>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function renderProjectsSidebar() {
|
||||
const el = document.getElementById('projects-list');
|
||||
if (!el) return;
|
||||
updateProjectsListCount();
|
||||
const q = getProjectsListFilter();
|
||||
const list = q
|
||||
? projectsCache.filter((p) => (p.name || '').toLowerCase().includes(q) || (p.description || '').toLowerCase().includes(q))
|
||||
: projectsCache;
|
||||
const list = projectsCache;
|
||||
if (!projectsCache.length) {
|
||||
el.innerHTML =
|
||||
`<div class="projects-empty">${escapeHtml(tp('projects.noProjects'))}<br><button type="button" class="btn-primary btn-small projects-empty-btn" onclick="showNewProjectModal()">${escapeHtml(tp('projects.newProject'))}</button></div>`;
|
||||
updateProjectsDetailVisibility();
|
||||
renderProjectsPagination();
|
||||
return;
|
||||
}
|
||||
if (!list.length) {
|
||||
el.innerHTML = `<div class="projects-empty">${escapeHtml(tp('projects.noMatchingProjects'))}</div>`;
|
||||
updateProjectsDetailVisibility();
|
||||
renderProjectsPagination();
|
||||
return;
|
||||
}
|
||||
el.innerHTML = list.map((p) => {
|
||||
@@ -1345,7 +1536,8 @@ function getChatProjectSelection() {
|
||||
|
||||
function isActiveChatProjectId(id) {
|
||||
if (!id) return false;
|
||||
return projectsCache.some((p) => p.id === id && p.status !== 'archived');
|
||||
const source = projectsCacheAll.length ? projectsCacheAll : projectsCache;
|
||||
return source.some((p) => p.id === id && p.status !== 'archived');
|
||||
}
|
||||
|
||||
/** 用于 UI:无效/已删除/无可用项目时视为未绑定 */
|
||||
@@ -1400,7 +1592,8 @@ function renderChatProjectPanelList() {
|
||||
const list = document.getElementById('chat-project-list');
|
||||
if (!list) return;
|
||||
const selected = resolveChatProjectSelection();
|
||||
const activeProjects = projectsCache.filter((p) => p.status !== 'archived');
|
||||
const source = projectsCacheAll.length ? projectsCacheAll : projectsCache;
|
||||
const activeProjects = source.filter((p) => p.status !== 'archived');
|
||||
const items = [{ id: '', name: tp('projects.noProject'), description: tp('projects.noProjectDescription') }, ...activeProjects];
|
||||
if (!items.length) {
|
||||
list.innerHTML = `<div class="chat-project-panel-empty">${escapeHtml(tp('projects.noProjectsClickCreate'))}</div>`;
|
||||
@@ -1543,6 +1736,7 @@ function initChatProjectSelector() {
|
||||
window._projectsLanguageListenerBound = true;
|
||||
document.addEventListener('languagechange', () => {
|
||||
renderProjectsSidebar();
|
||||
renderProjectsPagination();
|
||||
updateChatProjectButtonLabel();
|
||||
const panel = document.getElementById('chat-project-panel');
|
||||
if (panel && panel.style.display === 'flex') renderChatProjectPanelList();
|
||||
@@ -1602,6 +1796,10 @@ window.restoreProjectFactByKey = restoreProjectFactByKey;
|
||||
window.openVulnerabilitiesForProject = openVulnerabilitiesForProject;
|
||||
window.openVulnerabilityDetail = openVulnerabilityDetail;
|
||||
window.filterProjectsList = filterProjectsList;
|
||||
window.goProjectsPage = goProjectsPage;
|
||||
window.changeProjectsPageSize = changeProjectsPageSize;
|
||||
window.parseProjectsListResponse = parseProjectsListResponse;
|
||||
window.fetchAllProjects = fetchAllProjects;
|
||||
window.debouncedLoadProjectFacts = debouncedLoadProjectFacts;
|
||||
window.debouncedLoadProjectVulnerabilities = debouncedLoadProjectVulnerabilities;
|
||||
window.loadProjectVulnerabilities = loadProjectVulnerabilities;
|
||||
|
||||
+12
-5
@@ -819,12 +819,19 @@ async function refreshBatchProjectSelectOptions() {
|
||||
projectSelect.innerHTML = `<option value="">${escapeHtml(noneLabel)}</option>`;
|
||||
|
||||
try {
|
||||
const response = await apiFetch('/api/projects?status=active&limit=200');
|
||||
if (!response.ok) {
|
||||
throw new Error(_t('projects.loadProjectsFailed'));
|
||||
let list = [];
|
||||
if (typeof fetchAllProjects === 'function') {
|
||||
list = await fetchAllProjects(false);
|
||||
} else {
|
||||
const response = await apiFetch('/api/projects?status=active&limit=500');
|
||||
if (!response.ok) {
|
||||
throw new Error(_t('projects.loadProjectsFailed'));
|
||||
}
|
||||
const data = await response.json();
|
||||
list = typeof parseProjectsListResponse === 'function'
|
||||
? parseProjectsListResponse(data).items
|
||||
: (Array.isArray(data) ? data : (data.projects || []));
|
||||
}
|
||||
const projects = await response.json();
|
||||
const list = Array.isArray(projects) ? projects : [];
|
||||
const activeProjectId = typeof getActiveProjectId === 'function' ? getActiveProjectId() || '' : '';
|
||||
|
||||
list.forEach((project) => {
|
||||
|
||||
@@ -855,11 +855,6 @@ function renderVulnerabilities(vulnerabilities) {
|
||||
if (typeof window.applyTranslations === 'function') {
|
||||
window.applyTranslations(listContainer);
|
||||
}
|
||||
// 清空分页信息
|
||||
const paginationContainer = document.getElementById('vulnerability-pagination');
|
||||
if (paginationContainer) {
|
||||
paginationContainer.innerHTML = '';
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -960,12 +955,6 @@ function renderVulnerabilityPagination() {
|
||||
|
||||
const { currentPage, totalPages, total, pageSize } = vulnerabilityPagination;
|
||||
|
||||
// 如果没有数据,不显示分页控件
|
||||
if (total === 0) {
|
||||
paginationContainer.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算显示范围
|
||||
const start = total === 0 ? 0 : (currentPage - 1) * pageSize + 1;
|
||||
const end = total === 0 ? 0 : Math.min(currentPage * pageSize, total);
|
||||
@@ -1052,13 +1041,23 @@ async function populateVulnerabilityModalProjectSelect(selectedId) {
|
||||
const sel = document.getElementById('vulnerability-project-id');
|
||||
if (!sel) return;
|
||||
try {
|
||||
const res = await apiFetch('/api/projects?limit=200');
|
||||
if (res.ok) {
|
||||
const list = await res.json();
|
||||
let list = [];
|
||||
if (typeof fetchAllProjects === 'function') {
|
||||
list = await fetchAllProjects();
|
||||
} else {
|
||||
const res = await apiFetch('/api/projects?limit=500');
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
list = typeof parseProjectsListResponse === 'function'
|
||||
? parseProjectsListResponse(data).items
|
||||
: (Array.isArray(data) ? data : (data.projects || []));
|
||||
}
|
||||
}
|
||||
if (list.length) {
|
||||
if (typeof rebuildProjectNameMap === 'function') {
|
||||
rebuildProjectNameMap(list);
|
||||
} else if (typeof projectNameById !== 'undefined') {
|
||||
(list || []).forEach((p) => { if (p.id) projectNameById[p.id] = p.name || p.id; });
|
||||
list.forEach((p) => { if (p.id) projectNameById[p.id] = p.name || p.id; });
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -1722,9 +1721,17 @@ async function refreshVulnerabilityProjectFilter() {
|
||||
const sel = document.getElementById('vulnerability-project-filter');
|
||||
if (!sel) return;
|
||||
try {
|
||||
const res = await apiFetch('/api/projects?limit=200');
|
||||
if (!res.ok) return;
|
||||
const list = await res.json();
|
||||
let list = [];
|
||||
if (typeof fetchAllProjects === 'function') {
|
||||
list = await fetchAllProjects(true);
|
||||
} else {
|
||||
const res = await apiFetch('/api/projects?limit=500');
|
||||
if (!res.ok) return;
|
||||
const data = await res.json();
|
||||
list = typeof parseProjectsListResponse === 'function'
|
||||
? parseProjectsListResponse(data).items
|
||||
: (Array.isArray(data) ? data : (data.projects || []));
|
||||
}
|
||||
if (typeof rebuildProjectNameMap === 'function') {
|
||||
rebuildProjectNameMap(list);
|
||||
} else if (typeof projectNameById !== 'undefined') {
|
||||
|
||||
Reference in New Issue
Block a user