Skip to content

Commit

Permalink
feat: change wallet connection flow
Browse files Browse the repository at this point in the history
  • Loading branch information
totraev committed Nov 29, 2024
1 parent 059f87c commit 7d21e3a
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-carrots-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@babylonlabs-io/bbn-wallet-connect": patch
---

feat: change wallet connection flow
5 changes: 3 additions & 2 deletions src/components/Chains/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import { useWalletConnect } from "@/hooks/useWalletConnect";
interface ContainerProps {
className?: string;
onClose?: () => void;
onConfirm?: () => void;
}

export default function ChainsContainer(props: ContainerProps) {
const { chains, selectedWallets, displayWallets } = useWidgetState();
const { connected } = useWalletConnect();
const { selected } = useWalletConnect();

const chainArr = useMemo(() => Object.values(chains), [chains]);

Expand All @@ -26,7 +27,7 @@ export default function ChainsContainer(props: ContainerProps) {

return (
<Chains
disabled={!connected}
disabled={!selected}
chains={chainArr}
selectedWallets={selectedWallets}
onSelectChain={handleSelectChain}
Expand Down
5 changes: 3 additions & 2 deletions src/components/Chains/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ interface ChainsProps {
className?: string;
selectedWallets?: Record<string, IWallet | undefined>;
onClose?: () => void;
onConfirm?: () => void;
onSelectChain?: (chain: IChain) => void;
}

export const Chains = memo(
({ disabled = false, chains, selectedWallets = {}, className, onClose, onSelectChain }: ChainsProps) => (
({ disabled = false, chains, selectedWallets = {}, className, onClose, onConfirm, onSelectChain }: ChainsProps) => (
<div className={twMerge("b-flex b-flex-1 b-flex-col", className)}>
<DialogHeader className="b-mb-10" title="Connect Wallets" onClose={onClose}>
<Text>Connect to both Bitcoin and Babylon Chain Wallets</Text>
Expand Down Expand Up @@ -53,7 +54,7 @@ export const Chains = memo(
Cancel
</Button>

<Button disabled={disabled} fluid onClick={onClose}>
<Button disabled={disabled} fluid onClick={onConfirm}>
Done
</Button>
</DialogFooter>
Expand Down
5 changes: 4 additions & 1 deletion src/components/WalletProvider/components/Screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ interface ScreenProps {
onAccepTermsOfService?: () => void;
onToggleInscriptions?: (value: boolean, showAgain: boolean) => void;
onClose?: () => void;
onConfirm?: () => void;
}

const SCREENS = {
TERMS_OF_SERVICE: ({ className, onClose, onAccepTermsOfService }: ScreenProps) => (
<TermsOfService className={className} onClose={onClose} onSubmit={onAccepTermsOfService} />
),
CHAINS: ({ className, onClose }: ScreenProps) => <Chains className={className} onClose={onClose} />,
CHAINS: ({ className, onClose, onConfirm }: ScreenProps) => (
<Chains className={className} onClose={onClose} onConfirm={onConfirm} />
),
WALLETS: ({ className, widgets, onClose, onSelectWallet }: ScreenProps) => (
<Wallets widgets={widgets} className={className} onClose={onClose} onSelectWallet={onSelectWallet} />
),
Expand Down
29 changes: 26 additions & 3 deletions src/components/WalletProvider/components/WalletDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,20 @@ interface WalletDialogProps {
widgets?: Record<string, JSX.Element | undefined>;
}

const ANIMATION_DELAY = 1000;

export function WalletDialog({ widgets, onError }: WalletDialogProps) {
const { visible, screen, close, selectWallet, displayLoader, displayChains, displayInscriptions } = useWidgetState();
const {
visible,
screen,
close,
reset = () => {},
confirm,
selectWallet,
displayLoader,
displayChains,
displayInscriptions,
} = useWidgetState();
const { showAgain, toggleShowAgain, toggleLockInscriptions } = useInscriptionProvider();
const connectors = useChainProviders();

Expand Down Expand Up @@ -52,13 +64,24 @@ export function WalletDialog({ widgets, onError }: WalletDialogProps) {
[toggleShowAgain, toggleLockInscriptions, displayChains],
);

const handleClose = useCallback(() => {
close?.();
setTimeout(reset, ANIMATION_DELAY);
}, [close, reset]);

const handleConfirm = useCallback(() => {
confirm?.();
close?.();
}, [confirm]);

return (
<Dialog open={visible} onClose={close}>
<Dialog open={visible} onClose={handleClose}>
<Screen
current={screen}
widgets={widgets}
className="b-size-[600px]"
onClose={close}
onClose={handleClose}
onConfirm={handleConfirm}
onSelectWallet={handleSelectWallet}
onAccepTermsOfService={displayChains}
onToggleInscriptions={handleToggleInscriptions}
Expand Down
2 changes: 1 addition & 1 deletion src/components/WalletProvider/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Button, ScrollLocker } from "@babylonlabs-io/bbn-core-ui";

import { useWidgetState } from "@/hooks/useWidgetState";
import { Network } from "@/core/types";
import { bbnDevnet } from "@/core/chains/bbnDevnet";

import { WalletProvider } from "./index";
import { bbnDevnet } from "@/core/chains/bbnDevnet";

const meta: Meta<typeof WalletProvider> = {
component: WalletProvider,
Expand Down
7 changes: 4 additions & 3 deletions src/hooks/useWalletConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useMemo } from "react";
import { useWidgetState } from "./useWidgetState";

export function useWalletConnect() {
const { chains: chainMap, selectedWallets, open: openModal, close, reset } = useWidgetState();
const { confirmed, chains: chainMap, selectedWallets, open: openModal, close, reset } = useWidgetState();

const open = useCallback(() => {
reset?.();
Expand All @@ -13,15 +13,16 @@ export function useWalletConnect() {
reset?.();
}, [close]);

const connected = useMemo(() => {
const selected = useMemo(() => {
const chains = Object.values(chainMap).filter(Boolean);
const result = chains.map((chain) => selectedWallets[chain.id]);

return result.every(Boolean);
}, [chainMap, selectedWallets]);

return {
connected,
selected,
connected: selected && confirmed,
open,
disconnect,
};
Expand Down
5 changes: 5 additions & 0 deletions src/state/state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IChain, IWallet } from "@/core/types";
import { Actions, type State } from "./types";

const defaultState: State = {
confirmed: false,
visible: false,
screen: { type: "TERMS_OF_SERVICE" },
chains: {},
Expand Down Expand Up @@ -66,6 +67,10 @@ export function StateProvider({ children }: PropsWithChildren) {
addChain: (chain: IChain) => {
setState((state) => ({ ...state, chains: { ...state.chains, [chain.id]: chain } }));
},

confirm: () => {
setState((state) => ({ ...state, confirmed: true }));
},
}),
[],
);
Expand Down
2 changes: 2 additions & 0 deletions src/state/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type Screens =
| Screen<"INSCRIPTIONS">;

export interface State {
confirmed: boolean;
visible: boolean;
screen: Screens;
selectedWallets: Record<string, IWallet | undefined>;
Expand All @@ -30,5 +31,6 @@ export interface Actions {
selectWallet?: (chain: string, wallet: IWallet) => void;
removeWallet?: (chain: string) => void;
addChain?: (chain: IChain) => void;
confirm?: () => void;
reset?: () => void;
}

0 comments on commit 7d21e3a

Please sign in to comment.