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

[Issue-932]: Update showing staking account on the Staking detail screen #983

Merged
merged 1 commit into from
Aug 25, 2023
Merged
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
32 changes: 32 additions & 0 deletions src/hooks/screen/Staking/useGetAccountsByStaking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { StakingType } from '@subwallet/extension-base/background/KoniTypes';
import { useMemo } from 'react';
import { useSelector } from 'react-redux';
import { RootState } from 'stores/index';
import { findAccountByAddress } from 'utils/account';
import { AccountJson } from '@subwallet/extension-base/background/types';

export default function useGetAccountsByStaking(chain: string, stakingType: StakingType): AccountJson[] {
const stakingItems = useSelector((state: RootState) => state.staking.stakingMap);
const accounts = useSelector((state: RootState) => state.accountState.accounts);

return useMemo(() => {
const accountInfos: AccountJson[] = [];

stakingItems.forEach(stakingItem => {
if (stakingItem.chain === chain && stakingItem.type === stakingType) {
accountInfos.push({ address: stakingItem.address });
}
});

accountInfos.forEach(accountInfo => {
const accountJson = findAccountByAddress(accounts, accountInfo.address);

if (accountJson) {
accountInfo.name = accountJson.name;
accountInfo.type = accountJson.type;
}
});

return accountInfos;
}, [accounts, chain, stakingItems, stakingType]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import i18n from 'utils/i18n/i18n';
import { CustomToast } from 'components/design-system-ui/toast';
import { SWModalRefProps } from 'components/design-system-ui/modal/ModalBaseV2';
import StakingActionModal from 'screens/Home/Staking/StakingDetail/StakingActionModal';
import useGetAccountsByStaking from 'hooks/screen/Staking/useGetAccountsByStaking';

interface Props {
nominatorMetadata?: NominatorMetadata;
Expand Down Expand Up @@ -104,7 +105,7 @@ export const StakingDetailModal = ({
: i18n.header.poolDetails;
const theme = useSubWalletTheme().swThemes;
const [seeMore, setSeeMore] = useState<boolean>(false);
const { accounts, currentAccount } = useSelector((state: RootState) => state.accountState);
const { currentAccount } = useSelector((state: RootState) => state.accountState);
const toastRef = useRef<ToastContainer>(null);
const onClickFooterButton = usePreCheckReadOnly(toastRef, currentAccount?.address);
const chainInfo = useFetchChainInfo(staking.chain);
Expand All @@ -117,6 +118,10 @@ export const StakingDetailModal = ({
pooled: i18n.filterOptions.pooled,
};
const modalRef = useRef<SWModalRefProps>(null);
const stakingAccounts = useGetAccountsByStaking(
nominatorMetadata?.chain || '',
nominatorMetadata?.type || StakingType.NOMINATED,
);

const onCloseDetailModal = useCallback(() => modalRef?.current?.close(), []);

Expand Down Expand Up @@ -319,7 +324,7 @@ export const StakingDetailModal = ({
<MetaInfo.AccountGroup
label={i18n.inputLabel.account}
content={nominatorMetadata?.address === 'ALL' ? i18n.common.allAccounts : ''}
addresses={accounts.map(acc => acc.address)}
addresses={stakingAccounts.map(acc => acc.address)}
/>
) : (
<MetaInfo.Account
Expand Down
Loading