Skip to content

Commit

Permalink
refactor: decoupling code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrorezende committed Dec 27, 2024
1 parent 1e70cd7 commit 8e1384e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 41 deletions.
47 changes: 7 additions & 40 deletions apps/namadillo/src/App/Common/FixedWarningBanner.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,13 @@
import { indexerHeartbeatAtom } from "atoms/settings";
import { useNamadaKeychain } from "hooks/useNamadaKeychain";
import { useAtomValue } from "jotai";
import { ReactNode, useEffect, useState } from "react";
import { ReactNode } from "react";
import { IoWarning } from "react-icons/io5";
import {
checkIndexerCompatibilityErrors,
checkKeychainCompatibilityError,
} from "utils/compatibility";

export const FixedWarningBanner = (): JSX.Element => {
const [errorMessage, setErrorMessage] = useState<ReactNode | undefined>();
const indexerHealth = useAtomValue(indexerHeartbeatAtom);
const keychain = useNamadaKeychain();

const verifyKeychainVersion = async (): Promise<void> => {
const namadaKeychain = await keychain.namadaKeychain.get();
if (namadaKeychain) {
const version = namadaKeychain.version();
const versionErrorMessage = checkKeychainCompatibilityError(version);
if (versionErrorMessage) {
setErrorMessage(versionErrorMessage);
}
}
};

const verifyIndexerVersion = async (): Promise<void> => {
const versionErrorMessage = checkIndexerCompatibilityErrors(
indexerHealth.data?.version || ""
);
if (versionErrorMessage) {
setErrorMessage(versionErrorMessage);
}
};

useEffect(() => {
verifyKeychainVersion();
}, [keychain]);

useEffect(() => {
indexerHealth.isSuccess && verifyIndexerVersion();
}, [indexerHealth]);
type FixedWarningBannerProps = {
errorMessage: ReactNode;
};

export const FixedWarningBanner = ({
errorMessage,
}: FixedWarningBannerProps): JSX.Element => {
if (!errorMessage) return <></>;

return (
Expand Down
4 changes: 3 additions & 1 deletion apps/namadillo/src/App/Layout/AppLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FixedWarningBanner } from "App/Common/FixedWarningBanner";
import { useCompatibilityErrors } from "hooks/useCompatibilityErrors";
import { ReactNode, useState } from "react";
import { twMerge } from "tailwind-merge";
import { AppHeader } from "./AppHeader";
Expand All @@ -11,6 +12,7 @@ export const AppLayout = ({
children: ReactNode;
}): JSX.Element => {
const [displayNavigation, setDisplayNavigation] = useState(false);
const compatibilityErrors = useCompatibilityErrors();

return (
<div className="custom-container pb-2">
Expand Down Expand Up @@ -43,7 +45,7 @@ export const AppLayout = ({
</aside>
<main className="min-h-full">{children}</main>
</div>
<FixedWarningBanner />
<FixedWarningBanner errorMessage={compatibilityErrors} />
</div>
);
};
47 changes: 47 additions & 0 deletions apps/namadillo/src/hooks/useCompatibilityErrors.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { indexerHeartbeatAtom } from "atoms/settings";
import { useAtomValue } from "jotai";
import { useEffect, useState } from "react";
import {
checkIndexerCompatibilityErrors,
checkKeychainCompatibilityError,
} from "utils/compatibility";
import { useNamadaKeychain } from "./useNamadaKeychain";

export const useCompatibilityErrors = (): React.ReactNode | undefined => {
const indexerHealth = useAtomValue(indexerHeartbeatAtom);
const keychain = useNamadaKeychain();
const [errorMessage, setErrorMessage] = useState<
React.ReactNode | undefined
>();

const verifyKeychainVersion = async (): Promise<void> => {
const namadaKeychain = await keychain.namadaKeychain.get();
if (namadaKeychain) {
const version = namadaKeychain.version();
const versionErrorMessage = checkKeychainCompatibilityError(version);
if (versionErrorMessage) {
setErrorMessage(versionErrorMessage);
}
}
};

const verifyIndexerVersion = async (): Promise<void> => {
const versionErrorMessage = checkIndexerCompatibilityErrors(
indexerHealth.data?.version || ""
);

if (versionErrorMessage) {
setErrorMessage(versionErrorMessage);
}
};

useEffect(() => {
verifyKeychainVersion();
}, [keychain]);

useEffect(() => {
indexerHealth.isSuccess && verifyIndexerVersion();
}, [indexerHealth]);

return errorMessage;
};

0 comments on commit 8e1384e

Please sign in to comment.