File size: 4,628 Bytes
5afd2f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import {useMemo, useState} from 'react';
import type {NPFamily} from './contract.ts';
import {allocate, buildTree, familyCounts, quotaLeaves, quotaValue, unlock} from './quota.ts';
import type {QuotaNode, QuotaResult, QuotaTargets} from './quota.ts';
import './quota.css';

export function QuotaTree({families, targets, onChange}: {families: NPFamily[]; targets: QuotaTargets; onChange: (targets: QuotaTargets) => void}) {
  const tree = useMemo(() => buildTree(families), [families]);
  const [expanded, setExpanded] = useState<string[]>([]), [error, setError] = useState('');
  const leaves = quotaLeaves(tree), total = quotaValue(tree, targets.versions);
  const supported = leaves.filter(v => v.supported).reduce((n, v) => n + quotaValue(v, targets.versions), 0);
  function apply(result: QuotaResult) {
    setError(result.error || '');
    if (!result.error) onChange({total: quotaValue(tree, result.versions), versions: result.versions, locked: result.locked});
  }
  function family(node: QuotaNode) {   // 母题卡片(配置屏 1-2):绿条=能出题的版本占比
    const c = familyCounts(node);
    return <div className="quota-family">
      <span>单机身 {c.single} · 多机身 {c.multi} 个版本</span>
      <div className="quota-bar" role="progressbar" aria-label={node.name + ' 能出题的版本'} aria-valuemin={0} aria-valuemax={c.total} aria-valuenow={c.supported}><div style={{width: (c.total ? 100 * c.supported / c.total : 0) + '%'}} /></div>
      <span>能出题 {c.supported} / {c.total}</span>
      {!!node.paused?.length && <details className="quota-paused"><summary>暂停 {node.paused.length} 版</summary>
        <ul>{node.paused.map(p => <li key={p.id}>{p.id}:{p.reason || '目录里没写原因'}</li>)}</ul></details>}
    </div>;
  }
  function row(node: QuotaNode, depth: number) {
    const versions = quotaLeaves(node), open = expanded.includes(node.id), isLocked = targets.locked.includes(node.id);
    return <div key={node.id}>
      <div className="quota-row" style={{paddingLeft: depth * 18}} id={'np-' + node.id.replace(/^f:/, '')}>
        {node.children.length ? <button type="button" className="quota-expand" aria-expanded={open} aria-label={(open ? '收起 ' : '展开 ') + node.name} onClick={() => setExpanded(open ? expanded.filter(id => id !== node.id) : [...expanded, node.id])}>{open ? '▾' : '▸'}</button> : <span className="quota-indent" />}
        <label className="quota-name" htmlFor={'quota-' + node.id}>{node.name}</label>
        {depth === 0 && <button type="button" className="quota-step" aria-label={'减少 ' + node.name} disabled={quotaValue(node, targets.versions) <= 0} onClick={() => apply(allocate(tree, targets.versions, targets.locked, node.id, quotaValue(node, targets.versions) - 1))}>−</button>}
        <input id={'quota-' + node.id} aria-label={node.name + ' 题数'} type="number" min="0" step="1" value={quotaValue(node, targets.versions)} onChange={e => apply(allocate(tree, targets.versions, targets.locked, node.id, e.target.valueAsNumber))} />
        {depth === 0 && <button type="button" className="quota-step" aria-label={'增加 ' + node.name} onClick={() => apply(allocate(tree, targets.versions, targets.locked, node.id, quotaValue(node, targets.versions) + 1))}>+</button>}
        <button type="button" className="quota-lock" disabled={!isLocked} aria-label={'解锁 ' + node.name} title={isLocked ? '解锁并按父级总数重新均分' : '修改后锁定'} onClick={() => apply(unlock(tree, targets.versions, targets.locked, node.id))}>{isLocked ? '🔒' : '—'}</button>
        <span className="quota-status">{node.version ? node.supported ? '能出' : '还不能出' : `能出 ${versions.filter(v => v.supported).length} / ${versions.length} 个版本`}</span>
      </div>
      {depth === 0 && !node.version && family(node)}
      {open && node.children.map(c => row(c, depth + 1))}
    </div>;
  }
  return <div className="quota-tree"><div className="quota-total"><label>总数 <input aria-label="总题数" type="number" min="0" step="1" value={total} onChange={e => apply(allocate(tree, targets.versions, targets.locked, 'total', e.target.valueAsNumber))} /></label>{targets.locked.includes('total') && <button type="button" aria-label="解锁总数" onClick={() => apply(unlock(tree, targets.versions, targets.locked, 'total'))}>🔒</button>}<span>能出题的版本共 {supported} 道 · 还出不了的 {total - supported} 道(这些版本生成器或证明器还不支持)</span></div>{error && <p role="alert" className="np-error">{error}</p>}{tree.children.map(c => row(c, 0))}</div>;
}