-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bridge-ui-v2): switch chain on wrong network (#14511)
- Loading branch information
1 parent
3407f82
commit 89b8a86
Showing
8 changed files
with
119 additions
and
10 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
79 changes: 79 additions & 0 deletions
79
packages/bridge-ui-v2/src/components/SwitchChainModal/SwitchChainModal.svelte
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,79 @@ | ||
<script lang="ts"> | ||
import { type Chain, switchNetwork } from '@wagmi/core'; | ||
import { t } from 'svelte-i18n'; | ||
import { UserRejectedRequestError } from 'viem'; | ||
import { LoadingMask } from '$components/LoadingMask'; | ||
import { warningToast } from '$components/NotificationToast'; | ||
import { chains } from '$libs/chain'; | ||
import { chainToIconMap } from '$libs/util/chainToIconMap'; | ||
import { switchChainModal } from '$stores/modal'; | ||
let switchingNetwork = false; | ||
function closeModal() { | ||
$switchChainModal = false; | ||
} | ||
async function selectChain(chain: Chain) { | ||
// We want to switch the wallet to the selected network. | ||
// This will trigger the network switch in the UI also | ||
switchingNetwork = true; | ||
try { | ||
await switchNetwork({ chainId: chain.id }); | ||
closeModal(); | ||
} catch (err) { | ||
console.error(err); | ||
if (err instanceof UserRejectedRequestError) { | ||
warningToast($t('messages.network.rejected')); | ||
} | ||
} finally { | ||
switchingNetwork = false; | ||
} | ||
} | ||
function getChainKeydownHandler(chain: Chain) { | ||
return (event: KeyboardEvent) => { | ||
if (event.key === 'Enter') { | ||
selectChain(chain); | ||
} | ||
}; | ||
} | ||
</script> | ||
|
||
<dialog class="modal modal-bottom md:modal-middle" class:modal-open={$switchChainModal}> | ||
<div class="modal-box relative px-6 py-[35px] md:py-[20px] bg-primary-base-background text-primary-base-content"> | ||
{#if switchingNetwork} | ||
<LoadingMask | ||
class="bg-grey-0/60" | ||
spinnerClass="border-primary-base-content" | ||
text={$t('messages.network.switching')} /> | ||
{/if} | ||
|
||
<h3 class="title-body-bold mb-[20px]">{$t('switch_modal.title')}</h3> | ||
<p class="body-regular">{$t('switch_modal.description')}</p> | ||
<ul role="menu" class="space-y-4"> | ||
{#each chains as chain (chain.id)} | ||
<li | ||
role="menuitem" | ||
tabindex="0" | ||
class="p-4 rounded-[10px] hover:bg-primary-content hover:cursor-pointer" | ||
on:click={() => selectChain(chain)} | ||
on:keydown={getChainKeydownHandler(chain)}> | ||
<!-- TODO: agree on hover:bg color --> | ||
<div class="f-row justify-between"> | ||
<div class="f-items-center space-x-4"> | ||
<i role="img" aria-label={chain.name}> | ||
<svelte:component this={chainToIconMap[chain.id]} size={32} /> | ||
</i> | ||
<span class="body-bold">{chain.name}</span> | ||
</div> | ||
<span class="body-regular">{chain.network}</span> | ||
</div> | ||
</li> | ||
{/each} | ||
</ul> | ||
</div> | ||
</dialog> |
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 @@ | ||
export { default as SwitchChainModal } from './SwitchChainModal.svelte'; |
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,10 @@ | ||
import type { ComponentType } from 'svelte'; | ||
|
||
import { EthIcon, TaikoIcon } from '$components/Icon'; | ||
import { PUBLIC_L1_CHAIN_ID, PUBLIC_L2_CHAIN_ID } from '$env/static/public'; | ||
|
||
export const chainToIconMap: Record<string, ComponentType> = { | ||
[PUBLIC_L1_CHAIN_ID]: EthIcon, | ||
[PUBLIC_L2_CHAIN_ID]: TaikoIcon, | ||
// TODO: L3 | ||
}; |
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
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,5 @@ | ||
import { writable } from 'svelte/store'; | ||
|
||
// We make this global because we need to be able to | ||
// open and close the modal from anywhere in the app | ||
export const switchChainModal = writable<boolean>(false); |
89b8a86
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
bridge-ui-v2 – ./packages/bridge-ui-v2
taiko-mono-bridge-ui-v2.vercel.app
bridge-ui-v2-git-main-taikoxyz.vercel.app
bridge-ui-v2-taikoxyz.vercel.app