import { useEffect, useRef, useState } from 'react' import type { FormEvent } from 'react' import { useOptionalAuth } from './auth' import { fetchQARecord, fetchQARecords, fetchQAStatus, mergeQAPages, qaCellValue, qaColumns } from './qaOps' import type { QAQuery, QARecord, QARecordPage, QAStatus } from './qaOps' import './qaOps.css' import { QARegions } from './QARegions' import { QAUsage } from './QAUsage' import { QAMonthly } from './QAMonthly' import { QAQualityEditor, QAQualityCounts } from './QAQuality' import { qualityLabels } from './qaQuality' import { usageLabels } from './qaUsage' interface ViewProps { onQualitySaved?: (record: QARecord) => void onReloadDetail?: () => void page: QARecordPage | null status: QAStatus | null loading: boolean error: string | null detail: QARecord | null detailLoading: boolean detailError: string | null onSelect: (id: string) => void onCloseDetail: () => void onLoadMore: () => void onRetry: () => void } const stateLabels = { disabled: '未配置', unavailable: '连接不可用', blocked: '接入被阻断', connected: '已连接' } const warningLabels: Record = { database_temp_enabled: '数据库仍允许临时对象:未视为完全只读验收通过', database_transport_unencrypted: '数据库连接未启用 TLS:上线前须完成传输安全审核', } const stamp = (value: string) => new Date(value).toLocaleString('zh-CN', { hour12: false }) const statusLabels: Record = { succeeded: '成功', failed: '失败', running: '处理中', cancelled: '已取消', } const sourceCoverageLabels: Record = { covered: '已覆盖', uncovered: '未覆盖' } export function QAOperationsView(props: ViewProps) { const page = props.page const columns = qaColumns.filter(({ key }) => page?.columns.includes(key)) const detail = props.detail const showDrawer = Boolean(detail || props.detailLoading || props.detailError) return (
{props.loading ? '正在读取…' : page ? `共 ${page.total} 条 · 已显示 ${page.items.length} 条` : '尚未读取问答'} DobotAI 2.0 / 钉钉 · 独立只读连接{props.status ? ` · ${stateLabels[props.status.state]}` : ''}
{page?.organization ? (

{page.organization.state === 'ready' ? `缺失组织信息按当前通讯录补全(${stamp(page.organization.synced_at!)}),不代表提问时归属。目录:${page.organization.directory_id};区域版本:${page.organization.revision_id || '未保存'}` : '尚无完整通讯录,仅展示源端已有组织快照,不根据姓名推断。'}

) : null} {props.status?.warnings.map((warning) => (

{warningLabels[warning] ?? '数据源有待审核的安全告警'}

))} {props.error ? (
{props.error}{' '}
) : null} {page && page.items.length > 0 ? (
{columns.map(({ key, label }) => ( ))} {page.items.map((item) => ( {columns.map(({ key }) => ( ))} ))}
{label}操作
{qaCellValue(item, key) ?? '—'} {item.quality ? ( {qualityLabels[item.quality.state]} ) : null}
) : page && !props.loading && !props.error ? (

没有符合条件的问答,请调整时间范围或关键词。

) : null} {page ? (
本次读取:{stamp(page.as_of)} · 空列按整个筛选结果隐藏 {page.has_more ? ( ) : page.items.length > 0 ? ( 已显示全部 ) : null}
) : null} {showDrawer ? ( <> {props.detailLoading ?

正在读取完整问答…

: null} {props.detailError ? (

{props.detailError}

) : null} {detail ? (
提问时间
{stamp(detail.asked_at)} {detail.time_source === 'turn.created_at' ? '(问答时间回退,入站消息缺失)' : ''}
提问人
{detail.asker || '未关联'}
部门
{detail.department || '未匹配'} {detail.organization?.department_origin === 'current_directory' ? '(当前通讯录补全)' : ''}
区域
{detail.region || '未配置/待核对'} {detail.organization?.region_origin === 'current_directory' ? '(当前配置补全)' : ''}
{detail.organization ? ( <>
当前组织关联
{detail.organization.state === 'matched' ? '已关联' : (usageLabels[detail.organization.state] ?? detail.organization.state)}
当前通讯录部门/区域
{detail.organization.departments.join(';') || '未匹配'} /{' '} {detail.organization.region || '未配置或待核对'}
组织版本
{detail.organization.version.directory_id || '无完整目录'} /{' '} {detail.organization.version.revision_id || '未保存区域配置'}
源端组织快照
{detail.organization.source_department || '空'} / {detail.organization.source_region || '空'}
) : null}
问答状态
{statusLabels[detail.status] ?? detail.status}
流程触发人员信息
{detail.trigger_person || '未记录'}

原始问题

{detail.question || '无文本问题'}

AI答案

{detail.answer || '尚无答案;不默认判覆盖或正确'}

治理侧覆盖复检

{detail.coverage.verdict === 'uncovered' ? '未覆盖:答案包含“暂无已发布”,引用不能抵消这一结果。' : '待评估:未命中“暂无已发布”不代表已覆盖。'}

规则:{detail.coverage.rule_version}

源端覆盖(仅对照)

{sourceCoverageLabels[detail.source_coverage.verdict] ?? '未记录'} ·{' '} {detail.source_coverage.evaluator_version || '无版本'}

{detail.source_coverage.reason_code || '未记录判定原因'}

{detail.quality ? ( {})} onReload={props.onReloadDetail ?? (() => {})} /> ) : (

质量留档/人工复核未配置。

)}

用户反馈

提问人: {detail.feedback.asker_value === 'up' ? '点赞' : detail.feedback.asker_value === 'down' ? '点踩' : '无反馈'} ;全部反馈:赞 {detail.feedback.up} / 踩 {detail.feedback.down}

引用知识

{JSON.stringify(detail.references, null, 2)}
问题媒体记录
{JSON.stringify(detail.media, null, 2)}
) : null} ) : null}
) } function localInput(value: Date): string { return new Date(value.getTime() - value.getTimezoneOffset() * 60_000).toISOString().slice(0, 16) } function initialDraft() { const end = new Date() return { start: localInput(new Date(end.getTime() - 7 * 24 * 3600_000)), end: localInput(end), keyword: '' } } export function QAOperations() { const [view, setView] = useState<'records' | 'regions' | 'usage'>('records') const [usageMode, setUsageMode] = useState<'regions' | 'monthly'>('regions') const auth = useOptionalAuth() const canRead = auth?.identity?.role === 'admin' const [draft, setDraft] = useState(initialDraft) const [query, setQuery] = useState(null) const [page, setPage] = useState(null) const [status, setStatus] = useState(null) const [error, setError] = useState(null) const [loading, setLoading] = useState(false) const [revision, setRevision] = useState(0) const [qualityRevision, setQualityRevision] = useState(0) const [detail, setDetail] = useState(null) const [detailLoading, setDetailLoading] = useState(false) const [detailError, setDetailError] = useState(null) const generation = useRef(0) const detailRequest = useRef(null) const moreRequest = useRef(null) const returnFocus = useRef(null) useEffect(() => { if (!canRead || query) return setQuery({ start: new Date(draft.start).toISOString(), end: new Date(draft.end).toISOString(), keyword: draft.keyword, cursor: '', }) }, [canRead, draft, query]) useEffect(() => { if (!query || !canRead) return if (view !== 'records') { setLoading(false) moreRequest.current?.abort() detailRequest.current?.abort() setDetail(null) return } const controller = new AbortController() generation.current += 1 moreRequest.current?.abort() detailRequest.current?.abort() setLoading(true) setError(null) setPage(null) setDetail(null) setDetailError(null) setDetailLoading(false) void fetchQAStatus(controller.signal) .then(setStatus) .catch(() => { if (!controller.signal.aborted) setStatus(null) }) void fetchQARecords(query, controller.signal) .then((result) => { if (!controller.signal.aborted) setPage(result) }) .catch((reason: unknown) => { if (!controller.signal.aborted) setError(reason instanceof Error ? reason.message : '问答读取失败') }) .finally(() => { if (!controller.signal.aborted) setLoading(false) }) return () => controller.abort() }, [query, canRead, revision, view]) useEffect( () => () => { detailRequest.current?.abort() moreRequest.current?.abort() }, [] ) const submit = (event: FormEvent) => { event.preventDefault() try { const start = new Date(draft.start), end = new Date(draft.end) if (!(start < end)) throw new Error('开始时间必须早于结束时间') setQuery({ start: start.toISOString(), end: end.toISOString(), keyword: draft.keyword, cursor: '' }) } catch (reason) { setError(reason instanceof Error ? reason.message : '时间格式无效') } } const select = async (id: string) => { detailRequest.current?.abort() returnFocus.current = document.activeElement as HTMLElement | null const controller = new AbortController() detailRequest.current = controller setDetail(null) setDetailError(null) setDetailLoading(true) try { const result = await fetchQARecord(id, controller.signal) if (!controller.signal.aborted) setDetail(result) } catch (reason) { if (!controller.signal.aborted) setDetailError(reason instanceof Error ? reason.message : '详情读取失败') } finally { if (!controller.signal.aborted) setDetailLoading(false) } } const close = () => { detailRequest.current?.abort() setDetail(null) setDetailError(null) setDetailLoading(false) returnFocus.current?.focus() } const loadMore = async () => { if (!query || !page?.has_more || loading) return const currentGeneration = generation.current const controller = new AbortController() moreRequest.current = controller setLoading(true) setError(null) try { const result = await fetchQARecords({ ...query, cursor: page.next_cursor }, controller.signal) if (!controller.signal.aborted && currentGeneration === generation.current) { setPage(mergeQAPages(page, result)) } } catch (reason) { if (!controller.signal.aborted && currentGeneration === generation.current) setError(reason instanceof Error ? reason.message : '翻页失败') } finally { if (!controller.signal.aborted && currentGeneration === generation.current) setLoading(false) } } return (

问答运营 / {view === 'regions' ? '区域归属' : view === 'usage' ? '使用统计' : '问答记录'}

真实问答只读查询;原始记录、源端判断与治理复检分开保留。

{!canRead ? (

尚未配置员工问答可见范围,当前仅管理员可访问。

) : ( <> {view === 'usage' ? ( ) : null} {view === 'usage' && usageMode === 'monthly' ? ( ) : view === 'regions' ? ( ) : ( <>

部门/区域保留源快照,缺失时按当前通讯录补全;使用统计不是绩效结论。外语翻译与AI评估仍暂缓,空值不默认填“否”。

{view === 'records' ? : null} {view === 'usage' ? ( ) : ( { if (detail) void select(detail.id) }} onQualitySaved={(fresh) => { if (fresh.id !== detail?.id) return setDetail(fresh) setPage((old) => { if (!old) return old const all = new Set(old.columns) const f = fresh.quality?.effective if (f) { if (f.coverage) all.add('coverage') if (f.correctness) all.add('is_correct') for (const key of ['product_category', 'problem_type', 'root_cause'] as const) if (f[key]) all.add(key) } return { ...old, items: old.items.map((item) => (item.id === fresh.id ? fresh : item)), columns: qaColumns.filter((c) => all.has(c.key)).map((c) => c.key), } }) setQualityRevision((v) => v + 1) }} onSelect={(id) => void select(id)} onCloseDetail={close} onLoadMore={() => void loadMore()} onRetry={() => setRevision((value) => value + 1)} /> )} )} )}
) }