import { useEffect, useState } from 'react' import type { FormEvent } from 'react' import { beijingMonth, fetchQAMonthly } from './qaMonthly' import type { MonthlyPeople, MonthlyQuery } from './qaMonthly' import { usageLabels } from './qaUsage' const stamp = (v: string) => new Date(v).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai', hour12: false }) interface ViewProps { data: MonthlyPeople | null loading: boolean error: string onRetry: () => void onPage: (page: number) => void } export function QAMonthlyView({ data, loading, error, onRetry, onPage }: ViewProps) { return (
{loading ? (

正在读取个人月明细…

) : null} {error ? (
{error}{' '}
) : null} {data && !loading && !error ? ( <>
{data.month} · {data.partial ? '当月进度 / 截止时点' : '完整自然月时间范围'} 仅 DobotAI 2.0 · 北京时间

当前通讯录:{data.roster_people} 人 · 已关联提问 {data.used_people} 人 · 本源零记录 {data.zero_people} 人

本月问答 {data.questions} 条 · 已关联 {data.linked_questions} 条 · 未关联 {data.unresolved_questions} 条

{data.missing_names > 0 ? (

{data.missing_names} 人姓名未同步,请重新同步通讯录。

) : null}
统计范围与组织版本

范围:{stamp(data.start)} 至 {stamp(data.end)}(不含结束)。当前通讯录同步于{' '} {stamp(data.organization.synced_at!)} ;目录: {data.organization.directory_id};区域:{data.organization.revision_id || '未保存'}。

符合人员筛选 {data.total} 人 · 第 {data.page} 页
{data.rows.length ? (
{['姓名', '当前部门完整路径', '区域 / 待核对状态', '问答次数', '成功', '失败', '其他'].map((v) => ( ))} {data.rows.map((row) => ( ))}
{v}
{row.name || '姓名未同步'} {row.departments.join(';') || '无部门记录'} {usageLabels[row.region || row.state] ?? (row.region || row.state)} {row.questions} {row.succeeded} {row.failed} {row.other}
) : (

没有符合人员筛选的记录,请调整条件。

)} ) : null}
) } export function QAMonthly() { const [draft, setDraft] = useState(() => ({ month: beijingMonth(), cutoff: '', region: '', department_id: '', activity: 'all', search: '', })) const [request, setRequest] = useState({ month: draft.month }) const [data, setData] = useState(null), [options, setOptions] = useState([]), [regionOptions, setRegionOptions] = useState([]), [loading, setLoading] = useState(false), [error, setError] = useState('') useEffect(() => { const controller = new AbortController() setLoading(true) setData(null) setError('') fetchQAMonthly(request, controller.signal) .then((next) => { if (!controller.signal.aborted) { setData(next) setOptions(next.department_options) setRegionOptions(next.organization.region_names ?? []) } }) .catch((reason) => { if (!controller.signal.aborted) setError(reason instanceof Error ? reason.message : '个人月明细读取失败') }) .finally(() => { if (!controller.signal.aborted) setLoading(false) }) return () => controller.abort() }, [request]) const change = (key: keyof typeof draft, value: string) => { setDraft({ ...draft, [key]: value }) setData(null) setError('') } const query = () => { try { setRequest({ ...draft, cutoff: draft.cutoff ? new Date(draft.cutoff + '+08:00').toISOString() : undefined, page: 1, }) } catch { setData(null) setError('截止时间无效,请按北京时间填写') } } const submit = (event: FormEvent) => { event.preventDefault() query() } const page = (n: number) => { if (data) setRequest({ ...request, cutoff: data.end, page: n, directory_id: data.organization.directory_id, revision_id: data.organization.revision_id, }) } return ( <>
) }