Skip to content

Commit

Permalink
Fix: fetch pools/stakes in dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
mypeaceduck committed Jul 18, 2024
1 parent 6d4842e commit ef93aea
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/hooks/useDashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,18 @@ export function useDashboard(account: `0x${string}`): IDashboard | undefined {
const { data: totalPools = 0n } = useReadStaqeProtocolGetTotalPools();
const [totalPoolsProcessed, setTotalPoolsProcessed] = useState(0n);

const perPage = 1;
const perPage = 10;

const [page, setPage] = useState(1);
const total = useMemo(() => totalPools / BigInt(perPage), [totalPools]);
const { pools: getPools } = usePools(page, perPage, account);
const totalPages = useMemo(
() => (totalPools + BigInt(perPage) - 1n) / BigInt(perPage),
[totalPools]
);
const {
pools: getPools,
hasPrev,
hasNext,
} = usePools(page, perPage, account);
const pools = useMemo(
() =>
JSON.stringify(getPools, (_, v) =>
Expand Down Expand Up @@ -69,17 +76,23 @@ export function useDashboard(account: `0x${string}`): IDashboard | undefined {
}, []);

useEffect(() => {
if (page >= total) return;
console.log("Page:", page, "/", total);
if (BigInt(page) > totalPages) {
setCompleted(true);
return;
}

if (getPools && getPools.length === 0) {
setPage((prev) => prev + 1); // Move to the next page if current page has no pools
return;
}

const timer = setTimeout(() => setPage((page) => page + 1), 1000);
return () => clearTimeout(timer);
}, [page, total]);
}, [page, totalPages, getPools]);

useEffect(() => {
if (totalPools > 0n && totalPoolsProcessed < totalPools) {
setCompleted(false);
} else {
setCompleted(true);
}
}, [totalPools, totalPoolsProcessed]);

Expand All @@ -96,7 +109,7 @@ export function useDashboard(account: `0x${string}`): IDashboard | undefined {
});

setTotalPoolsProcessed((prev) => prev + BigInt(getPools.length));
}, [pools, account]);
}, [pools, hasPrev, hasNext, account]);

return {
completed,
Expand Down

0 comments on commit ef93aea

Please sign in to comment.