Files
knowledge-governance/web/src/QAUsage.tsx
T
2026-10-18 21:50:29 +08:00

106 lines
4.2 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, useState } from 'react'
import type { QAQuery } from './qaOps'
import { fetchQAUsage, usageLabels } from './qaUsage'
import type { QAUsage as Usage } from './qaUsage'
interface Props {
data: Usage | null
loading: boolean
error: string
onRetry: () => void
}
const stamp = (v: string) => new Date(v).toLocaleString('zh-CN', { hour12: false })
export function QAUsageView({ data, loading, error, onRetry }: Props) {
return (
<section className="qa-ops-results" aria-label="使用统计">
<p className="qa-ops-slice-note">
按当前通讯录回看历史问答,不代表提问时的组织归属。成功/失败是源端运行状态,不等于答案正确;不输出绩效结论。
</p>
{loading ? <p role="status">正在读取完整筛选范围的使用统计…</p> : null}
{error ? (
<p className="error-banner" role="alert">
{error}{' '}
<button type="button" onClick={onRetry} disabled={loading}>
重试统计
</button>
</p>
) : null}
{data && !error ? (
<>
<div className="qa-ops-result-bar">
<span>
共 {data.questions} 条问答 · 已关联提问人数 {data.identified_users}
</span>
<span>统计截止读取时间:{stamp(data.as_of)}</span>
</div>
<p className="qa-ops-muted qa-ops-organization">
范围:{stamp(data.start)} 至 {stamp(data.end)}(不含结束时刻)
{data.keyword ? ` · 原问题包含:${data.keyword}` : ''}
</p>
<p className="qa-ops-muted qa-ops-organization">
{data.organization.state === 'ready'
? `通讯录同步:${stamp(data.organization.synced_at!)} · 目录 ${data.organization.directory_id} · 区域配置 ${data.organization.revision_id || '尚未保存'}`
: '尚无完整通讯录,当前不能确定部门区域或去重人数。'}
</p>
<div className="qa-ops-table-scroll" tabIndex={0} aria-label="使用统计表格滚动区">
<table className="qa-ops-table qa-usage-table">
<thead>
<tr>
<th>区域/待核对状态</th>
<th>问答条数</th>
<th>成功</th>
<th>失败</th>
<th>其他状态</th>
<th>已关联提问人数</th>
<th>通讯录归属人数</th>
</tr>
</thead>
<tbody>
{data.rows.map((row) => (
<tr key={row.key}>
<td>{usageLabels[row.key] ?? row.key}</td>
<td>{row.questions}</td>
<td>{row.succeeded}</td>
<td>{row.failed}</td>
<td>{row.other}</td>
<td>{row.users ?? '不可确定'}</td>
<td>{row.roster_people ?? '不适用'}</td>
</tr>
))}
</tbody>
</table>
</div>
<p className="qa-ops-muted qa-ops-organization">
按问答唯一 ID 计次;同一名册人员的多部门、多个身份记录只计 1
人。未匹配或身份冲突不编造人数;通讯录人数不是在职或考核人数。
</p>
</>
) : null}
</section>
)
}
export function QAUsage({ query }: { query: QAQuery | null }) {
const [data, setData] = useState<Usage | null>(null),
[loading, setLoading] = useState(false),
[error, setError] = useState(''),
[revision, setRevision] = useState(0)
useEffect(() => {
if (!query) return
const c = new AbortController()
setLoading(true)
setData(null)
setError('')
void fetchQAUsage(query, c.signal)
.then((value) => {
if (!c.signal.aborted) setData(value)
})
.catch((e: unknown) => {
if (!c.signal.aborted) setError(e instanceof Error ? e.message : '统计读取失败')
})
.finally(() => {
if (!c.signal.aborted) setLoading(false)
})
return () => c.abort()
}, [query, revision])
return <QAUsageView data={data} loading={loading} error={error} onRetry={() => setRevision((v) => v + 1)} />
}