Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wallet): save selected wallet type local storage #570

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions apps/deploy-web/src/components/layout/WalletStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ const withBilling = browserEnvConfig.NEXT_PUBLIC_BILLING_ENABLED;

export function WalletStatus() {
const { walletName, isWalletLoaded, isWalletConnected, isManaged, isWalletLoading, isTrialing } = useWallet();
const { balance: walletBalance } = useWalletBalance();
const { balance: walletBalance, isLoading: isWalletBalanceLoading } = useWalletBalance();
const [isSignedInWithTrial] = useAtom(walletStore.isSignedInWithTrial);
const [open, setOpen] = useState(false);
const isLoadingBalance = isWalletBalanceLoading && !walletBalance;
const isInit = isWalletLoaded && !isWalletLoading && !isLoadingBalance;

return (
<>
{isWalletLoaded && !isWalletLoading ? (
{isInit ? (
isWalletConnected ? (
<div className="flex w-full items-center">
<div className="w-full py-2">
Expand Down
13 changes: 10 additions & 3 deletions apps/deploy-web/src/components/wallet/ManagedWalletPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Button, Separator } from "@akashnetwork/ui/components";
import { CoinsSwap, HandCard } from "iconoir-react";

import { TopUpAmountPicker } from "@src/components/top-up-amount-picker/TopUpAmountPicker";
import { useSelectedChain } from "@src/context/CustomChainProvider";
import { useWallet } from "@src/context/WalletProvider";
import { useLoginRequiredEventHandler } from "@src/hooks/useLoginRequiredEventHandler";
import { useManagedEscrowFaqModal } from "@src/hooks/useManagedEscrowFaqModal";
Expand All @@ -18,9 +19,10 @@ interface ManagedWalletPopupProps extends React.PropsWithChildren {

export const ManagedWalletPopup: React.FC<ManagedWalletPopupProps> = ({ walletBalance }) => {
const isEmailVerified = useIsEmailVerified();
const { switchWalletType, isManaged, isTrialing } = useWallet();
const { isManaged, isTrialing, switchWalletType } = useWallet();
const whenLoggedIn = useLoginRequiredEventHandler();
const { showManagedEscrowFaqModal } = useManagedEscrowFaqModal();
const { connect, isWalletConnected } = useSelectedChain();

const goToCheckout = () => {
window.location.href = "/api/proxy/v1/checkout";
Expand Down Expand Up @@ -77,12 +79,17 @@ export const ManagedWalletPopup: React.FC<ManagedWalletPopupProps> = ({ walletBa
<VerifyEmail />

<TopUpAmountPicker mdMode="click" className="w-full">
<Button onClick={whenLoggedIn(goToCheckout, "Sign In or Sign Up to add funds")} variant="outline" className="w-full space-x-2" disabled={!isEmailVerified}>
<Button
onClick={whenLoggedIn(goToCheckout, "Sign In or Sign Up to add funds")}
variant="outline"
className="w-full space-x-2"
disabled={!isEmailVerified}
>
<HandCard />
<span>Add Funds</span>
</Button>
</TopUpAmountPicker>
<Button onClick={switchWalletType} variant="outline" className="w-full space-x-2">
<Button onClick={isWalletConnected ? switchWalletType : connect} variant="outline" className="w-full space-x-2">
<CoinsSwap />
<span>Switch to Wallet Payments</span>
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,17 @@ export function useSelectedChain() {
}

const ModalWrapper = (props: WalletModalProps) => {
const [, setIsWalletModalOpen] = useAtom(walletStore.isWalletModalOpen);
const { isWalletConnected } = useSelectedChain();
const [isWalletModalOpen, setIsWalletModalOpen] = useAtom(walletStore.isWalletModalOpen);
const [, setSelectedWalletType] = useAtom(walletStore.selectedWalletType);

useEffect(() => {
setIsWalletModalOpen(props.isOpen);
}, [props.isOpen]);

if (isWalletModalOpen && !props.isOpen && isWalletConnected) {
setSelectedWalletType("custodial");
}
}, [isWalletModalOpen, props.isOpen, isWalletConnected]);

return <DefaultModal {...props} isOpen={props.isOpen} setOpen={props.setOpen} />;
};
24 changes: 11 additions & 13 deletions apps/deploy-web/src/context/WalletProvider/WalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import networkStore from "@src/store/networkStore";
import walletStore from "@src/store/walletStore";
import { AnalyticsCategory, AnalyticsEvents } from "@src/types/analytics";
import { UrlService } from "@src/utils/urlUtils";
import { getSelectedStorageWallet, getStorageWallets, updateStorageManagedWallet, updateStorageWallets } from "@src/utils/walletUtils";
import { getStorageWallets, updateStorageManagedWallet, updateStorageWallets } from "@src/utils/walletUtils";
import { useSelectedChain } from "../CustomChainProvider";
import { useSettings } from "../SettingsProvider";

Expand Down Expand Up @@ -66,8 +66,6 @@ const MESSAGE_STATES: Record<string, LoadingState> = {
"/akash.deployment.v1beta3.MsgDepositDeployment": "depositingDeployment"
};

const initialWallet = getSelectedStorageWallet();

export const WalletProvider = ({ children }) => {
const [isWalletLoaded, setIsWalletLoaded] = useState<boolean>(true);
const [loadingState, setLoadingState] = useState<LoadingState | undefined>(undefined);
Expand All @@ -76,11 +74,9 @@ export const WalletProvider = ({ children }) => {
const { settings } = useSettings();
const user = useUser();
const userWallet = useSelectedChain();
const { wallet: managedWallet, isLoading, create: createManagedWallet } = useManagedWallet();
const [isWalletModalOpen, setIsWalletModelOpen] = useAtom(walletStore.isWalletModalOpen);
const [selectedWalletType, selectWalletType] = useState<"managed" | "custodial">(
initialWallet?.selected && initialWallet?.isManaged ? "managed" : "custodial"
);
const { wallet: managedWallet, isLoading: isManagedWalletLoading, create: createManagedWallet } = useManagedWallet();
const [, setIsWalletModelOpen] = useAtom(walletStore.isWalletModalOpen);
const [selectedWalletType, setSelectedWalletType] = useAtom(walletStore.selectedWalletType);
const {
address: walletAddress,
username,
Expand All @@ -93,11 +89,9 @@ export const WalletProvider = ({ children }) => {
fee: { default: feeGranter }
} = useAllowance(walletAddress as string, isManaged);
const [selectedNetworkId, setSelectedNetworkId] = networkStore.useSelectedNetworkIdStore({ reloadOnChange: true });
const shouldAutoConnectManagedWallet =
!isWalletConnected && selectedWalletType === "custodial" && !!managedWallet && !isWalletModalOpen && !userWallet.isWalletConnecting;
const isLoading = (selectedWalletType === "managed" && isManagedWalletLoading) || (selectedWalletType === "custodial" && userWallet.isWalletConnecting);

useWhen(walletAddress, loadWallet);
useWhen(shouldAutoConnectManagedWallet, switchWalletType);

useEffect(() => {
if (!settings.apiEndpoint || !settings.rpcEndpoint) return;
Expand Down Expand Up @@ -126,14 +120,14 @@ export const WalletProvider = ({ children }) => {
});
}

selectWalletType(prev => (prev === "custodial" ? "managed" : "custodial"));
setSelectedWalletType(prev => (prev === "custodial" ? "managed" : "custodial"));
}

function connectManagedWallet() {
if (!managedWallet) {
createManagedWallet();
}
selectWalletType("managed");
setSelectedWalletType("managed");
}

function logout() {
Expand All @@ -145,6 +139,10 @@ export const WalletProvider = ({ children }) => {
});

router.push(UrlService.home());

if (managedWallet) {
setSelectedWalletType("managed");
}
}

async function connectWallet() {
Expand Down
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 && !userWallet.isWalletConnecting) {
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
2 changes: 2 additions & 0 deletions apps/deploy-web/src/store/walletStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { atom } from "jotai";
import { atomWithStorage } from "jotai/utils";

const isSignedInWithTrial = atomWithStorage<boolean>("isSignedInWithTrial", false);
const selectedWalletType = atomWithStorage<"managed" | "custodial">("selectedWalletType", "custodial");
const isWalletModalOpen = atom<boolean>(false);

export default {
isSignedInWithTrial,
selectedWalletType,
isWalletModalOpen
};
Loading