vega-frontend-monorepo/apps/token/src/routes/staking/staking-nodes-container.tsx
Dexter Edwards 56d9a70b97
Fix/1685 connet wrong on associate (#1718)
* fix: incorrect connection logic on the associate page

* test: add tests for staking wallet container

* chore: move files to be consistent with other routes structures

* chore: rename to be consistent with other route strcutures

* style: lint

* test: allow seeing of node information when not connected to wallets

* test: add test for disassociate page
2022-10-13 15:10:27 +01:00

132 lines
2.8 KiB
TypeScript

import { gql, useQuery } from '@apollo/client';
import { Callout, Intent, Splash } from '@vegaprotocol/ui-toolkit';
import { useVegaWallet } from '@vegaprotocol/wallet';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { SplashLoader } from '../../components/splash-loader';
import type { Staking as StakingQueryResult } from './__generated__/Staking';
export const STAKING_QUERY = gql`
query Staking($partyId: ID!) {
party(id: $partyId) {
id
stake {
currentStakeAvailable
currentStakeAvailableFormatted @client
}
delegations {
amount
amountFormatted @client
epoch
node {
id
}
}
}
epoch {
id
timestamps {
start
end
expiry
}
}
nodes {
id
name
pubkey
infoUrl
location
ethereumAddress
stakedByOperator
stakedByDelegates
stakedTotal
pendingStake
stakedByOperatorFormatted @client
stakedByDelegatesFormatted @client
stakedTotalFormatted @client
pendingStakeFormatted @client
epochData {
total
offline
online
}
status
rankingScore {
rankingScore
stakeScore
performanceScore
votingPower
stakeScore
}
}
nodeData {
stakedTotal
stakedTotalFormatted @client
totalNodes
inactiveNodes
validatingNodes
uptime
}
}
`;
const RPC_ERROR = 'rpc error: code = NotFound desc = NotFound error';
export const StakingNodesContainer = ({
children,
}: {
children: ({ data }: { data?: StakingQueryResult }) => React.ReactElement;
}) => {
const { t } = useTranslation();
const { pubKey } = useVegaWallet();
const { data, loading, error, refetch } = useQuery<StakingQueryResult>(
STAKING_QUERY,
{
variables: { partyId: pubKey || '' },
}
);
React.useEffect(() => {
const interval = setInterval(() => {
if (!data?.epoch.timestamps.expiry) return;
const now = Date.now();
const expiry = new Date(data.epoch.timestamps.expiry).getTime();
if (now > expiry) {
refetch();
clearInterval(interval);
}
}, 1000);
return () => {
clearInterval(interval);
};
}, [data?.epoch.timestamps.expiry, refetch]);
if (error) {
return (
<Callout intent={Intent.Danger} title={t('Something went wrong')}>
<pre>
{error.message.includes(RPC_ERROR)
? t('resourceNotFound')
: error.message}
</pre>
</Callout>
);
}
if (loading) {
return (
<Splash>
<SplashLoader />
</Splash>
);
}
return children({ data });
};
export default StakingNodesContainer;