feat(skill-market): 优化筛选界面并恢复真实接口
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
export const PAGE_SIZE = 24;
|
||||
|
||||
// Both data sources filter the entire catalog before paginating. Selected tags use OR.
|
||||
export function queryCatalog(
|
||||
skills,
|
||||
{
|
||||
keyword = '',
|
||||
selectedTags = [],
|
||||
scope = 'all',
|
||||
isMine = () => false,
|
||||
page = 1,
|
||||
pageSize = PAGE_SIZE,
|
||||
} = {},
|
||||
) {
|
||||
const term = keyword.trim().toLocaleLowerCase();
|
||||
const filtered = skills.filter(
|
||||
(skill) =>
|
||||
(scope !== 'mine' || isMine(skill)) &&
|
||||
(!selectedTags.length ||
|
||||
selectedTags.some((tag) => (skill.tags || []).includes(tag))) &&
|
||||
(!term ||
|
||||
`${skill.name || ''} ${skill.description || ''} ${skill.canonical_id || ''}`
|
||||
.toLocaleLowerCase()
|
||||
.includes(term)),
|
||||
);
|
||||
const current = Math.max(
|
||||
1,
|
||||
Math.min(page, Math.ceil(filtered.length / pageSize) || 1),
|
||||
);
|
||||
return {
|
||||
items: filtered.slice((current - 1) * pageSize, current * pageSize),
|
||||
total: filtered.length,
|
||||
page: current,
|
||||
allCount: skills.length,
|
||||
mineCount: skills.filter(isMine).length,
|
||||
};
|
||||
}
|
||||
|
||||
// Keep the existing endpoint and page contract; never send UI scope or multi-tag state.
|
||||
export async function loadMarketCatalog(api, signal) {
|
||||
const skills = new Map();
|
||||
let offset = 0;
|
||||
for (;;) {
|
||||
const response = await api.get('/api/skills/market', {
|
||||
params: {
|
||||
tag: undefined,
|
||||
keyword: undefined,
|
||||
page_size: PAGE_SIZE,
|
||||
offset,
|
||||
},
|
||||
signal,
|
||||
disableDuplicate: true,
|
||||
skipErrorHandler: true,
|
||||
});
|
||||
if (!response.data?.success)
|
||||
throw new Error(response.data?.message || '加载失败');
|
||||
const rows = response.data.data;
|
||||
if (!Array.isArray(rows)) throw new Error('加载失败');
|
||||
const previousSize = skills.size;
|
||||
for (const skill of rows) skills.set(skill.id, skill);
|
||||
offset += rows.length;
|
||||
const total = response.data.total;
|
||||
const hasTotal = Number.isInteger(total) && total >= 0;
|
||||
if ((hasTotal && offset >= total) || (!hasTotal && rows.length < PAGE_SIZE))
|
||||
break;
|
||||
if (!rows.length || skills.size === previousSize)
|
||||
throw new Error('加载失败');
|
||||
if (signal?.aborted) throw new Error('加载失败');
|
||||
}
|
||||
return [...skills.values()];
|
||||
}
|
||||
Reference in New Issue
Block a user