Files
knowledge-governance/web/src/QADashboard.tsx
T
姚章浩 cb9f0c389f
build / build (push) Successful in 1m10s
feat(qaops): 看板改用自然月质量指标并增加统计口径
去掉看板个人月明细;十一卡悬停和键盘可读的真实统计范围、公式与排除数。
自然月独立计算;跨月周取周一所在月,保留旧周证据和已存周报正文。

验证:make verify;一次性PG全镜像套件-race;真实恢复数据7个月与周报calc_metrics对拍。
真实浏览器十一卡逐项悬停、Tab/Escape、30秒刷新、跨月周、390px和完整PNG通过。
没有写DobotAI源问答、触发付费模型、外发或应用新迁移。
2026-10-21 08:48:55 +08:00

131 lines
4.2 KiB
TypeScript

import { useEffect, useRef, useState } from 'react'
import type { QAQuery } from './qaOps'
import { fetchQADashboard, liveDashboardQuery } from './qaDashboardData'
import type { QADashboardData } from './qaDashboardData'
import { QADashboardDrawing } from './QADashboardDrawing'
import { createDashboardPNG } from './qaDashboardExport'
import { currentQAWeek } from './qaWeeks'
import './qaDashboard.css'
export interface QADashboardProps {
data: QADashboardData | null
loading: boolean
error: string
onRetry: () => void
}
export function QADashboardView({ data, loading, error, onRetry }: QADashboardProps) {
const host = useRef<HTMLDivElement>(null),
svg = useRef<SVGSVGElement>(null)
const [width, setWidth] = useState(1280),
[exporting, setExporting] = useState(false),
[exportError, setExportError] = useState('')
useEffect(() => {
const element = host.current
if (!element) return
const update = () => setWidth(Math.max(240, Math.floor(element.clientWidth)))
update()
const observer = new ResizeObserver(update)
observer.observe(element)
return () => observer.disconnect()
}, [])
useEffect(() => setExportError(''), [data])
async function download() {
if (!svg.current || !data || loading || error || exporting) return
setExporting(true)
setExportError('')
try {
const blob = await createDashboardPNG(svg.current),
url = URL.createObjectURL(blob),
link = document.createElement('a'),
week = currentQAWeek(new Date(data.week_start))
link.href = url
link.download = `DobotAI_QA_Dashboard_${week.year}_W${String(week.week).padStart(2, '0')}.png`
document.body.append(link)
link.click()
link.remove()
window.setTimeout(() => URL.revokeObjectURL(url), 1000)
} catch (e) {
setExportError(e instanceof Error ? e.message : '看板图片生成失败')
} finally {
setExporting(false)
}
}
return (
<section className="qa-dashboard" aria-label="问题统计看板">
<div className="qa-dashboard-toolbar">
<button type="button" disabled={!data || loading || !!error || exporting} onClick={() => void download()}>
{exporting ? '正在生成图片…' : '导出看板图片'}
</button>
</div>
{error ? (
<div role="alert" className="error-banner">
{error}{' '}
<button type="button" onClick={onRetry}>
重新读取
</button>
</div>
) : null}
{exportError ? (
<div role="alert" className="error-banner">
{exportError}
</div>
) : null}
{loading ? <p role="status">正在读取完整统计范围…</p> : null}
<div className="qa-dashboard-canvas" ref={host}>
{!loading && !error && data ? <QADashboardDrawing data={data} width={width} svgRef={svg} /> : null}
</div>
</section>
)
}
export function QADashboard({
query,
error,
revision,
onRetry,
}: {
query: QAQuery | null
error: string | null
revision: number
onRetry: () => void
}) {
const [data, setData] = useState<QADashboardData | null>(null),
[failure, setFailure] = useState(''),
[loading, setLoading] = useState(false)
useEffect(() => {
setData(null)
setFailure('')
if (!query) {
setLoading(false)
return
}
const abort = new AbortController()
let timer: ReturnType<typeof setTimeout> | undefined
setLoading(true)
const refresh = async () => {
try {
const next = await fetchQADashboard(liveDashboardQuery(query, new Date()), abort.signal)
if (!abort.signal.aborted) {
setData(next)
setFailure('')
}
} catch (e) {
if (!abort.signal.aborted) setFailure(e instanceof Error ? e.message : '统计看板读取失败')
} finally {
if (!abort.signal.aborted) {
setLoading(false)
timer = setTimeout(() => void refresh(), 30000)
}
}
}
void refresh()
return () => {
abort.abort()
if (timer !== undefined) clearTimeout(timer)
}
}, [query, revision])
return (
<div className="qa-dashboard-scroll">
<QADashboardView data={data} loading={loading} error={error || failure} onRetry={onRetry} />
</div>
)
}