Skip to content

Commit

Permalink
Refactor testnet status update (#1748)
Browse files Browse the repository at this point in the history
* update testnet status display in the health indicator
  • Loading branch information
Jennievon authored Jan 17, 2024
1 parent 3ba9d7c commit 7f0ad72
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
4 changes: 2 additions & 2 deletions tools/tenscan/frontend/api/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { pathToUrl } from "@/src/routes/router";
import { apiRoutes } from "@/src/routes";

export const fetchTestnetStatus = async (): Promise<
ResponseDataInterface<any>
ResponseDataInterface<boolean>
> => {
return await httpRequest<ResponseDataInterface<any>>({
return await httpRequest<ResponseDataInterface<boolean>>({
method: "post",
url: pathToUrl(apiRoutes.getHealthStatus),
data: { jsonrpc: "2.0", method: "obscuro_health", params: [], id: 1 },
Expand Down
20 changes: 16 additions & 4 deletions tools/tenscan/frontend/src/components/health-indicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@ import { Badge, badgeVariants } from "./ui/badge";
import { useGeneralService } from "../services/useGeneralService";

const HealthIndicator = () => {
const [status, setStatus] = React.useState<boolean>(false);
const { testnetStatus } = useGeneralService();

// if testnetStatus.result is true, status is set to true
// if testnetStatus.result is false but testnetStatus.error includes [p2p], status is set to true

React.useEffect(() => {
if (testnetStatus?.result) {
setStatus(true);
} else if (testnetStatus?.errors?.includes("[p2p]")) {
setStatus(true);
} else {
setStatus(false);
}
}, [testnetStatus]);

return (
<div className="flex items-center space-x-2 border rounded-lg p-2">
<h3 className="text-sm">Testnet Status: </h3>
<Badge
variant={
(testnetStatus?.result
? "success"
: "destructive") as keyof typeof badgeVariants
(status ? "success" : "destructive") as keyof typeof badgeVariants
}
>
{testnetStatus?.result ? "Live" : "Down"}
{status ? "Live" : "Down"}
</Badge>
</div>
);
Expand Down
12 changes: 3 additions & 9 deletions tools/tenscan/frontend/src/services/useGeneralService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@ import { useQuery } from "@tanstack/react-query";
import { useState } from "react";

export const useGeneralService = () => {
const [noPolling, setNoPolling] = useState(false);

const {
data: testnetStatus,
isLoading: isStatusLoading,
refetch: refetchTestnetStatus,
} = useQuery({
const { data: testnetStatus, isLoading: isStatusLoading } = useQuery({
queryKey: ["testnetStatus"],
queryFn: () => fetchTestnetStatus(),
// refetchInterval: noPolling ? false : pollingInterval,
refetchInterval: 10000,
});

return { testnetStatus, isStatusLoading, setNoPolling, refetchTestnetStatus };
return { testnetStatus, isStatusLoading };
};
1 change: 1 addition & 0 deletions tools/tenscan/frontend/src/types/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface PaginationInterface {

export interface ResponseDataInterface<T> {
result: T;
errors?: string[] | string;
item: T;
message: string;
pagination?: PaginationInterface;
Expand Down
21 changes: 15 additions & 6 deletions tools/walletextension/frontend/src/components/health-indicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const HealthIndicator = () => {
setLoading(true);
try {
const status = await getTestnetStatus();

return status;
} catch (error) {
console.error(error);
Expand All @@ -20,18 +21,26 @@ const HealthIndicator = () => {
}
};

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

useEffect(() => {
let isMounted = true;

// if overall health is true, status is set to true
// if overall health is false but if the error includes [p2p], status is set to true

testnetStatus().then((res) => {
if (isMounted.current) {
setStatus(res?.result);
if (isMounted) {
if (res?.result?.OverallHealth) {
setStatus(true);
} else if (res?.result?.Errors?.includes("[p2p]")) {
setStatus(true);
} else {
setStatus(false);
}
}
});

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

0 comments on commit 7f0ad72

Please sign in to comment.