Skip to content

Commit

Permalink
feat: chain status hook (#1058)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk authored Sep 4, 2024
1 parent 2e65ae8 commit c5e3c5b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
4 changes: 3 additions & 1 deletion apps/namadillo/src/App/Common/SyncIndicator.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Tooltip } from "@namada/components";
import { syncStatusAtom } from "atoms/syncStatus/atoms";
import { useChainStatus } from "hooks/useChainStatus";
import { useAtomValue } from "jotai";
import { twMerge } from "tailwind-merge";

export const SyncIndicator = (): JSX.Element => {
const syncStatus = useAtomValue(syncStatusAtom);
const { data } = useChainStatus();

return (
<div className="relative group/tooltip p-1">
Expand All @@ -21,7 +23,7 @@ export const SyncIndicator = (): JSX.Element => {
"Syncing"
: syncStatus.isError ?
"Error syncing"
: "Fully synced"}
: `Fully synced: height ${data?.height}, epoch ${data?.epoch}`}
</Tooltip>
</div>
);
Expand Down
18 changes: 18 additions & 0 deletions apps/namadillo/src/hooks/useChainStatus.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useSSE } from "@namada/hooks";
import { indexerUrlAtom } from "atoms/settings";
import { useAtomValue } from "jotai";

export type ChainStatus = {
height: number;
epoch: number;
};

export const useChainStatus = (): {
data?: ChainStatus;
error?: Event;
closeFn?: () => void;
} => {
const url = useAtomValue(indexerUrlAtom);

return useSSE<ChainStatus>(`${url}/api/v1/chain/status`);
};
1 change: 1 addition & 0 deletions packages/hooks/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./useDebounce";
export * from "./useEffectSkipFirstRender";
export * from "./useEvent";
export * from "./useSSE";
export * from "./useSanitizedLocation";
export * from "./useSanitizedParams";
export * from "./useUntil";
32 changes: 32 additions & 0 deletions packages/hooks/src/useSSE.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useEffect, useState } from "react";

export function useSSE<T>(
url: string,
options?: EventSourceInit
): { data?: T; error?: Event; closeFn?: () => void } {
const [data, setData] = useState<T>();
const [error, setError] = useState<Event>();
const [closeFn, setCloseFn] = useState<() => void>();

useEffect(() => {
const eventSource = new EventSource(url, options);
setCloseFn(() => eventSource.close.bind(eventSource));

eventSource.onmessage = (event) => {
setData((old) =>
event.data === JSON.stringify(old) ? old : JSON.parse(event.data)
);
};

eventSource.onerror = (err) => {
setError(err);
eventSource.close();
};

return () => {
eventSource.close();
};
}, [url]);

return { data, error, closeFn };
}

1 comment on commit c5e3c5b

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.