Files
knowledge-governance/web/src/QARegions.tsx
T

247 lines
9.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useEffect, useRef, useState } from 'react'
import { changeRegionRule, fetchOrganization, previewOrganization, regionNames, saveOrganization } from './qaOrg'
import type { Organization, RegionRule } from './qaOrg'
import './qaRegions.css'
interface Props {
state: Organization | null
preview: Organization | null
rules: RegionRule[]
selectedRegion: string
search: string
busy: boolean
error: string
notice: string
dirty: boolean
onRegion: (value: string) => void
onSearch: (value: string) => void
onToggle: (id: string, checked: boolean) => void
onRemoveMissing: (id: string) => void
onPreview: () => void
onSave: () => void
onReload: () => void
}
export function RegionMappingView(p: Props) {
const display = p.preview ?? p.state
const available = p.state?.state === 'ready'
const departments =
display?.view.departments.filter((d) => d.path.toLowerCase().includes(p.search.toLowerCase())) ?? []
return (
<section className="qa-regions" aria-label="区域归属配置">
<div className="qa-region-heading">
<h3>区域归属配置</h3>
<button type="button" disabled={p.busy} onClick={p.onReload}>
{p.dirty ? '放弃修改并重读' : '重新读取'}
</button>
</div>
{p.state?.directory_synced_at ? (
<p className="qa-ops-muted">
{p.state.directory_source === 'weekly_report_snapshot' ? '历史快照' : '全量同步快照'} · 通讯录同步时间:
{new Date(p.state.directory_synced_at).toLocaleString('zh-CN', { hour12: false })} · 配置版本:
{p.state.revision_id || '尚未保存'}
</p>
) : null}
<p className="qa-ops-muted">
父部门包含子部门,子部门单独配置优先。配置区域归属不等于纳入绩效考核;跨区域多部门人员先待核对。
</p>
{p.error ? (
<p role="alert" className="error-banner">
{p.error}
</p>
) : null}
{p.notice ? <p role="status">{p.notice}</p> : null}
{!p.state ? (
<p role="status">{p.busy ? '正在读取通讯录…' : '尚未读取通讯录'}</p>
) : !available ? (
<p className="qa-ops-empty">尚无完整通讯录快照。先接入已验证的全量同步结果,不能使用按需人员缓存或编造部门。</p>
) : (
<>
<div className="qa-region-filters">
<label>
选择区域
<select value={p.selectedRegion} disabled={p.busy} onChange={(e) => p.onRegion(e.target.value)}>
{regionNames.map((r) => (
<option key={r}>{r}</option>
))}
</select>
</label>
<label>
搜索完整部门路径
<input
type="search"
value={p.search}
onChange={(e) => p.onSearch(e.target.value)}
placeholder="输入部门名称或完整路径"
/>
</label>
<button type="button" disabled={p.busy || !p.dirty} onClick={p.onPreview}>
预览影响
</button>
<button type="button" disabled={p.busy || !p.dirty || !p.preview} onClick={p.onSave}>
保存归属
</button>
</div>
{display ? (
<div className="qa-region-preview" aria-label="归属人数预览">
<strong>{p.preview ? '预览结果(未保存)' : '已保存归属'}</strong>
<span>通讯录人数:{display.view.total_people}</span>
{regionNames.map((r) => (
<span key={r}>
{r}:{display.view.regions[r]}
</span>
))}
<span>未归属:{display.view.unassigned_people}</span>
<span>跨区域待核对:{display.view.conflicting_people}</span>
</div>
) : null}
{p.dirty && !p.preview ? (
<p className="qa-ops-muted">勾选已修改,人数仍为已保存结果;请预览后保存。</p>
) : null}
{display?.view.missing_departments.length ? (
<div role="alert" className="qa-ops-warning">
部分已配置部门不在当前通讯录中,原规则保留,需核对:
{display.view.missing_departments.map((id) => (
<span key={id}>
{id}{' '}
<button type="button" disabled={p.busy} onClick={() => p.onRemoveMissing(id)}>
移除此规则
</button>
</span>
))}
</div>
) : null}
<div className="qa-region-tree-scroll">
<table className="qa-region-table">
<thead>
<tr>
<th>直接配置</th>
<th>完整部门路径</th>
<th>{p.dirty && !p.preview ? '已保存归属' : '生效归属'}</th>
<th>依据</th>
</tr>
</thead>
<tbody>
{departments.map((d) => (
<tr key={d.id}>
<td>
<input
type="checkbox"
aria-label={`将 ${d.path} 直接归入 ${p.selectedRegion}`}
checked={p.rules.some((r) => r.department_id === d.id && r.region === p.selectedRegion)}
disabled={p.busy}
onChange={(e) => p.onToggle(d.id, e.target.checked)}
/>
</td>
<td>{d.path}</td>
<td>{d.region || '未归属'}</td>
<td>{d.explicit ? '直接配置' : d.inherited_from ? '继承上级' : '未配置'}</td>
</tr>
))}
</tbody>
</table>
{!departments.length ? <p>没有匹配的部门路径</p> : null}
</div>
<p className="qa-ops-muted">
此处仅预览通讯录归属人数;2.0 使用次数、考核名单与绩效统计尚未接入。不会将未知在职状态当作已确认在职人数。
</p>
</>
)}
</section>
)
}
function signature(rules: RegionRule[]) {
return JSON.stringify([...rules].sort((a, b) => a.department_id.localeCompare(b.department_id)))
}
export function QARegions() {
const [state, setState] = useState<Organization | null>(null),
[preview, setPreview] = useState<Organization | null>(null),
[rules, setRules] = useState<RegionRule[]>([])
const [region, setRegion] = useState<string>(regionNames[0]),
[search, setSearch] = useState(''),
[busy, setBusy] = useState(false),
[error, setError] = useState(''),
[notice, setNotice] = useState('')
const request = useRef<AbortController | null>(null)
const accept = (value: Organization) => {
setState(value)
setRules(value.rules)
setPreview(null)
}
const load = async () => {
request.current?.abort()
const controller = new AbortController()
request.current = controller
setBusy(true)
setError('')
setNotice('')
setPreview(null)
try {
const value = await fetchOrganization(controller.signal)
if (!controller.signal.aborted) accept(value)
} catch (e) {
if (!controller.signal.aborted) setError(e instanceof Error ? e.message : '读取失败')
} finally {
if (!controller.signal.aborted) setBusy(false)
}
}
useEffect(() => {
void load()
return () => request.current?.abort()
}, [])
const edit = (next: RegionRule[]) => {
setRules(next)
setPreview(null)
setNotice('')
setError('')
}
const act = async (save: boolean) => {
if (!state || busy || (save && !preview)) return
const controller = new AbortController()
request.current?.abort()
request.current = controller
setBusy(true)
setError('')
setNotice('')
try {
const value = await (save
? saveOrganization(state, rules, controller.signal)
: previewOrganization(state, rules, controller.signal))
if (!controller.signal.aborted) {
if (save) {
const readback = await fetchOrganization(controller.signal)
if (controller.signal.aborted) return
accept(readback)
if (readback.revision_id !== value.revision_id) throw new Error('保存后配置再次变化,已读取最新版本,请核对')
setNotice('区域归属已保存并重新读取')
} else setPreview(value)
}
} catch (e) {
if (!controller.signal.aborted) {
setPreview(null)
setError(e instanceof Error ? e.message : '操作失败')
}
} finally {
if (!controller.signal.aborted) setBusy(false)
}
}
return (
<RegionMappingView
state={state}
preview={preview}
rules={rules}
selectedRegion={region}
search={search}
busy={busy}
error={error}
notice={notice}
dirty={Boolean(state && signature(rules) !== signature(state.rules))}
onRegion={setRegion}
onSearch={setSearch}
onToggle={(id, checked) => edit(changeRegionRule(rules, id, region, checked))}
onRemoveMissing={(id) => edit(rules.filter((r) => r.department_id !== id))}
onPreview={() => void act(false)}
onSave={() => void act(true)}
onReload={() => void load()}
/>
)
}