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

Yaki-sub-rerender-fix #37

Merged
merged 5 commits into from
Jan 9, 2024
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
74 changes: 19 additions & 55 deletions src/routes/overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import useApi from "../hooks/useApi";
import useAccount from "../stores/account";
import { useQuery } from "urql";
import { AnyJson, Codec } from "@polkadot/types/types";
import { UnsubscribePromise } from "@polkadot/api/types";
import { StakesInfo, VestingData, VestingSchedule } from "./claim";
import MetricDashboard from "../components/MetricDashboard";
import { loadProjectCores } from '../utils/stakingServices';
Expand Down Expand Up @@ -56,43 +55,18 @@ const Overview = () => {
pause: !selectedAccount,
});

const setupSubscriptions = useCallback(({
selectedAccount,
}: {
selectedAccount: InjectedAccountWithMeta;
}) => {
// Current block subscription
const blocks = api.rpc.chain.subscribeNewHeads(() => { });

// Next era starting block subscription
const nextEraStartingBlock = api.query.ocifStaking.nextEraStartingBlock(() => { });

let generalEraInfo;

if (currentStakingEra > 0) {
generalEraInfo = api.query.ocifStaking.generalEraInfo(currentStakingEra);
}

// Staking current era subscription
const currentEra = api.query.ocifStaking.currentEra((era: Codec) => {
setCurrentStakingEra(era.toPrimitive() as number);
});

const account = api.query.system.account(selectedAccount.address);

const unsubs = [blocks, nextEraStartingBlock, currentEra, account];

if (generalEraInfo) {
unsubs.push(generalEraInfo);
}
const setupSubscriptions = useCallback(async () => {
if (!selectedAccount) {
throw new Error("selectedAccount is null");
};

const userStakedInfoMap: Map<
number, UserStakedInfoType
> = new Map();

if (coreEraStakeInfo && coreEraStakeInfo.length > 0) {
for (const stakingCore of stakingCores) {
api.query.ocifStaking.generalStakerInfo(
await api.query.ocifStaking.generalStakerInfo(
stakingCore.key,
selectedAccount.address,
(generalStakerInfo: Codec) => {
Expand Down Expand Up @@ -147,15 +121,12 @@ const Overview = () => {
const newTotalStaked = Array.from(
userStakedInfoMap.values()
).reduce((acc, cur) => acc.plus(cur.staked), new BigNumber(0));

setTotalUserStaked(newTotalStaked);
}
);
}
}

return Promise.resolve(unsubs as UnsubscribePromise[]);
}, [api, currentStakingEra, coreEraStakeInfo, stakingCores, unclaimedEras]);
}, [api, currentStakingEra, stakingCores, unclaimedEras, selectedAccount, coreEraStakeInfo]);

const loadAggregateStaked = useCallback(async () => {
const totalIssuance = (await api.query.balances.totalIssuance()).toPrimitive() as string;
Expand All @@ -181,6 +152,11 @@ const Overview = () => {
setLockedBalance(new BigNumber(balance.data.frozen));
}, [api]);

const loadCurrentEra = useCallback(async () => {
const currentStakingEra = (await api.query.ocifStaking.currentEra()).toPrimitive() as number;
setCurrentStakingEra(currentStakingEra);
}, [api]);

const loadVestingBalance = useCallback(async (selectedAccount: InjectedAccountWithMeta | null) => {
if (!selectedAccount) return;
try {
Expand All @@ -198,13 +174,15 @@ const Overview = () => {
}
}, [api]);


const initializeData = useCallback(async (selectedAccount: InjectedAccountWithMeta | null) => {
try {
toast.loading("Loading staking cores...");

if (selectedAccount) {
await Promise.all([
loadAccountInfo(selectedAccount),
loadCurrentEra(),
loadCores(),
loadAggregateStaked(),
loadVestingBalance(selectedAccount)
Expand All @@ -219,7 +197,7 @@ const Overview = () => {
setLoading(false);
setDataLoaded(true);
}
}, [loadAccountInfo, loadCores, loadAggregateStaked, loadVestingBalance]);
}, [loadAccountInfo, loadCores, loadAggregateStaked, loadVestingBalance, loadCurrentEra]);

useEffect(() => {
initializeData(selectedAccount);
Expand Down Expand Up @@ -258,27 +236,15 @@ const Overview = () => {
}, [selectedAccount, stakingCores, rewardsCoreClaimedQuery.data, rewardsCoreClaimedQuery.fetching]);

useEffect(() => {
let unsubs: UnsubscribePromise[] = [];
const setup = async () => {
if (selectedAccount) {
unsubs = await setupSubscriptions({ selectedAccount });
if (selectedAccount && typeof setupSubscriptions === 'function') {
console.log('calling setupSubscriptions');
await setupSubscriptions();
}
};
setup();

return () => {
unsubs.forEach((unsub: UnsubscribePromise) => {
if (unsub) {
unsub.then(unsubFunc => {
if (typeof unsubFunc === 'function') {
unsubFunc();
}
});
}
});
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedAccount, api]);
}, [selectedAccount, stakingCores, coreEraStakeInfo]);

return (
<div className="mx-auto w-full flex max-w-7xl flex-col justify-between p-4 sm:px-6 lg:px-8 mt-14 md:mt-0 gap-3">
Expand All @@ -288,9 +254,7 @@ const Overview = () => {
<span>{isLoading || !isDataLoaded ? <LoadingSpinner /> : null}</span>
</h2>
</div>
{selectedAccount &&
currentStakingEra &&
unclaimedEras ? (
{selectedAccount ? (
<div>
<MetricDashboard
isOverview={true}
Expand Down
Loading