Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor testnet status update #1748

Merged
merged 4 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
Comment on lines +6 to +12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The refetchInterval is now set to a constant value of 10000, which standardizes the polling mechanism. Ensure that this interval is appropriate for the application's needs and does not lead to unnecessary network traffic or user interface updates.

};
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
Loading