刘光辉
3 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import type { TemplateHealthAction, TemplateStatus } from '#/api/x/eln/templates';
 
export type TemplateAction = 'annotate' | 'createVersion' | 'disable' | 'enable' | 'health' | 'publish' | 'setCurrent';
export type HealthAction = 'confirmRemove' | 'locate' | 'repair';
 
const serverHealthActions: Record<HealthAction, TemplateHealthAction> = {
  confirmRemove: 'CONFIRM_REMOVE',
  locate: 'LOCATE',
  repair: 'REPAIR',
};
 
export function templateActions(input: { current: boolean; enabled: boolean; status: TemplateStatus }): TemplateAction[] {
  const actions: TemplateAction[] = [];
  if (input.status === 'DRAFT') actions.push('annotate');
  actions.push('health');
  if (input.status === 'DRAFT') actions.push('publish');
  actions.push('createVersion');
  if (input.status === 'PUBLISHED' && input.enabled && !input.current) actions.push('setCurrent');
  actions.push(input.enabled ? 'disable' : 'enable');
  return actions;
}
 
export function healthActions(input: { actions?: TemplateHealthAction[]; bindingId?: string; code: string }): HealthAction[] {
  const actions = new Set(input.actions);
  return [
    ...(actions.has(serverHealthActions.locate) ? ['locate'] as const : []),
    ...(input.bindingId && actions.has(serverHealthActions.repair) ? ['repair'] as const : []),
    ...(input.bindingId && actions.has(serverHealthActions.confirmRemove) ? ['confirmRemove'] as const : []),
  ];
}