From 8ede977fcfddd9cee3a87537112cda2e5250b594 Mon Sep 17 00:00:00 2001 From: Yaroslav Grishajev Date: Thu, 19 Sep 2024 15:37:22 +0200 Subject: [PATCH] fix(wallet): refetch using query directly to avoid circular dep --- .../src/context/WalletProvider/WalletProvider.tsx | 4 ++-- apps/deploy-web/src/queries/queryKeys.ts | 2 +- apps/deploy-web/src/queries/useBalancesQuery.ts | 9 ++++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/apps/deploy-web/src/context/WalletProvider/WalletProvider.tsx b/apps/deploy-web/src/context/WalletProvider/WalletProvider.tsx index b1dfcbb79..52f710959 100644 --- a/apps/deploy-web/src/context/WalletProvider/WalletProvider.tsx +++ b/apps/deploy-web/src/context/WalletProvider/WalletProvider.tsx @@ -16,8 +16,8 @@ import { browserEnvConfig } from "@src/config/browser-env.config"; import { useAllowance } from "@src/hooks/useAllowance"; import { useManagedWallet } from "@src/hooks/useManagedWallet"; import { useUser } from "@src/hooks/useUser"; -import { useWalletBalance } from "@src/hooks/useWalletBalance"; import { useWhen } from "@src/hooks/useWhen"; +import { useBalances } from "@src/queries/useBalancesQuery"; import { txHttpService } from "@src/services/http/http.service"; import networkStore from "@src/store/networkStore"; import { AnalyticsEvents } from "@src/utils/analytics"; @@ -72,7 +72,6 @@ export const WalletProvider = ({ children }) => { const { enqueueSnackbar, closeSnackbar } = useSnackbar(); const router = useRouter(); const { settings } = useSettings(); - const { refetch: refetchBalances } = useWalletBalance(); const user = useUser(); const userWallet = useSelectedChain(); const { wallet: managedWallet, isLoading, create: createManagedWallet } = useManagedWallet(); @@ -84,6 +83,7 @@ export const WalletProvider = ({ children }) => { username, isWalletConnected } = useMemo(() => (selectedWalletType === "managed" && managedWallet) || userWallet, [managedWallet, userWallet, selectedWalletType]); + const { refetch: refetchBalances } = useBalances(walletAddress); const { addEndpoints } = useManager(); const isManaged = useMemo(() => !!managedWallet && managedWallet?.address === walletAddress, [walletAddress, managedWallet]); diff --git a/apps/deploy-web/src/queries/queryKeys.ts b/apps/deploy-web/src/queries/queryKeys.ts index 942c10432..1fc839b47 100644 --- a/apps/deploy-web/src/queries/queryKeys.ts +++ b/apps/deploy-web/src/queries/queryKeys.ts @@ -40,7 +40,7 @@ export class QueryKeys { static getProviderActiveLeasesGraph = (providerAddress: string) => ["PROVIDER_ACTIVE_LEASES_GRAPH", providerAddress]; static getAuditorsKey = () => ["AUDITORS"]; static getBlockKey = (id: string) => ["BLOCK", id]; - static getBalancesKey = (address: string) => ["BALANCES", address]; + static getBalancesKey = (address?: string) => (address ? ["BALANCES", address] : []); static getTemplatesKey = () => ["TEMPLATES"]; static getProviderAttributesSchema = () => ["PROVIDER_ATTRIBUTES_SCHEMA"]; static getDepositParamsKey = () => ["DEPOSIT_PARAMS"]; diff --git a/apps/deploy-web/src/queries/useBalancesQuery.ts b/apps/deploy-web/src/queries/useBalancesQuery.ts index e014af4f8..749fdf076 100644 --- a/apps/deploy-web/src/queries/useBalancesQuery.ts +++ b/apps/deploy-web/src/queries/useBalancesQuery.ts @@ -12,7 +12,7 @@ import { useSettings } from "../context/SettingsProvider"; import { QueryKeys } from "./queryKeys"; // Account balances -async function getBalances(apiEndpoint: string, address: string): Promise { +async function getBalances(apiEndpoint: string, address?: string): Promise { if (!address || !apiEndpoint) return undefined; const usdcIbcDenom = getUsdcDenom(); @@ -61,7 +61,10 @@ async function getBalances(apiEndpoint: string, address: string): Promise, "queryKey" | "queryFn">) { +export function useBalances(address?: string, options?: Omit, "queryKey" | "queryFn">) { const { settings } = useSettings(); - return useQuery(QueryKeys.getBalancesKey(address), () => getBalances(settings.apiEndpoint, address), options); + return useQuery(QueryKeys.getBalancesKey(address), () => getBalances(settings.apiEndpoint, address), { + enabled: !!address, + ...options + }); }