Skip to content

Commit

Permalink
[Gateway] health check on gateway UI/frontend (#1729)
Browse files Browse the repository at this point in the history
* feat: format health response

* Update getHealthStatus API route

* fix useEffect warning msgs
  • Loading branch information
Jennievon authored Jan 9, 2024
1 parent f6f147a commit 5aee83a
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tools/walletextension/frontend/src/api/general.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ResponseDataInterface } from "@/types/interfaces";
import { httpRequest } from ".";
import { pathToUrl } from "@/routes/router";
import { apiRoutes } from "@/routes";

export const fetchTestnetStatus = async (): Promise<
ResponseDataInterface<any>
> => {
return await httpRequest<ResponseDataInterface<any>>({
method: "post",
url: pathToUrl(apiRoutes.getHealthStatus),
data: { jsonrpc: "2.0", method: "obscuro_health", params: [], id: 1 },
});
};
57 changes: 57 additions & 0 deletions tools/walletextension/frontend/src/components/health-indicator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React, { useEffect } from "react";
import { Badge, badgeVariants } from "./ui/badge";
import useGatewayService from "@/services/useGatewayService";
import { Skeleton } from "./ui/skeleton";

const HealthIndicator = () => {
const { getTestnetStatus } = useGatewayService();
const [loading, setLoading] = React.useState(false);
const [status, setStatus] = React.useState<boolean>();

const testnetStatus = async () => {
setLoading(true);
try {
const status = await getTestnetStatus();
return status;
} catch (error) {
console.error(error);
} finally {
setLoading(false);
}
};

// using useRef to avoid memory leak
let isMounted = React.useRef(true);

useEffect(() => {
testnetStatus().then((res) => {
if (isMounted.current) {
setStatus(res?.result);
}
});

return () => {
isMounted.current = false;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return (
<div className="flex items-center space-x-2 border rounded-lg p-2">
<h3 className="text-sm">Testnet Status: </h3>
{loading ? (
<Skeleton className="h-4 w-10" />
) : (
<Badge
variant={
(status ? "success" : "destructive") as keyof typeof badgeVariants
}
>
{status ? "Live" : "Down"}
</Badge>
)}
</div>
);
};

export default HealthIndicator;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Link from "next/link";
import { HamburgerMenuIcon } from "@radix-ui/react-icons";
import { useState } from "react";
import { Button } from "../ui/button";
import HealthIndicator from "../health-indicator";

export default function Header() {
return (
Expand All @@ -16,6 +17,7 @@ export default function Header() {
<div className="hidden md:flex items-center space-x-4">
<MainNav className="mx-6" />
<div className="flex items-center space-x-4">
<HealthIndicator />
<ModeToggle />
<ConnectWalletButton />
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export const WalletConnectionProvider = ({
ethereum.removeListener("accountsChanged", fetchUserAccounts);
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const walletConnectionContextValue: WalletConnectionContextType = {
Expand Down
3 changes: 3 additions & 0 deletions tools/walletextension/frontend/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export const apiRoutes = {
queryAccountToken: `/${tenGatewayVersion}/query/`,
revoke: `/${tenGatewayVersion}/revoke/`,
version: `/version/`,

// **** INFO ****
getHealthStatus: `/${tenGatewayVersion}/network-health/`,
};

export const requestMethods = {
Expand Down
11 changes: 11 additions & 0 deletions tools/walletextension/frontend/src/services/useGatewayService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getToken,
switchToTenNetwork,
} from "@/api/ethRequests";
import { fetchTestnetStatus } from "@/api/general";

const useGatewayService = () => {
const { token, provider, fetchUserAccounts, setLoading } =
Expand Down Expand Up @@ -76,8 +77,18 @@ const useGatewayService = () => {
}
};

const getTestnetStatus = async () => {
try {
return await fetchTestnetStatus();
} catch (error) {
showToast(ToastType.DESTRUCTIVE, "Unable to connect to Obscuro Testnet");
throw error;
}
};

return {
connectToTenTestnet,
getTestnetStatus,
};
};

Expand Down

0 comments on commit 5aee83a

Please sign in to comment.