import React, { Component } from "react"; import * as ReactDom from "react-dom/client"; import { DAL } from "../DAL/systemInfo"; import {SystemInfo, CpuInfo, NetworkInfo, DriveInfo} from "../../src/models/systemInfo"; import menu from "./menu"; interface SystemInfoComponentState { loading: boolean; hidden: boolean; } interface SystemInfoProps { hostname: string; setHidden: any;//((val:boolean)=>void|null); } export class SystemInfoComponent extends Component { private data: SystemInfo|undefined = undefined; public constructor(props: SystemInfoProps) { super(props); this.state = { loading: true, hidden: this.isFiltered() }; DAL.SystemInfo.getInfo(this.props.hostname).then(data => { this.data = data; // FIXME error handling this.setState({...this.state, loading: false}); }); menu.addFilterEventListener(() => { this.refreshFilter(); }); } private isFiltered(): boolean { const filterShown = menu.getFilteredHostnames(); return (filterShown.indexOf(this.props.hostname) < 0); } private refreshFilter() { const hidden = this.isFiltered(); if (hidden === this.state.hidden) return; this.setState({...this.state, hidden: hidden}); } private layout(content: React.JSX.Element) { return

{this.props.hostname}

{content}
; } private statItem(key: string, value: string|React.JSX.Element, listKey?: number): React.JSX.Element { if (!value) return <>; key = key.charAt(0).toUpperCase()+String(key).slice(1); if (listKey === undefined) return
  • {key}: {value}
  • ; return
  • {key}: {value}
  • ; } private statItemNumber(key: string, value: number): React.JSX.Element { if (!value || isNaN(value)) return <>; return this.statItem(key, `${value}`); } private statItemMhz(key: string, value: number): React.JSX.Element { if (!value || isNaN(value)) return <>; return this.statItem(key, `${value}`); } private stringifyByte(value: number): string { if (!value || isNaN(value)) return ""; let units = ["Byte", "kB", "MB", "GB", "TB", "PB"]; let unitIndex = 0; while (value > 1024 && unitIndex < units.length) { value /= 1024; unitIndex++; } value = Math.round(value*100)/100; return `${value}${units[unitIndex]}`; } private statItemBytes(key: string, value: number): React.JSX.Element { if (!value || isNaN(value)) return <>; return this.statItem(key, this.stringifyByte(value)); } private statItemReadOnly(key: string, value: boolean|undefined): React.JSX.Element { if (value === undefined) return <>; return this.statItem(key, value ? "Read-Write" : "Read Only"); } private statItemBoolean(key: string, value: boolean|undefined): React.JSX.Element { if (value === undefined) return <>; return this.statItem(key, value ? "true" : "false"); } private statItemArray(key: string, values: string[]): React.JSX.Element { return this.statItem(key, ); } private statItemFragArray(key: string, values: React.JSX.Element[]): React.JSX.Element { return this.statItem(key, ); } private uptime(data: SystemInfo): string { let totalTime = data.uptime; let timeInSec = Math.floor(totalTime % 60); totalTime /= 60; let timeInMin = Math.floor(totalTime % 60); totalTime /= 60; let timeInHours = Math.floor(totalTime % 24); totalTime /= 24; let timeInDays = Math.floor(totalTime); let result = ""; if (timeInDays) result += `${timeInDays}d, `; if (result.length || timeInHours) result += (`${timeInHours}:`).padStart(3, "0"); result += (`${timeInMin}`).padStart(2, "0")+':'+(`${timeInSec}`).padStart(2, "0"); return result; } private cpuInfos(cpu: CpuInfo, cpuIndex: number): React.JSX.Element { return this.statItem("model", cpu.model, cpuIndex); } private networkInfo(network: NetworkInfo, netIndex: number): React.JSX.Element { return
  • ; } private driveInfo(drive: DriveInfo, driveIdx: number): React.JSX.Element { return
  • ; } public render(): React.JSX.Element { if (this.state.loading) return this.layout(<>Loading..); else if (this.data === undefined) return this.layout(<>Error); const data = this.data!; return this.layout(<> ); } public static async renderMultiple(container: HTMLElement): Promise { let domNodes = (await DAL.SystemInfo.listHosts()).map(hostname => ); ReactDom.createRoot(container).render(<>{domNodes}); return domNodes; } }