Skip to content

Commit

Permalink
Automatically reload supply table after deposit/withdraw (#851)
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenshively authored Apr 21, 2024
1 parent 3ebe367 commit 5d197f9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
33 changes: 18 additions & 15 deletions earn/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Suspense, useEffect } from 'react';
import React, { Suspense, useCallback, useEffect, useMemo, useRef, useState } from 'react';

import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/react-hooks';
import * as Sentry from '@sentry/react';
Expand Down Expand Up @@ -144,7 +144,9 @@ function AppBodyWrapper() {
}

function App() {
const [activeChain, setActiveChain] = React.useState<Chain>(DEFAULT_CHAIN);
const mounted = useRef(true);

const [activeChain, setActiveChain] = useState<Chain>(DEFAULT_CHAIN);
const [accountRisk, setAccountRisk] = useSafeState<AccountRiskResult>({ isBlocked: false, isLoading: true });
const [geoFencingInfo, setGeoFencingInfo] = useSafeState<GeoFencingInfo>({
isAllowed: false,
Expand All @@ -155,7 +157,14 @@ function App() {
const { address: userAddress } = useAccount();
const provider = useProvider({ chainId: activeChain.id });

const value = { activeChain, setActiveChain };
const refetch = useCallback(async () => {
const chainId = (await provider.getNetwork()).chainId;
const res = await getAvailableLendingPairs(chainId, provider);
if (mounted.current) setLendingPairs(res);
}, [provider, setLendingPairs]);

const lendingPairsContextValue = useMemo(() => ({ lendingPairs, refetch }), [lendingPairs, refetch]);
const chainContextValue = { activeChain, setActiveChain };

useEffect(() => {
Sentry.setTag('chain_name', activeChain.name);
Expand Down Expand Up @@ -184,27 +193,21 @@ function App() {
}, [userAddress, setAccountRisk]);

useEffect(() => {
let mounted = true;

(async () => {
const chainId = (await provider.getNetwork()).chainId;
const res = await getAvailableLendingPairs(chainId, provider);
if (mounted) setLendingPairs(res);
})();

mounted.current = true;
refetch();
return () => {
mounted = false;
mounted.current = false;
};
}, [provider, setLendingPairs]);
}, [refetch]);

return (
<>
<Suspense fallback={null}>
<WagmiProvider>
<AccountRiskContext.Provider value={accountRisk}>
<GeoFencingContext.Provider value={geoFencingInfo}>
<ChainContext.Provider value={value}>
<LendingPairsContext.Provider value={lendingPairs}>
<ChainContext.Provider value={chainContextValue}>
<LendingPairsContext.Provider value={lendingPairsContextValue}>
<ScrollToTop />
<AppBodyWrapper />
</LendingPairsContext.Provider>
Expand Down
13 changes: 8 additions & 5 deletions earn/src/data/hooks/UseLendingPairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import { Address } from 'wagmi';

import { LendingPair } from '../LendingPair';

export const LendingPairsContext = createContext<LendingPair[] | null>(null);
export const LendingPairsContext = createContext<{ lendingPairs: LendingPair[] | null; refetch?: () => void }>({
lendingPairs: null,
});

export function useLendingPairs() {
const ctxt = useContext(LendingPairsContext);

return useMemo(
() => ({
isLoading: ctxt === null,
lendingPairs: ctxt ?? [],
isLoading: ctxt.lendingPairs === null,
lendingPairs: ctxt.lendingPairs ?? [],
refetch: ctxt.refetch,
}),
[ctxt]
);
Expand All @@ -23,8 +26,8 @@ export function useLendingPair(token0?: Address, token1?: Address) {

const { lendingPairs } = useMemo(
() => ({
isLoading: ctxt === null,
lendingPairs: ctxt ?? [],
isLoading: ctxt.lendingPairs === null,
lendingPairs: ctxt.lendingPairs ?? [],
}),
[ctxt]
);
Expand Down
7 changes: 5 additions & 2 deletions earn/src/pages/MarketsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default function MarketsPage() {
}, [searchParams]);

// MARK: custom hooks
const { lendingPairs } = useLendingPairs();
const { lendingPairs, refetch: refetchLendingPairs } = useLendingPairs();

// NOTE: Instead of `useAvailablePools()`, we're able to compute `availablePools` from `lendingPairs`.
// This saves a lot of data.
Expand Down Expand Up @@ -446,7 +446,10 @@ export default function MarketsPage() {
}}
onConfirm={() => {
setIsPendingTxnModalOpen(false);
setTimeout(() => refetch(), 100);
setTimeout(() => {
refetchLendingPairs?.();
refetch();
}, 100);
}}
status={pendingTxnModalStatus}
/>
Expand Down

0 comments on commit 5d197f9

Please sign in to comment.