Skip to content

Commit

Permalink
feat: refresh on next-time open dashboard after tx completed.
Browse files Browse the repository at this point in the history
  • Loading branch information
richardo2016x committed Apr 29, 2024
1 parent 5787a0c commit 94a1dbf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3794,7 +3794,7 @@ autoLockService.onAutoLock = async () => {
method: EVENTS.LOCK_WALLET,
});
};
refreshBalanceService.onRefreshBalance = async (ctx) => {
refreshBalanceService.onRefreshBalance = (ctx) => {
const address =
ctx?.accountToRefresh || refreshBalanceService.accountToRefresh;
if (!address) return;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/hooks/useCurrentBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export default function useCurrentBalance(
if (!account) return false;

try {
return wallet.isAddressBalanceExpired(account);
return wallet.isAddressBalanceExpired(account.toLowerCase());
} catch (error) {
return false;
}
Expand Down
45 changes: 36 additions & 9 deletions src/ui/views/Dashboard/components/BalanceView/BalanceView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from './useHomeBalanceView';
import { BALANCE_LOADING_TIMES } from '@/constant/timeout';
import type { Account } from '@/background/service/preference';
import { IExtractFromPromise } from '@/ui/utils/type';

const BalanceView = ({
currentAccount,
Expand Down Expand Up @@ -134,6 +135,17 @@ const BalanceView = ({
latestCurveData,
]);

const getCacheExpired = useCallback(async () => {
const res = {
balanceExpired: await isCurrentBalanceExpired(),
curveExpired: await isCurveCollectionExpired(),
expired: false,
};
res.expired = res.balanceExpired || res.curveExpired;

return res;
}, [isCurrentBalanceExpired, isCurveCollectionExpired]);

const { isManualRefreshing, onRefresh } = useRefreshHomeBalanceView({
currentAddress: currentAccount?.address,
refreshFn: useCallback(
Expand All @@ -147,19 +159,33 @@ const BalanceView = ({
},
[refreshBalance, refreshCurve]
),
isExpired: useCallback(async () => {
return {
balanceExpired: await isCurrentBalanceExpired(),
curveExpired: await isCurveCollectionExpired(),
};
}, [isCurrentBalanceExpired, isCurveCollectionExpired]),
isExpired: getCacheExpired,
});

const refreshTimerlegacy = useRef<NodeJS.Timeout>();
// only execute once on component mounted or address changed
useEffect(() => {
if (!currentHomeBalanceCache?.balance) {
onRefresh({ balanceExpired: true, curveExpired: true, isManual: false });
}
(async () => {
let expirationInfo: IExtractFromPromise<
ReturnType<typeof getCacheExpired>
> | null = null;
if (!currentHomeBalanceCache?.balance) {
onRefresh({
balanceExpired: true,
curveExpired: true,
isManual: false,
});
} else if (
(expirationInfo = await getCacheExpired()) &&
expirationInfo.expired
) {
onRefresh({
balanceExpired: expirationInfo.balanceExpired,
curveExpired: expirationInfo.curveExpired,
isManual: false,
});
}
})();

const handler = async ({ address }) => {
if (
Expand Down Expand Up @@ -192,6 +218,7 @@ const BalanceView = ({
currentAccount?.address,
dispatch.transactions,
onRefresh,
getCacheExpired,
]);

const handleIsGnosisChange = useCallback(async () => {
Expand Down

0 comments on commit 94a1dbf

Please sign in to comment.