Files
knowledge-governance/web/src/QAInlineFields.tsx
T
姚章浩 65653e5c01
build / build (push) Successful in 1m14s
fix(qa): 已覆盖正确性改为问题答案二元评估
已覆盖只允许正确或错误,未覆盖可待确认,缺引用不再阻止正确结论。
复用统一事务写入与不可变审计重评旧待确认,保留原问答和人工结论。
验证:全仓 make verify、真实隔离 PostgreSQL 全包与 race 回归通过。
2026-10-24 16:47:09 +08:00

162 lines
5.6 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 './qaPeriodInput.css'
import type { QARecord } from './qaOps'
import { qaColumns } from './qaOps'
import {
editableFieldKeys,
editableFieldValue,
fieldChoices,
fetchFieldOptions,
saveInlineField,
} from './qaInlineFields'
import type { EditableField, FieldOptions } from './qaInlineFields'
import { newOperationID } from './operationKey'
import { qaFieldColors } from './qaFieldColors'
export function QAInlineFields({
record,
onSaved,
onReload,
}: {
record: QARecord
onSaved: (record: QARecord) => void
onReload: () => void
}) {
const [options, setOptions] = useState<FieldOptions | null>(null),
[error, setError] = useState(''),
[notice, setNotice] = useState(''),
[saving, setSaving] = useState(false),
[reloadOptions, setReloadOptions] = useState(0)
const [pending, setPending] = useState<{
record: QARecord
field: EditableField
value: string
nonce: string
} | null>(null)
const writing = useRef<AbortController | null>(null)
useEffect(() => {
const c = new AbortController()
setError('')
void fetchFieldOptions(c.signal)
.then(setOptions)
.catch((e) => {
if (!c.signal.aborted) setError(e instanceof Error ? e.message : '主表选项读取失败')
})
return () => c.abort()
}, [reloadOptions])
useEffect(() => () => writing.current?.abort(), [])
const save = async (p: NonNullable<typeof pending>) => {
if (writing.current) return
const c = new AbortController()
writing.current = c
setSaving(true)
setError('')
setNotice('')
try {
const fresh = await saveInlineField(p.record, p.field, p.value, p.nonce, c.signal)
if (!c.signal.aborted) {
setPending(null)
onSaved(fresh)
setNotice('已保存')
}
} catch (e) {
if (!c.signal.aborted) setError(e instanceof Error ? e.message : '字段保存失败,选择尚未确认')
} finally {
writing.current = null
if (!c.signal.aborted) setSaving(false)
}
}
return (
<div className="qa-inline-fields">
<div className="qa-detail-quality" aria-label="固定质量字段">
{editableFieldKeys.map((field) => {
const value = pending?.field === field ? pending.value : editableFieldValue(record, field)
const choices = fieldChoices(field, options?.[field] ?? [], editableFieldValue(record, 'coverage'))
const rootNotNeeded = field === 'root_cause' && editableFieldValue(record, 'is_correct') === '是'
const downvoteLocked = field === 'is_correct' && record.quality?.feedback_rule === 'downvote-is-incorrect-v1'
return (
<div key={field} data-quality-field={field}>
<label>
<span>{qaColumns.find((c) => c.key === field)!.label}</span>
<select
aria-label={`修改${qaColumns.find((c) => c.key === field)!.label}`}
title={downvoteLocked ? '已点踩,是否正确固定为否' : '选择后自动保存'}
value={rootNotNeeded ? '' : value}
disabled={
saving ||
!!pending ||
!options ||
!record.record_origin ||
!record.quality ||
rootNotNeeded ||
downvoteLocked
}
style={qaFieldColors(field, value || '—')}
onChange={(e) => {
if (e.target.value === editableFieldValue(record, field)) return
try {
const next = { record, field, value: e.target.value, nonce: newOperationID() }
setPending(next)
void save(next)
} catch (e) {
setError(e instanceof Error ? e.message : '无法生成保存标识')
}
}}
>
<option value="" disabled>
{rootNotNeeded ? '无需根因' : '未设置'}
</option>
{value && !choices.includes(value) ? (
<option value={value} disabled>
{value}
</option>
) : null}
{choices.map((v) => (
<option key={v} value={v}>
{v}
</option>
))}
</select>
</label>
</div>
)
})}
</div>
{record.quality?.feedback_rule === 'downvote-is-incorrect-v1' ? (
<p className="qa-inline-save">已点踩,是否正确固定为否</p>
) : null}
{saving || notice ? (
<p className="qa-inline-save" role="status" aria-live="polite">
{saving ? '保存中…' : notice}
</p>
) : null}
{error ? (
<p role="alert" className="error-banner">
{error}{' '}
{pending ? (
<>
<button type="button" disabled={saving} onClick={() => void save(pending)}>
重试保存
</button>
<button
type="button"
disabled={saving}
onClick={() => {
setPending(null)
setError('')
onReload()
}}
>
放弃并重读
</button>
</>
) : (
<button type="button" onClick={() => setReloadOptions((v) => v + 1)}>
重试读取选项
</button>
)}
</p>
) : null}
</div>
)
}