Skip to content

Commit

Permalink
feat(wallet): fix inital state when user has managed wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
baktun14 committed Dec 18, 2024
1 parent 7484fb6 commit 116f07e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
16 changes: 15 additions & 1 deletion apps/deploy-web/src/hooks/useManagedWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useMemo } from "react";
import { useAtom } from "jotai";

import { browserEnvConfig } from "@src/config/browser-env.config";
import { useSelectedChain } from "@src/context/CustomChainProvider";
import { useUser } from "@src/hooks/useUser";
import { useCreateManagedWalletMutation, useManagedWalletQuery } from "@src/queries/useManagedWalletQuery";
import walletStore from "@src/store/walletStore";
Expand All @@ -14,7 +15,20 @@ const isBillingEnabled = NEXT_PUBLIC_BILLING_ENABLED;
export const useManagedWallet = () => {
const user = useUser();
const { user: signedInUser } = useCustomUser();
const { data: queried, isFetched, isLoading: isFetching, refetch } = useManagedWalletQuery(isBillingEnabled ? user?.id : undefined);
const userWallet = useSelectedChain();
const [selectedWalletType, setSelectedWalletType] = useAtom(walletStore.selectedWalletType);
const {
data: queried,
isFetched,
isLoading: isFetching,
refetch
} = useManagedWalletQuery(isBillingEnabled ? user?.id : undefined, {
onSuccess: wallet => {
if (selectedWalletType === "custodial" && wallet && !userWallet.isWalletConnected) {
setSelectedWalletType("managed");
}
}
});
const { mutate: create, data: created, isLoading: isCreating, isSuccess: isCreated } = useCreateManagedWalletMutation();
const wallet = useMemo(() => queried || created, [queried, created]);
const isLoading = isFetching || isCreating;
Expand Down
8 changes: 5 additions & 3 deletions apps/deploy-web/src/queries/useManagedWalletQuery.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useMutation, useQuery, useQueryClient } from "react-query";
import { QueryKey, useMutation, useQuery, useQueryClient, UseQueryOptions } from "react-query";
import { ApiWalletOutput } from "@akashnetwork/http-sdk";

import { managedWalletHttpService } from "@src/services/managed-wallet-http/managed-wallet-http.service";

const MANAGED_WALLET = "MANAGED_WALLET";

export function useManagedWalletQuery(userId?: string) {
export function useManagedWalletQuery(userId?: string, options?: Omit<UseQueryOptions<ApiWalletOutput, Error, any, QueryKey>, "queryKey" | "queryFn">) {
return useQuery(
[MANAGED_WALLET, userId],
async () => {
Expand All @@ -14,7 +15,8 @@ export function useManagedWalletQuery(userId?: string) {
},
{
enabled: !!userId,
staleTime: Infinity
staleTime: Infinity,
...options
}
);
}
Expand Down

0 comments on commit 116f07e

Please sign in to comment.