Skip to content

Commit

Permalink
Merge pull request #41 from InvArch/yaki-page-rerender
Browse files Browse the repository at this point in the history
hide overview dao cards if stake is 0
  • Loading branch information
shibatales authored Jan 10, 2024
2 parents 8cfcc8a + 0e63b9c commit 4ee3716
Showing 1 changed file with 25 additions and 64 deletions.
89 changes: 25 additions & 64 deletions src/components/DaoList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import useApi from '../hooks/useApi';
import { toast } from 'react-hot-toast';
import useAccount from '../stores/account';
import { useQuery } from 'urql';
import { UnsubscribePromise } from '@polkadot/api/types';
import { StakesInfo } from '../routes/claim';
import useModal, { modalName } from '../stores/modals';

Expand All @@ -34,11 +33,11 @@ const DaoList = (props: DaoListProps) => {
const [totalUserStakedData, setTotalUserStakedData] = useState<TotalUserStakedData>({});
const [userStakedInfo, setUserStakedInfo] = useState<UserStakedInfoType[]
>([]);
const [currentStakingEra, setCurrentStakingEra] = useState<number>(0);

const [rewardsCoreClaimedQuery, reexecuteQuery] = useQuery({
query: TotalRewardsCoreClaimedQuery,
variables: {}
variables: {},
pause: !selectedAccount || isLoading || !isDataLoaded,
});

const toggleViewMembers = (core: StakingCore, members: AnyJson[]) => {
Expand Down Expand Up @@ -87,8 +86,6 @@ const DaoList = (props: DaoListProps) => {
}, [api]);

const loadTotalUserStaked = useCallback(() => {
if (!selectedAccount) return;

const coreInfoResults: { [key: number]: Partial<CoreEraStakeInfoType> | undefined; } = {};
const totalUserStakedResults: TotalUserStakedData = {};

Expand All @@ -101,7 +98,7 @@ const DaoList = (props: DaoListProps) => {
}

setTotalUserStakedData(totalUserStakedResults);
}, [selectedAccount, stakingCores, coreEraStakeInfo, userStakedInfo]);
}, [stakingCores, coreEraStakeInfo, userStakedInfo]);

const loadAccountInfo = useCallback(async () => {
if (!selectedAccount) return;
Expand All @@ -123,12 +120,10 @@ const DaoList = (props: DaoListProps) => {
const initializeData = useCallback(async (selectedAccount: InjectedAccountWithMeta | null) => {
try {
if (selectedAccount) {
await Promise.all([
loadAccountInfo(),
loadCores(),
loadStakingConstants(),
loadTotalUserStaked()
]);
await loadAccountInfo();
await loadCores();
await loadStakingConstants();
loadTotalUserStaked();
}

} catch (error) {
Expand All @@ -139,35 +134,10 @@ const DaoList = (props: DaoListProps) => {
}
}, [loadAccountInfo, loadCores, loadStakingConstants, loadTotalUserStaked]);

const setupSubscriptions = useCallback(async ({
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 coreEraStakeInfoMap: Map<
number, CoreEraStakeInfoType> = new Map();
Expand All @@ -178,7 +148,7 @@ const DaoList = (props: DaoListProps) => {

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 @@ -213,21 +183,19 @@ const DaoList = (props: DaoListProps) => {
);
}
}

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

useEffect(() => {
initializeData(selectedAccount);
}, [initializeData, selectedAccount]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedAccount]);

useEffect(() => {
const loadDaos = async () => {
if (!selectedAccount) return;
const daos = await loadStakedDaos(stakingCores, selectedAccount.address, api);
setStakedDaos(daos);
};

if (!selectedAccount) return;
if (!stakingCores) return;

Expand All @@ -236,7 +204,7 @@ const DaoList = (props: DaoListProps) => {

useEffect(() => {
loadTotalUserStaked();
}, [loadTotalUserStaked]);
}, [stakingCores, loadTotalUserStaked]);

useEffect(() => {
if (selectedAccount) {
Expand All @@ -257,26 +225,14 @@ const DaoList = (props: DaoListProps) => {
}, [selectedAccount, stakingCores, rewardsCoreClaimedQuery.data]);

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

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

const stakedCoresCount = useMemo(() => {
return isOverview
Expand All @@ -302,6 +258,11 @@ const DaoList = (props: DaoListProps) => {
const coreInfo = coreEraStakeInfo.find((info) => info.coreId === core.key);
const userStaked = totalUserStakedData[core.key] ? totalUserStakedData[core.key] : new BigNumber(0);

// If userStaked is zero, don't render the card
if (isOverview && userStaked && userStaked.isEqualTo(0)) {
return null;
}

const projectCard = (minified: boolean) => (
<ProjectCard
mini={minified}
Expand Down

1 comment on commit 4ee3716

@vercel
Copy link

@vercel vercel bot commented on 4ee3716 Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.