Skip to content

Commit

Permalink
feat: hide points if the config url is not present
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab committed Sep 25, 2024
1 parent 2c2c1a8 commit c9c52f3
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ where,
- `NEXT_PUBLIC_API_URL` specifies the back-end API to use for the staking
system queries
- `NEXT_PUBLIC_POINTS_API_URL` specifies the Points API to use for the points
system
system (Optional)
- `NEXT_PUBLIC_NETWORK` specifies the BTC network environment
- `NEXT_PUBLIC_DISPLAY_TESTING_MESSAGES` boolean value to indicate whether display
testing network related message. Default to true
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-staking",
"version": "0.3.0",
"version": "0.3.1",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
11 changes: 4 additions & 7 deletions src/app/components/Delegations/Delegation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { FaBitcoin } from "react-icons/fa";
import { IoIosWarning } from "react-icons/io";
import { Tooltip } from "react-tooltip";

import { useHealthCheck } from "@/app/hooks/useHealthCheck";
import { DelegationState, StakingTx } from "@/app/types/delegations";
import { GlobalParamsVersion } from "@/app/types/globalParams";
import { getNetworkConfig } from "@/config/network.config";
Expand Down Expand Up @@ -44,7 +43,6 @@ export const Delegation: React.FC<DelegationProps> = ({
}) => {
const { startTimestamp } = stakingTx;
const [currentTime, setCurrentTime] = useState(Date.now());
const { isApiNormal, isGeoBlocked } = useHealthCheck();

useEffect(() => {
const timerId = setInterval(() => {
Expand Down Expand Up @@ -164,11 +162,10 @@ export const Delegation: React.FC<DelegationProps> = ({
<Tooltip id={`tooltip-${stakingTxHash}`} className="tooltip-wrap" />
</div>
</div>
{isApiNormal && !isGeoBlocked && (
<div className="relative flex justify-end lg:justify-center order-5">
<DelegationPoints stakingTxHash={stakingTxHash} />
</div>
)}
<DelegationPoints
stakingTxHash={stakingTxHash}
className="relative flex justify-end lg:justify-center order-5"
/>
<div className="order-6">{generateActionButton()}</div>
</div>
</div>
Expand Down
8 changes: 4 additions & 4 deletions src/app/components/Delegations/Delegations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "@/app/types/delegations";
import { ErrorState } from "@/app/types/errors";
import { GlobalParamsVersion } from "@/app/types/globalParams";
import { shouldDisplayPoints } from "@/config";
import { signUnbondingTx } from "@/utils/delegations/signUnbondingTx";
import { signWithdrawalTx } from "@/utils/delegations/signWithdrawalTx";
import { getIntermediateDelegationsLocalStorageKey } from "@/utils/local_storage/getIntermediateDelegationsLocalStorageKey";
Expand Down Expand Up @@ -100,6 +101,8 @@ const DelegationsContent: React.FC<DelegationsProps> = ({
const { isApiNormal, isGeoBlocked } = useHealthCheck();
const [awaitingWalletResponse, setAwaitingWalletResponse] = useState(false);

const shouldShowPoints =
isApiNormal && !isGeoBlocked && shouldDisplayPoints();
// Local storage state for intermediate delegations (withdrawing, unbonding)
const intermediateDelegationsLocalStorageKey =
getIntermediateDelegationsLocalStorageKey(publicKeyNoCoord);
Expand Down Expand Up @@ -284,9 +287,7 @@ const DelegationsContent: React.FC<DelegationsProps> = ({
<p>Inception</p>
<p className="text-center">Transaction hash</p>
<p className="text-center">Status</p>
{isApiNormal && !isGeoBlocked && (
<p className="text-center">Points</p>
)}
{shouldShowPoints && <p className="text-center">Points</p>}
<p>Action</p>
</div>
<div
Expand All @@ -307,7 +308,6 @@ const DelegationsContent: React.FC<DelegationsProps> = ({
stakingValueSat,
stakingTx,
stakingTxHashHex,
finalityProviderPkHex,
state,
isOverflow,
} = delegation;
Expand Down
46 changes: 29 additions & 17 deletions src/app/components/Points/DelegationPoints.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,53 @@ import React from "react";
import { NumericFormat } from "react-number-format";

import { useDelegationsPoints } from "@/app/context/api/DelegationsPointsProvider";
import { useHealthCheck } from "@/app/hooks/useHealthCheck";
import { shouldDisplayPoints } from "@/config";

interface DelegationPointsProps {
stakingTxHash: string;
className?: string;
}

export const DelegationPoints: React.FC<DelegationPointsProps> = ({
stakingTxHash,
className,
}) => {
const { isApiNormal, isGeoBlocked } = useHealthCheck();
const { delegationPoints, isLoading } = useDelegationsPoints();
// Early return if the API is not normal or the user is geo-blocked
if (!isApiNormal || isGeoBlocked || !shouldDisplayPoints()) {
return null;
}

const points = delegationPoints.get(stakingTxHash);

if (isLoading) {
return (
<div className="flex items-center justify-end gap-1">
<div className="h-5 w-12 animate-pulse rounded bg-gray-300 dark:bg-gray-700"></div>
<div className={className}>
<div className="flex items-center justify-end gap-1">
<div className="h-5 w-12 animate-pulse rounded bg-gray-300 dark:bg-gray-700"></div>
</div>
</div>
);
}

return (
<div className="flex items-center justify-end gap-1">
<p className="whitespace-nowrap">
<span className="lg:hidden">Points: </span>
{points !== undefined ? (
<NumericFormat
value={points.toFixed(3)}
displayType="text"
thousandSeparator=","
decimalSeparator="."
/>
) : (
"n.a."
)}
</p>
<div className={className}>
<div className="flex items-center justify-end gap-1">
<p className="whitespace-nowrap">
<span className="lg:hidden">Points: </span>
{points !== undefined ? (
<NumericFormat
value={points.toFixed(3)}
displayType="text"
thousandSeparator=","
decimalSeparator="."
/>
) : (
"n.a."
)}
</p>
</div>
</div>
);
};
3 changes: 2 additions & 1 deletion src/app/components/Summary/Summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Tooltip } from "react-tooltip";
import { useGlobalParams } from "@/app/context/api/GlobalParamsProvider";
import { useBtcHeight } from "@/app/context/mempool/BtcHeightProvider";
import { useHealthCheck } from "@/app/hooks/useHealthCheck";
import { shouldDisplayPoints } from "@/config";
import { getNetworkConfig } from "@/config/network.config";
import { satoshiToBtc } from "@/utils/btcConversions";
import {
Expand Down Expand Up @@ -79,7 +80,7 @@ export const Summary: React.FC<SummaryProps> = ({
</p>
</div>
</div>
{isApiNormal && !isGeoBlocked && (
{isApiNormal && !isGeoBlocked && shouldDisplayPoints() && (
<>
<div className="divider xl:divider-horizontal xl:mx-4 my-0" />
<div className="flex flex-1 gap-2 text-sm flex-col xl:flex-row xl:items-center justify-start xl:justify-between">
Expand Down
4 changes: 3 additions & 1 deletion src/app/context/api/DelegationsPointsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { createContext, useContext, useEffect, useState } from "react";
import { getDelegationPointsByStakingTxHashHexes } from "@/app/api/getPoints";
import { useHealthCheck } from "@/app/hooks/useHealthCheck";
import { Delegation } from "@/app/types/delegations";
import { shouldDisplayPoints } from "@/config";

interface PointsContextType {
delegationPoints: Map<string, number>;
Expand Down Expand Up @@ -78,7 +79,8 @@ export const DelegationsPointsProvider: React.FC<
isWalletConnected &&
delegationsAPI.length > 0 &&
isApiNormal &&
!isGeoBlocked,
!isGeoBlocked &&
shouldDisplayPoints(),
refetchInterval: 300000, // Refetch every 5 minutes
refetchOnWindowFocus: false,
retry: 1,
Expand Down
6 changes: 6 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ export const getNetworkAppUrl = (): string => {
? "https://btcstaking.testnet.babylonchain.io"
: "https://btcstaking.babylonlabs.io";
};

// shouldDisplayPoints function is used to check if the application should
// display points or not based on the existence of the environment variable.
export const shouldDisplayPoints = (): boolean => {
return !!process.env.NEXT_PUBLIC_POINTS_API_URL;
};

0 comments on commit c9c52f3

Please sign in to comment.