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

Fix remove throw error in render method #203

Closed
Closed
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
28 changes: 24 additions & 4 deletions src/app/components/Delegations/Delegations.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { networks } from "bitcoinjs-lib";
import { useEffect, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import InfiniteScroll from "react-infinite-scroll-component";
import { useLocalStorage } from "usehooks-ts";

Expand Down Expand Up @@ -101,6 +101,12 @@ const DelegationsContent: React.FC<DelegationsProps> = ({
const { isApiNormal, isGeoBlocked } = useHealthCheck();
const [awaitingWalletResponse, setAwaitingWalletResponse] = useState(false);

const delegation = useMemo(
() =>
delegationsAPI.find((delegation) => delegation.stakingTxHashHex === txID),
[delegationsAPI, txID],
);

const shouldShowPoints =
isApiNormal && !isGeoBlocked && shouldDisplayPoints();
// Local storage state for intermediate delegations (withdrawing, unbonding)
Expand Down Expand Up @@ -267,6 +273,21 @@ const DelegationsContent: React.FC<DelegationsProps> = ({
});
}, [delegationsAPI, setIntermediateDelegationsLocalStorage]);

useEffect(() => {
if (modalOpen && !delegation) {
showError({
error: {
message: "Delegation not found",
errorState: ErrorState.SERVER_ERROR,
},
noCancel: false,
});
setModalOpen(false);
setTxID("");
setModalMode(undefined);
}
}, [modalOpen, delegation, showError]);

// combine delegations from the API and local storage, prioritizing API data
const combinedDelegationsData = delegationsAPI
? [...delegationsLocalStorage, ...delegationsAPI]
Expand Down Expand Up @@ -339,7 +360,7 @@ const DelegationsContent: React.FC<DelegationsProps> = ({
</div>
</>
)}
{modalMode && txID && (
{modalMode && txID && delegation && (
<UnbondWithdrawModal
open={modalOpen}
onClose={() => setModalOpen(false)}
Expand All @@ -350,8 +371,7 @@ const DelegationsContent: React.FC<DelegationsProps> = ({
}}
mode={modalMode}
awaitingWalletResponse={awaitingWalletResponse}
delegationsAPI={delegationsAPI}
txID={txID}
delegation={delegation}
/>
)}
</div>
Expand Down
15 changes: 3 additions & 12 deletions src/app/components/Modals/UnbondWithdrawModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ interface PreviewModalProps {
onProceed: () => void;
mode: MODE;
awaitingWalletResponse: boolean;
delegationsAPI: DelegationInterface[];
txID: string;
delegation: DelegationInterface;
}

export const UnbondWithdrawModal: React.FC<PreviewModalProps> = ({
Expand All @@ -32,8 +31,7 @@ export const UnbondWithdrawModal: React.FC<PreviewModalProps> = ({
onProceed,
mode,
awaitingWalletResponse,
delegationsAPI,
txID,
delegation,
}) => {
const { coinName, networkName } = getNetworkConfig();
const { data: allGlobalParamsVersions } = useGlobalParams();
Expand All @@ -46,15 +44,8 @@ export const UnbondWithdrawModal: React.FC<PreviewModalProps> = ({
return currentVersion;
};

const delegation = delegationsAPI.find(
(delegation) => delegation.stakingTxHashHex === txID,
);
if (!delegation) {
jeremy-babylonlabs marked this conversation as resolved.
Show resolved Hide resolved
throw new Error("Delegation not found");
}

const globalParams = getGlobalParamsForDelegation(
delegation.stakingTx.startHeight,
delegation.stakingTx.startHeight ?? 0,
);
const unbondingFeeSat = globalParams?.unbondingFeeSat || 0;
const unbondingTimeBlocks = globalParams?.unbondingTime || 0;
Expand Down