From 5e8cfb12af04376c3df12d38278f8bb3f71ee0c9 Mon Sep 17 00:00:00 2001 From: DMY <147dmy@gmail.com> Date: Tue, 3 Sep 2024 10:51:49 +0800 Subject: [PATCH] Refactor hooks to accept dependencies and improve state management --- src/ui/views/GasAccount/hooks/index.ts | 28 ++++++++++++-------------- src/ui/views/GasAccount/index.tsx | 2 +- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/ui/views/GasAccount/hooks/index.ts b/src/ui/views/GasAccount/hooks/index.ts index 5e3de775c16..ada0b96747f 100644 --- a/src/ui/views/GasAccount/hooks/index.ts +++ b/src/ui/views/GasAccount/hooks/index.ts @@ -43,7 +43,7 @@ export const useGasAccountInfo = () => { dispatch.gasAccount.setGasAccountSig({}); return undefined; }); - }, [sig, refreshId]); + }, [sig, accountId, refreshId]); if (error?.message?.includes('failed') && sig && accountId) { dispatch.gasAccount.setGasAccountSig({}); @@ -80,15 +80,17 @@ export const useGasAccountMethods = () => { return { login, logout }; }; -export const useGasAccountLogin = () => { +export const useGasAccountLogin = ({ + loading, + value, +}: ReturnType) => { const { sig, accountId } = useGasAccountSign(); - const { loading, value } = useGasAccountInfo(); const { login, logout } = useGasAccountMethods(); const isLogin = useMemo( () => (!loading ? !!value?.account?.id : !!sig && !!accountId), - [sig, accountId, loading, value] + [sig, accountId, loading, value?.account?.id] ); return { login, logout, isLogin }; @@ -104,7 +106,7 @@ export const useGasAccountHistory = () => { setRefreshListTx((e) => e + 1); }, []); - const { refresh } = useGasAccountRefresh(); + const { refresh: refreshGasAccountBalance } = useGasAccountRefresh(); type History = Awaited< ReturnType @@ -144,16 +146,16 @@ export const useGasAccountHistory = () => { reloadDeps: [sig], isNoMore(data) { if (data) { - return data?.list.length >= data?.totalCount; + return !(!!data?.rechargeList && data?.rechargeList?.length > 0); } return true; }, - manual: !sig, + manual: !sig || !accountId, } ); const { value } = useAsync(async () => { - if (sig && accountId) { + if (sig && accountId && refreshTxListCount) { return wallet.openapi.getGasAccountHistory({ sig, account_id: accountId, @@ -170,8 +172,8 @@ export const useGasAccountHistory = () => { return; } - if (d.rechargeList.length !== value?.recharge_list?.length) { - refresh(); + if (value?.recharge_list?.length !== d.rechargeList.length) { + refreshGasAccountBalance(); } return { rechargeList: value?.recharge_list, @@ -182,10 +184,6 @@ export const useGasAccountHistory = () => { ), }; }); - - if (!value?.recharge_list?.length) { - refresh(); - } } }, [mutate, value]); @@ -209,7 +207,7 @@ export const useGasAccountHistory = () => { clearTimeout(timer); } }; - }, [loading, loadingMore, refreshListTx, txList?.list]); + }, [loading, loadingMore, refreshListTx, txList]); return { loading, diff --git a/src/ui/views/GasAccount/index.tsx b/src/ui/views/GasAccount/index.tsx index 5fc1ff6e2ee..54003653cfa 100644 --- a/src/ui/views/GasAccount/index.tsx +++ b/src/ui/views/GasAccount/index.tsx @@ -40,7 +40,7 @@ const GasAccountInner = () => { setDepositVisible(true); }; const { value, loading } = useGasAccountInfo(); - const { isLogin } = useGasAccountLogin(); + const { isLogin } = useGasAccountLogin({ value, loading }); const wallet = useWallet();