From c27f44ae49e86fad88ec6a5f20764b749c23df6b Mon Sep 17 00:00:00 2001 From: Michael Barroco Date: Thu, 17 Aug 2023 16:33:08 +0200 Subject: [PATCH] [report] load json embed in script tag --- reports/src/App.tsx | 8 ++++---- reports/src/capability/useReport.tsx | 24 ++++++++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/reports/src/App.tsx b/reports/src/App.tsx index dfa13bd..7c1cc82 100644 --- a/reports/src/App.tsx +++ b/reports/src/App.tsx @@ -5,15 +5,15 @@ import "./App.css"; function App() { // FIXME use the report from the config // eslint-disable-next-line @typescript-eslint/no-explicit-any - const configuration = JSON.parse((window as any)["interuss"] || "{}"); + const configuration = JSON.parse(document.getElementById("interuss_report_json")?.innerHTML || "{}"); console.log("Configuration:", configuration); - const { loading, report, nav } = useReport(configuration); + const { loading, error, report, nav } = useReport(configuration); if (loading) { return
Loading report...
; } - if (!report) { - return
Report not found
; + if (!report || error) { + return !error ?
Report not found
:
{error}
; } const router = createHashRouter(nav); return ; diff --git a/reports/src/capability/useReport.tsx b/reports/src/capability/useReport.tsx index e9119af..091303c 100644 --- a/reports/src/capability/useReport.tsx +++ b/reports/src/capability/useReport.tsx @@ -12,6 +12,7 @@ type UseReportProps = { type UseReportReturn = { loading: boolean; + error?: string; report?: ReportsReportTestRunReport; nav: RouteObject[]; }; @@ -20,7 +21,8 @@ export const useReport = ({ report: _report, }: UseReportProps): UseReportReturn => { const [loading, setLoading] = useState(true); - const [report, setReport] = useState(); + const [error, setError] = useState(); + const [report, setReport] = useState(_report); useEffect(() => { if (_report) { @@ -30,10 +32,20 @@ export const useReport = ({ } const fetchReport = async () => { - const res = await fetch(reportUrl); - const json = await res.json(); - setReport(json as ReportsReportTestRunReport); - setLoading(false); + try { + const res = await fetch(reportUrl); + if (res.status === 404) { + throw new Error("Report not found") + } + const json = await res.json(); + setReport(json as ReportsReportTestRunReport); + + } catch (err) { + console.error(err); + setError(JSON.stringify(err)); + } finally { + setLoading(false); + } }; fetchReport(); }, []); @@ -46,5 +58,5 @@ export const useReport = ({ () => (parsedReport ? getNavFromCapability(parsedReport.capability) : []), [parsedReport] ); - return { loading, report, nav }; + return {loading, error, report, nav}; };