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

fix: connect wallet before confirm if not connected #170

Merged
merged 2 commits into from
Feb 10, 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import {
} from 'wagmi';
import type { AssetEth } from '~/systems/Assets/utils';

import { useEffect } from 'react';
import { useOverlay } from '~/systems/Overlay';
import { parseEthAddressToFuel } from '../utils';

export function useEthAccountConnection(props?: {
erc20Address?: `0x${string}`;
}) {
const overlay = useOverlay();
const { erc20Address } = props || {};
const { address, isConnected } = useAccount();
const { data: ensName } = useEnsName({ address });
Expand All @@ -30,6 +33,10 @@ export function useEthAccountConnection(props?: {
const { disconnect } = useDisconnect();
const paddedAddress = parseEthAddressToFuel(address);

useEffect(() => {
overlay.settings.closeOnBlur = !isConnecting;
}, [isConnecting]);

async function addAsset(asset: AssetEth) {
if (asset.address && walletClient) {
await walletClient?.watchAsset({
Expand All @@ -43,9 +50,14 @@ export function useEthAccountConnection(props?: {
}
}

function connect() {
overlay.settings.closeOnBlur = false;
setOpen(true);
}

return {
handlers: {
connect: () => setOpen(true),
connect,
disconnect,
addAsset,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import { BridgeSteps, BridgeTxOverview } from '~/systems/Bridge';
import { shortAddress } from '~/systems/Core';
import { useOverlay } from '~/systems/Overlay';

import { useEthAccountConnection } from '../../eth';
import { useTxFuelToEth } from '../hooks';

export function TxFuelToEthDialog() {
const { asset: ethAsset } = useAsset();
const { metadata } = useOverlay<{ txId: string }>();
const {
isConnected,
isConnecting,
handlers: ethHandlers,
} = useEthAccountConnection();
const {
steps,
status,
Expand Down Expand Up @@ -50,14 +56,25 @@ export function TxFuelToEthDialog() {
{(status?.isWaitingEthWalletApproval ||
status?.isConfirmTransactionLoading) && (
<Dialog.Footer>
<Button
intent="primary"
css={styles.actionButton}
isLoading={status.isConfirmTransactionLoading}
onClick={handlers.relayToEth}
>
Confirm Transaction
</Button>
{isConnected ? (
<Button
intent="primary"
css={styles.actionButton}
isLoading={status.isConfirmTransactionLoading}
onClick={handlers.relayToEth}
>
Confirm Transaction
</Button>
) : (
<Button
intent="primary"
css={styles.actionButton}
isLoading={isConnecting}
onClick={ethHandlers.connect}
>
Connect Ethereum Wallet
</Button>
)}
</Dialog.Footer>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function OverlayDialog() {
<Dialog
isOpen={overlay.isDialogOpen}
css={styles.dialog}
shouldCloseOnInteractOutside={() => overlay.settings.closeOnBlur}
onOpenChange={(isOpen) => !isOpen && overlay.close()}
>
<Dialog.Content css={styles.content}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const selectors = {
},
};

const settings = {
closeOnBlur: true,
};

export function useOverlay<T = void>() {
const isDialogOpen = store.useSelector(
Services.overlay,
Expand All @@ -31,6 +35,7 @@ export function useOverlay<T = void>() {
is,
isDialogOpen,
overlay,
settings,
metadata: metadata as T,
open: store.openOverlay,
close: store.closeOverlay,
Expand Down
Loading