Files
monkey-ai-web/src/pages/CommissionV2/latestTiers.js
T

88 lines
3.1 KiB
JavaScript

/*
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 function latestTierURL(filters = {}, page = 1, snapshot = {}) {
const params = new URLSearchParams({
view: 'latest',
p: String(page),
page_size: String(filters.pageSize ?? 100),
});
if (filters.query?.trim()) params.set('q', filters.query.trim());
if (filters.type) params.set('source_type', filters.type);
const status =
{ enabled: 'active', scheduled: 'pending' }[filters.status] ||
filters.status;
if (status && status !== 'all') params.set('status', status);
if (snapshot.as_of) params.set('as_of', String(snapshot.as_of));
if (snapshot.revision != null)
params.set('revision', String(snapshot.revision));
return `/api/commission/v2/tiers?${params}`;
}
export async function fetchAllLatestTiers(fetchJSON, filters = {}) {
const rows = [];
let snapshot;
let total;
for (let page = 1; ; page++) {
const result = await fetchJSON(
latestTierURL({ ...filters, pageSize: 100 }, page, snapshot),
);
if (!snapshot) {
snapshot = { as_of: result.as_of, revision: result.revision };
total = result.total;
}
if (
result.as_of !== snapshot.as_of ||
result.revision !== snapshot.revision ||
result.total !== total
)
throw new Error('档位配置已变化,请重新加载或重新导出');
if (
!Array.isArray(result.items) ||
(rows.length < total && !result.items.length)
)
throw new Error('无法取得完整档位列表,请重试');
rows.push(...result.items);
if (rows.length === total) return rows;
if (rows.length > total) throw new Error('档位配置已变化,请重新导出');
}
}
export function mergeIdentityOptions(previous, next, selected) {
return [
...new Map(
[...(selected ? [selected] : []), ...previous, ...next].map((item) => [
Number(item.id),
item,
]),
).values(),
];
}
export function createRequestGeneration() {
let generation = 0;
return { next: () => ++generation, current: (value) => generation === value };
}
export function identityOptionLabel(item) {
const name =
item.name || item.display_name || item.username || item.code || '';
const identifier = item.name ? item.code : item.username;
return `${name}${identifier && identifier !== name ? ` · ${identifier}` : ''} (#${item.id})${item.disabled_reason ? ` · ${item.disabled_reason}` : ''}`;
}