forked from IDAES/idaes-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiagnostics.tsx
70 lines (63 loc) · 2.75 KB
/
Diagnostics.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { useContext, useEffect, useState } from "react";
import { AppContext } from "@/AppContext";
import axios from "axios";
import { messageBarTemplateGenerator } from "@/components/MessageBar/MessageBarTemplateGenerator";
// import { InitialDiagnostics } from "./InitialDiagnostics"; //????? here is imported but never use?
import DiagnosticHeader from "./DiagnosticsHeader";
import DiagnosticsDisplay from "./DiagnosticsDisplay";
import "./Diagnostics.css";
interface DiagnosticsDataInterface {
config: { key: any, value: any },
issues: { issues: any }
statistics: any
}
export default function Diagnostics() {
let { server_port, diagnosticsRefreshState } = useContext(AppContext);
// this use to hold all diagnostic data fetched from api end point pass down to sub components
const [diagnosticData, setDiagnosticsData] = useState<DiagnosticsDataInterface | null>(null);
// use to hold which issue currently is displayed on screen setWhichIssue to update diagnostics display
const [whichIssue, setWhichIssue] = useState<string | null>("structural");
const toggleIssueHandler = (issue: any) => {
// this function use in issues component's each issue tab
// use for when each issue tab click and toggle current displayed issue
setWhichIssue(issue)
}
useEffect(() => {
// const getDiagnosticUrl = `http://127.0.0.1:${server_port}/api/get_diagnostics`;
const windowURL = new URL(window.location.href);
const id = windowURL.searchParams.get("id");
const getDiagnosticUrl = `http://localhost:${server_port}/diagnostics?id=${id}`;
const fetchDiagnosticData = async (url: string) => {
// fetch diagnostic data from end point and update to state
try {
const res = await axios.get(url);
const data = res.data
messageBarTemplateGenerator("diagnosticRefresh", true);
setDiagnosticsData(data);
} catch (error) {
console.error("Fetch diagnostic data error", error)
messageBarTemplateGenerator("diagnosticRefresh", false);
}
}
fetchDiagnosticData(getDiagnosticUrl);
},
/**
* useEffect triggers:
* 1. onload
* 2. on diagnosticsRefreshState changed (click handler in mosaic to toggle this bool state)
*/
[diagnosticsRefreshState]);
return (
<>
<DiagnosticHeader
diagnosticData={diagnosticData}
toggleIssue={toggleIssueHandler}
whichIssue={whichIssue}
/>
<DiagnosticsDisplay
diagnosticData={diagnosticData}
whichIssue={whichIssue}
/>
</>
)
}