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: display the selected validator #8626

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions .changeset/pretty-hairs-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ledgerhq/coin-cardano": minor
"ledger-live-desktop": minor
---

Fix ADA Changing current delegation does not display the validator
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,9 @@ type Props = {
};

const ValidatorField = ({ account, delegation, onChangeValidator, selectedPoolId }: Props) => {
const [ledgerPools, setLedgerPools] = useState<Array<StakePool>>([]);
const [currentPool, setCurrentPool] = useState<Array<StakePool>>([]);
const unit = useAccountUnit(account);

const [showAll, setShowAll] = useState(
LEDGER_POOL_IDS.length === 0 ||
(LEDGER_POOL_IDS.length === 1 && delegation?.poolId === LEDGER_POOL_IDS[0]),
);

const [showAll, setShowAll] = useState(false);
const [ledgerPoolsLoading, setLedgerPoolsLoading] = useState(false);
const { pools, searchQuery, setSearchQuery, onScrollEndReached, isSearching, isPaginating } =
useCardanoFamilyPools(account.currency);
Expand All @@ -50,13 +45,14 @@ const ValidatorField = ({ account, delegation, onChangeValidator, selectedPoolId
useEffect(() => {
if (LEDGER_POOL_IDS.length) {
setLedgerPoolsLoading(true);
fetchPoolDetails(account.currency, LEDGER_POOL_IDS)
const delegationPoolId = delegation?.poolId ? [delegation.poolId] : [];
fetchPoolDetails(account.currency, delegationPoolId, LEDGER_POOL_IDS)
.then((apiRes: { pools: Array<StakePool> }) => {
setCurrentPool(apiRes.pools);
const filteredLedgerPools = apiRes.pools.filter(
pool => pool.poolId !== delegation?.poolId,
);
if (filteredLedgerPools.length) {
setLedgerPools(filteredLedgerPools);
onChangeValidator(filteredLedgerPools[0]);
}
})
Expand All @@ -66,6 +62,17 @@ const ValidatorField = ({ account, delegation, onChangeValidator, selectedPoolId
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
const selectedPool = pools.find(p => p.poolId === selectedPoolId);
if (selectedPool) {
setCurrentPool([selectedPool]);
onChangeValidator(selectedPool);
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedPoolId]);

const onSearch = useCallback(
(evt: React.ChangeEvent<HTMLInputElement>) => setSearchQuery(evt.target.value),
[setSearchQuery],
Expand All @@ -88,16 +95,26 @@ const ValidatorField = ({ account, delegation, onChangeValidator, selectedPoolId
{showAll && <ValidatorSearchInput noMargin={true} search={searchQuery} onSearch={onSearch} />}
<ValidatorsFieldContainer>
<Box p={1} data-testid="validator-list">
{(showAll && isSearching) || (!showAll && ledgerPoolsLoading) ? (
{(showAll && isSearching) ||
(!showAll && ledgerPoolsLoading) ||
(!showAll && !pools.length) ? (
<Box flex={1} py={3} alignItems="center" justifyContent="center">
<BigSpinner size={35} />
</Box>
) : (
<ScrollLoadingList
data={
showAll
? pools.filter(p => !poolIdsToFilterFromAllPools.includes(p.poolId))
: ledgerPools
? [
currentPool[0],
...pools.filter(
p =>
p &&
!poolIdsToFilterFromAllPools.includes(p.poolId) &&
p !== currentPool[0],
),
]
: currentPool
}
style={{
flex: showAll ? "1 0 256px" : "1 0 64px",
Expand Down
14 changes: 13 additions & 1 deletion libs/coin-modules/coin-cardano/src/api/getPools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@ export async function fetchPoolList(
export async function fetchPoolDetails(
currency: CryptoCurrency,
poolIds: Array<string>,
ledgerPoolIds?: Array<string>,
): Promise<APIGetPoolsDetail> {
const currentPoolIds = poolIds.length == 0 ? ledgerPoolIds : poolIds;
sprohaszka-ledger marked this conversation as resolved.
Show resolved Hide resolved
const res = await network({
method: "GET",
url: isTestnet(currency)
? `${CARDANO_TESTNET_API_ENDPOINT}/v1/pool/detail`
: `${CARDANO_API_ENDPOINT}/v1/pool/detail`,
params: { poolIds },
params: { poolIds: currentPoolIds },
});
if (!res || (res.data as APIGetPoolsDetail).pools.length === 0) {
const newRes = await network({
method: "GET",
url: isTestnet(currency)
? `${CARDANO_TESTNET_API_ENDPOINT}/v1/pool/detail`
: `${CARDANO_API_ENDPOINT}/v1/pool/detail`,
params: { poolIds: ledgerPoolIds },
});
return newRes && (newRes.data as APIGetPoolsDetail);
}
return res && (res.data as APIGetPoolsDetail);
}
Loading