Files
xieyi-weixin-/admin-web/apps/web-antd/src/views/knowledge/batch.ts
T
2026-10-31 09:34:37 +08:00

39 lines
1.8 KiB
TypeScript

import type { KnowledgeItem } from '#/api/knowledge';
import { t } from '#/locales/t';
export type BatchAction = 'approve' | 'delete' | 'disable' | 'publish' | 'reject';
export interface BatchResult { id: string; title: string; status: 'failed' | 'pending' | 'success'; error: string }
export function actionProblem(item: KnowledgeItem, action: BatchAction): string {
const allowed = { delete: ['rejected'], approve: ['draft'], publish: ['approved'], reject: ['draft', 'approved'], disable: ['published'] };
if (!allowed[action].includes(item.status)) return t('当前状态不支持此操作,请刷新或重新选择');
if (action === 'approve' && !item.conditions.trim()) return t('请先编辑并补充适用条件');
return '';
}
export function failureText(error: unknown): string {
const detail = (error as { response?: { data?: { detail?: unknown } } })?.response?.data?.detail;
return typeof detail === 'string' ? detail : t('请求未完成,请刷新确认状态后再操作');
}
/** Each request retains the selected revision; never retry ambiguous mutations automatically. */
export async function runKnowledgeBatch(
items: KnowledgeItem[], execute: (item: KnowledgeItem) => Promise<unknown>,
update: (results: BatchResult[]) => void, stopped: () => boolean,
) {
const results: BatchResult[] = items.map(({ id, title }) => ({ id, title, status: 'pending', error: '' }));
for (let index = 0; index < items.length; index++) {
if (stopped()) break;
const item = items[index]!;
try {
await execute(item);
results[index] = { id: item.id, title: item.title, status: 'success', error: '' };
} catch (error) {
results[index] = { id: item.id, title: item.title, status: 'failed', error: failureText(error) };
}
update(results.map((row) => ({ ...row })));
}
return results;
}