-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor connect button (#182)
* move files * create dedicated components * more and smaller components * less prop drilling * simplify logic * more cohesion for provider logo component * update import * move wallet connect closer to its usage * create @/chains alias * use more from route, less duplicate checks * routes aren't always the same
- Loading branch information
1 parent
d76586d
commit fd8581a
Showing
24 changed files
with
383 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,11 @@ | ||
export { default as ForkProvider } from './ForkProvider' | ||
export { getReadOnlyProvider } from './readOnlyProvider' | ||
export { ProvideInjectedWallet, useInjectedWallet } from './useInjectedWallet' | ||
export { default as useWalletConnect } from './useWalletConnect' | ||
export { | ||
ProvideInjectedWallet, | ||
useInjectedWallet, | ||
type InjectedWalletContextT, | ||
} from './useInjectedWallet' | ||
export { | ||
default as useWalletConnect, | ||
type WalletConnectResult, | ||
} from './useWalletConnect' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...rc/panel/routes/ConnectButton/Account.tsx → .../src/panel/routes/Edit/wallet/Account.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { WalletConnectResult } from '@/providers' | ||
import { ProviderType, Route } from '@/types' | ||
import { ZeroAddress } from 'ethers' | ||
import { ChainId, parsePrefixedAddress } from 'ser-kit' | ||
import { InjectedWalletContextT } from '../../../providers/useInjectedWallet' | ||
import { getChainId, isConnectedTo } from '../../routeHooks' | ||
import { InjectedWallet, InjectedWalletConnect } from './injectedWallet' | ||
import { WalletConnect, WalletConnectConnect } from './walletConnect' | ||
|
||
interface Props { | ||
route: Route | ||
onConnect(args: { | ||
providerType: ProviderType | ||
chainId: ChainId | ||
account: string | ||
}): void | ||
onDisconnect(): void | ||
} | ||
|
||
export const ConnectWallet = ({ route, onConnect, onDisconnect }: Props) => { | ||
const pilotAddress = getPilotAddress(route) | ||
const chainId = getChainId(route.avatar) | ||
|
||
const isConnected = ( | ||
provider: InjectedWalletContextT | WalletConnectResult | ||
) => | ||
route.initiator != null && isConnectedTo(provider, route.initiator, chainId) | ||
|
||
// not connected | ||
if (pilotAddress == null) { | ||
return ( | ||
<div className="flex flex-col gap-2"> | ||
<WalletConnectConnect | ||
routeId={route.id} | ||
onConnect={(chainId, account) => | ||
onConnect({ | ||
providerType: ProviderType.WalletConnect, | ||
chainId, | ||
account, | ||
}) | ||
} | ||
/> | ||
|
||
<InjectedWalletConnect | ||
onConnect={(chainId, account) => | ||
onConnect({ | ||
providerType: ProviderType.InjectedWallet, | ||
chainId, | ||
account, | ||
}) | ||
} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
switch (route.providerType) { | ||
case ProviderType.InjectedWallet: | ||
return ( | ||
<InjectedWallet | ||
chainId={chainId} | ||
pilotAddress={pilotAddress} | ||
onDisconnect={onDisconnect} | ||
isConnected={isConnected} | ||
/> | ||
) | ||
case ProviderType.WalletConnect: | ||
return ( | ||
<WalletConnect | ||
pilotAddress={pilotAddress} | ||
chainId={chainId} | ||
routeId={route.id} | ||
isConnected={isConnected} | ||
/> | ||
) | ||
} | ||
} | ||
|
||
const getPilotAddress = (route: Route) => { | ||
if (route.initiator == null) { | ||
return null | ||
} | ||
|
||
const address = parsePrefixedAddress(route.initiator)[1].toLowerCase() | ||
|
||
if (address === ZeroAddress) { | ||
return null | ||
} | ||
|
||
return address | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Button } from '@/components' | ||
import { PropsWithChildren } from 'react' | ||
import { Section } from './Section' | ||
|
||
type ConnectedProps = PropsWithChildren<{ | ||
onDisconnect: () => void | ||
}> | ||
|
||
export const Connected = ({ children, onDisconnect }: ConnectedProps) => ( | ||
<Section> | ||
{children} | ||
|
||
<Button onClick={onDisconnect}>Disconnect</Button> | ||
</Section> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { PropsWithChildren } from 'react' | ||
|
||
export const Section = ({ children }: PropsWithChildren) => ( | ||
<div className="flex flex-col gap-4">{children}</div> | ||
) | ||
|
||
const Actions = ({ children }: PropsWithChildren) => ( | ||
<div className="flex gap-2">{children}</div> | ||
) | ||
|
||
Section.Actions = Actions |
Oops, something went wrong.