All files / src/components/pages Health.tsx

56% Statements 14/25
33.33% Branches 2/6
58.33% Functions 7/12
54.17% Lines 13/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                                              2x     1x   1x 1x     1x 1x       1x       1x         1x                         1x     1x 1x                                                                                                                                 1x                                  
import React, { useEffect, useState } from "react";
import logo from "../../assets/images/logos/logo.png";
import {
  Button,
  Table,
  Container,
  OverlayTrigger,
  Tooltip,
} from "react-bootstrap";
import {
  callBackendHealth,
  DataIntegrity,
  SystemHealthData,
} from "../../background/api/api";
import { audioOnOff, setAudioVolumeByID } from "../../background/methods/sound";
import { logout } from "../../background/api/auth";
import { getDurationAsString } from "../../background/methods/time";
import { hasKey } from "../../background/methods/ObjectKeysTS";
import { formatBytes } from "../../background/methods/bytes";
import { FFLoading } from "../../components/basicElements/Loading";
import traffic_light from "../../assets/images/icons/material.io/traffic_light.svg";
 
export default function Health() {
  const [systemHealthData, setSystemHealthData] = useState<
    SystemHealthData | null | "loading"
  >("loading");
  const errorMsg = "not reachable";
 
  useEffect(() => {
    updateVariables();
  }, []);
 
  useEffect(() => {
    const timer = setTimeout(() => {
      updateVariables();
    }, 60000);
    // Clear timeout if the component is unmounted
    return () => clearTimeout(timer);
  });
 
  function updateVariables(): void {
    Promise.all([callBackendHealth()])
      .then(([data]) => {
        setSystemHealthData(data);
      })
      .catch(() => {
        setSystemHealthData(null);
      });
  }
 
  function getPathOfDataIntegrity(dataIntegrity: string): string {
    if (hasKey(DataIntegrity, dataIntegrity)) {
      return DataIntegrity[dataIntegrity];
    }
    console.log("[HEALTH] Couldn't parse SystemHealth string to enum.");
    return errorMsg;
  }
 
  function HealthContainer(): JSX.Element {
    Iif (systemHealthData === null) {
      return <p>{errorMsg}</p>;
    }
    Eif (systemHealthData === "loading") {
      return <FFLoading />;
    } else {
      return (
        <Container>
          <div>
            <Table striped bordered hover id={"ff-heath-table"}>
              <thead>
                <tr>
                  <th>System Health</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td>Deployment Type</td>
                  <td>{systemHealthData.deployment}</td>
                </tr>
                <tr>
                  <td>Version</td>
                  <td>{systemHealthData.version}</td>
                </tr>
                <tr>
                  <td>Uptime</td>
                  <td>
                    {getDurationAsString(systemHealthData.uptimeInSeconds)}
                  </td>
                </tr>
                <tr>
                  <td>Usercount</td>
                  <td>{systemHealthData.userCount}</td>
                </tr>
                <tr>
                  <td>Used Storage</td>
                  <td>{formatBytes(systemHealthData.usedStorageInBytes)}</td>
                </tr>
                <tr>
                  <td>Data Integrity</td>
                  <td>
                    <OverlayTrigger
                      placement="right"
                      delay={{ show: 250, hide: 400 }}
                      overlay={
                        <Tooltip id="button-tooltip" className={"pl-3"}>
                          {systemHealthData.dataIntegrity}
                        </Tooltip>
                      }
                    >
                      <img
                        className={getPathOfDataIntegrity(
                          systemHealthData.dataIntegrity
                        )}
                        src={traffic_light}
                        alt={systemHealthData.dataIntegrity}
                      />
                    </OverlayTrigger>
                  </td>
                </tr>
              </tbody>
            </Table>
          </div>
          <Button onClick={() => logout()}>Logout</Button>
        </Container>
      );
    }
  }
 
  return (
    <Container>
      <h1>FileFighter</h1>
 
      <img
        src={logo}
        alt="Logo FileFighter"
        onClick={() => {
          setAudioVolumeByID("audio_viking", 0.5);
          audioOnOff("audio_viking");
        }}
      />
 
      <HealthContainer />
    </Container>
  );
}