| 1 | import React, { useEffect, useState } from "react"; |
| 2 | import { ArrowDownToLine, LoaderCircle } from "lucide-react"; |
| 3 | import { api, native, nativeAvailable } from "./api"; |
| 4 | import { reportDiagnostic } from "./diagnostics.mjs"; |
| 5 | import { isRemoteClient, runtimeURL } from "./client-context.mjs"; |
| 6 | |
| 7 | export function DiagnosticSettings() { |
| 8 | const remote = isRemoteClient(); |
| 9 | const [info, setInfo] = useState(null); |
| 10 | const [busy, setBusy] = useState(false); |
| 11 | const [notice, setNotice] = useState(""); |
| 12 | const [error, setError] = useState(""); |
| 13 | useEffect(() => { |
| 14 | let live = true; |
| 15 | api("/diagnostics").then(value => { if (live) setInfo(value); }) |
| 16 | .catch(error => { if (live) setError(error.message); }); |
| 17 | return () => { live = false; }; |
| 18 | }, []); |
| 19 | const save = async () => { |
| 20 | setBusy(true); setError(""); setNotice(""); |
| 21 | try { |
| 22 | if (nativeAvailable()) { |
| 23 | const result = await native("exportDiagnostics"); |
| 24 | if (!result?.canceled) setNotice("诊断包已保存。分享前请检查其中内容。"); |
| 25 | } else { |
| 26 | |
| 27 | const response = await fetch(runtimeURL("/diagnostics/export")); |
| 28 | if (!response.ok) throw new Error((await response.text()).trim() || "诊断包导出失败"); |
| 29 | if (!response.headers.get("content-type")?.startsWith("application/zip")) throw new Error("当前后台不支持诊断导出,请重新打开 Cage"); |
| 30 | const url = URL.createObjectURL(await response.blob()); |
| 31 | const link = document.createElement("a"); link.href = url; link.download = "cage-diagnostics.zip"; link.click(); |
| 32 | setTimeout(() => URL.revokeObjectURL(url), 30_000); |
| 33 | setNotice("诊断包已下载。分享前请检查其中内容。"); |
| 34 | } |
| 35 | } catch (error) { setError(error.message); } |
| 36 | finally { setBusy(false); } |
| 37 | }; |
| 38 | return <section className="settings-group" aria-label="日志与诊断"> |
| 39 | <h3>日志与诊断</h3> |
| 40 | <p className="footnote">自动记录应用故障并脱敏,保留 14 天。每个后台日志上限 40 MiB,环境服务预算 10 MiB。诊断包会同时收集桌面连接与当前环境的记录,最大 96 MiB。采集范围限于故障日志与运行状态。</p> |
| 41 | {info && <p className="footnote">{remote ? "服务器" : "本机"}日志 {(info.bytes / 1024 / 1024).toFixed(1)} MiB · {info.files} 个文件</p>} |
| 42 | <button className="secondary-button" disabled={busy} onClick={save}> |
| 43 | {busy ? <LoaderCircle size={14} className="spin" /> : <ArrowDownToLine size={14} />} |
| 44 | {busy ? "正在导出…" : remote ? "导出服务器诊断包" : "导出诊断包"} |
| 45 | </button> |
| 46 | <p className="footnote">{remote ? "诊断包包含本机界面与当前云端主机的脱敏日志、版本和收集状态,需要保持远程连接。导出后保存在你选择的本机位置。" : "诊断包包含界面与环境的脱敏日志、版本和收集状态,环境离线时仍可导出本机记录。只保存在你选择的位置。"}</p> |
| 47 | {notice && <p role="status">{notice}</p>} |
| 48 | {error && <p role="alert" className="error-message">{error}</p>} |
| 49 | </section>; |
| 50 | } |
| 51 | |
| 52 | export class DiagnosticBoundary extends React.Component { |
| 53 | state = { failed: false }; |
| 54 | static getDerivedStateFromError() { return { failed: true }; } |
| 55 | componentDidCatch(error, info) { reportDiagnostic("react", new Error(`${error.message}\n${error.stack || ""}\n${info.componentStack || ""}`)); } |
| 56 | render() { |
| 57 | if (!this.state.failed) return this.props.children; |
| 58 | return <main style={{ padding: 32 }}> |
| 59 | <h2>工作台显示遇到异常</h2> |
| 60 | <p>后台任务仍可继续运行。可以导出诊断包,再重新加载界面。</p> |
| 61 | <DiagnosticSettings /> |
| 62 | <button className="primary-button" onClick={() => location.reload()}>重新加载界面</button> |
| 63 | </main>; |
| 64 | } |
| 65 | } |