前端报告详情页添加HTML Dashboard查看链接

This commit is contained in:
D-Robotics 2026-07-02 22:26:28 +08:00
parent 3e05b83e05
commit fa240afad8

View File

@ -0,0 +1,215 @@
import { useState, useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import { CheckCircle2, XCircle, Eye, FileText, ExternalLink } from 'lucide-react';
import { PRODUCTS } from '../layout/navConfig';
interface Report {
task_id: string;
task_name: string;
product: string;
total_runs: number;
pass: number;
fail: number;
result: string;
started_at: string;
finished_at: string;
thumbnail: string | null;
}
interface ReportDetail extends Report {
results: Array<{
name: string;
desc: string;
status: string;
duration: string;
module: string;
}>;
logs: Array<{ ts: string; level: string; msg: string }>;
screenshots: string[];
html_report?: string;
}
export default function ReportsView() {
const location = useLocation();
const platform = location.pathname.startsWith('/dcloud') ? 'dcloud' : 'robogo';
const [reports, setReports] = useState<Report[]>([]);
const [detail, setDetail] = useState<ReportDetail | null>(null);
useEffect(() => {
fetch(`/api/reports?product=${platform}`)
.then(r => r.json())
.then(setReports)
.catch(console.error);
}, []);
const viewReport = async (tid: string) => {
const res = await fetch(`/api/reports/${tid}`);
if (res.ok) setDetail(await res.json());
};
return (
<div className="space-y-4">
<h2 className="text-sm font-medium text-[rgb(var(--color-text-primary))]">
报告中心 ({reports.length})
</h2>
{!detail ? (
<div className="bg-[rgb(var(--color-surface))] border border-[rgb(var(--color-border))] rounded-xl overflow-hidden">
<table className="w-full text-sm">
<thead>
<tr className="border-b border-[rgb(var(--color-border))] text-xs text-[rgb(var(--color-text-secondary))]">
<th className="text-left px-4 py-3 font-medium">任务</th>
<th className="text-left px-4 py-3 font-medium">产品</th>
<th className="text-left px-4 py-3 font-medium">结果</th>
<th className="text-left px-4 py-3 font-medium">通过/总计</th>
<th className="text-left px-4 py-3 font-medium">时间</th>
<th className="text-right px-4 py-3 font-medium">操作</th>
</tr>
</thead>
<tbody>
{reports.length === 0 && (
<tr>
<td colSpan={6} className="text-center py-12 text-[rgb(var(--color-text-secondary))] text-xs">
暂无报告
</td>
</tr>
)}
{reports.map(r => (
<tr key={r.task_id} className="border-b border-[rgb(var(--color-border)/0.5)] hover:bg-[rgb(var(--color-surface-hover))] transition-colors">
<td className="px-4 py-3 text-xs text-[rgb(var(--color-text-primary))]">{r.task_name}</td>
<td className="px-4 py-3 text-xs text-[rgb(var(--color-text-secondary))]">{PRODUCTS[r.product] || r.product}</td>
<td className="px-4 py-3">
<div className="flex items-center gap-1">
{r.result === 'PASS' ? (
<CheckCircle2 className="w-4 h-4 text-[rgb(var(--color-success))]" />
) : (
<XCircle className="w-4 h-4 text-[rgb(var(--color-error))]" />
)}
<span className={`text-xs ${r.result === 'PASS' ? 'text-[rgb(var(--color-success))]' : 'text-[rgb(var(--color-error))]'}`}>
{r.result === 'PASS' ? '通过' : '失败'}
</span>
</div>
</td>
<td className="px-4 py-3 text-xs text-[rgb(var(--color-text-primary))]">{r.pass}/{r.total_runs}</td>
<td className="px-4 py-3 text-xs text-[rgb(var(--color-text-secondary))]">
{r.finished_at?.split('T')[1]?.slice(0, 5) || '-'}
</td>
<td className="px-4 py-3 text-right">
<button
onClick={() => viewReport(r.task_id)}
className="p-1.5 rounded hover:bg-[rgb(var(--color-accent-amber)/0.2)] text-[rgb(var(--color-accent-amber))] cursor-pointer"
title="查看详情"
>
<Eye className="w-3.5 h-3.5" />
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
) : (
<div className="space-y-4">
<button
onClick={() => setDetail(null)}
className="text-xs text-[rgb(var(--color-accent-amber))] hover:underline cursor-pointer"
>
&larr; 返回列表
</button>
<div className="bg-[rgb(var(--color-surface))] border border-[rgb(var(--color-border))] rounded-xl p-5">
<div className="flex items-center justify-between mb-4">
<div>
<h3 className="text-sm font-semibold text-[rgb(var(--color-text-primary))]">{detail.task_name}</h3>
<p className="text-xs text-[rgb(var(--color-text-secondary))]">Product: {PRODUCTS[detail.product] || detail.product}</p>
</div>
<div className={`px-3 py-1 rounded-lg text-xs font-medium ${
detail.result === 'PASS' ? 'bg-[rgb(var(--color-success)/0.2)] text-[rgb(var(--color-success))]' : 'bg-[rgb(var(--color-error)/0.2)] text-[rgb(var(--color-error))]'
}`}>
{detail.result === 'PASS' ? '通过' : '失败'}
</div>
</div>
<div className="grid grid-cols-4 gap-4 mb-6">
<div className="text-center">
<div className="text-lg font-bold text-[rgb(var(--color-text-primary))]">{detail.total_runs}</div>
<div className="text-xs text-[rgb(var(--color-text-secondary))]">总计运行</div>
</div>
<div className="text-center">
<div className="text-lg font-bold text-[rgb(var(--color-success))]">{detail.pass}</div>
<div className="text-xs text-[rgb(var(--color-text-secondary))]">通过</div>
</div>
<div className="text-center">
<div className="text-lg font-bold text-[rgb(var(--color-error))]">{detail.fail}</div>
<div className="text-xs text-[rgb(var(--color-text-secondary))]">失败</div>
</div>
<div className="text-center">
<div className="text-lg font-bold text-[rgb(var(--color-text-primary))]">
{detail.total_runs > 0 ? Math.round(detail.pass / detail.total_runs * 100) : 0}%
</div>
<div className="text-xs text-[rgb(var(--color-text-secondary))]">通过率</div>
</div>
</div>
{detail.html_report && (
<div className="mb-4">
<a
href={detail.html_report}
target="_blank"
rel="noopener noreferrer"
className="inline-flex items-center gap-2 px-4 py-2 bg-[rgb(var(--color-accent)/0.1)] text-[rgb(var(--color-accent))] text-xs font-medium rounded-lg hover:bg-[rgb(var(--color-accent)/0.2)] transition-colors"
>
<ExternalLink className="w-3.5 h-3.5" />
查看完整测试报告 Dashboard
</a>
</div>
)}
{detail.results.length > 0 && (
<div>
<div className="text-xs font-medium text-[rgb(var(--color-text-primary))] mb-3 flex items-center gap-2">
<FileText className="w-3.5 h-3.5" />
测试结果
</div>
<div className="space-y-2">
{detail.results.map((r, i) => (
<div key={i} className="flex items-center justify-between bg-[rgb(var(--color-surface-hover))] rounded-lg px-4 py-3">
<div>
<div className="text-xs text-[rgb(var(--color-text-primary))]">{r.desc || r.name}</div>
<div className="text-[10px] text-[rgb(var(--color-text-secondary))]">{r.module}</div>
</div>
<div className="flex items-center gap-3">
<span className="text-xs text-[rgb(var(--color-text-secondary))]">{r.duration}</span>
{r.status === 'PASS' ? (
<CheckCircle2 className="w-4 h-4 text-[rgb(var(--color-success))]" />
) : (
<XCircle className="w-4 h-4 text-[rgb(var(--color-error))]" />
)}
</div>
</div>
))}
</div>
</div>
)}
{detail.screenshots.length > 0 && (
<div className="mt-6">
<div className="text-xs font-medium text-[rgb(var(--color-text-primary))] mb-3">截图</div>
<div className="flex gap-2 flex-wrap">
{detail.screenshots.map(s => (
<img
key={s}
src={`/artifacts/screenshots/${s}`}
alt={s}
className="w-32 h-24 object-cover rounded-lg border border-[rgb(var(--color-border))]"
/>
))}
</div>
</div>
)}
</div>
</div>
)}
</div>
);
}