11<template >
2- <Select >
3- <SelectTrigger >
4- <SelectValue placeholder =" Select Chain" />
5- </SelectTrigger >
6- <SelectContent >
7- <SelectGroup
8- v-for =" chain in filteredChains"
9- :key =" chain.id"
10- :value =" chain.id.toString()"
11- :class =" className"
12- >
13- <SelectItem :value =" chain.id.toString()" >
14- <img :src =" chain.icon" class =" size-4" :alt =" chain.name" />
15- {{ chain.name }}
2+ <div class =" flex items-center" >
3+ <Select v-model =" selectedChainId" :class =" className" >
4+ <SelectTrigger >
5+ <SelectValue :placeholder =" selectedChain?.name || 'Select chain'" >
6+ <div v-if =" selectedChain" class =" flex items-center gap-2" >
7+ <img
8+ v-if =" selectedChain.icon"
9+ :src =" selectedChain.icon"
10+ :alt =" selectedChain.name"
11+ class =" h-4 w-4 rounded-full"
12+ />
13+ <span >{{ selectedChain.name }}</span >
14+ </div >
15+ <span v-else >Select Chain</span >
16+ </SelectValue >
17+ </SelectTrigger >
18+
19+ <SelectContent >
20+ <SelectItem
21+ v-for =" chain in filteredChains"
22+ :key =" chain.id"
23+ :value =" chain.id.toString()"
24+ >
25+ <SelectItemText >
26+ <div class =" flex items-center gap-2" >
27+ <img
28+ v-if =" chain.icon"
29+ :src =" chain.icon"
30+ :alt =" chain.name"
31+ class =" h-4 w-4 rounded-full"
32+ />
33+ <span >{{ chain.name }}</span >
34+ </div >
35+ </SelectItemText >
1636 </SelectItem >
17- </SelectGroup >
18- </SelectContent >
19- </Select >
37+ </SelectContent >
38+ </Select >
39+ </div >
2040</template >
2141
2242<script setup lang="ts">
23- import { computed } from ' vue' ;
43+ import { computed , watch } from ' vue' ;
2444import { useAccount } from ' @wagmi/vue' ;
2545import { useChainSwitch } from ' @/hooks/useChainSwitch' ;
2646import { getSupportedChains , getChainById } from ' @/utils/chain.utils' ;
2747import useUserStore from ' @/stores/useUser.store' ;
2848import {
2949 Select ,
3050 SelectContent ,
31- SelectGroup ,
3251 SelectItem ,
52+ SelectItemText ,
3353 SelectTrigger ,
3454 SelectValue ,
3555} from ' @/components/ui/select' ;
@@ -49,32 +69,34 @@ const userStore = useUserStore();
4969const filteredChains = getSupportedChains ();
5070
5171// Computed
72+ const selectedChain = computed (() => {
73+ const currentChainId = chainId .value || userStore .chainId ;
74+ return currentChainId ? getChainById (currentChainId ) : undefined ;
75+ });
76+
5277const selectedChainId = computed ({
5378 get : () => {
54- return (chainId .value || userStore .chainId || - 1 ).toString ();
79+ const currentChainId = chainId .value || userStore .chainId ;
80+ return currentChainId ? currentChainId .toString () : ' ' ;
5581 },
56- set : (value : string ) => {
82+ set : async (value : string ) => {
5783 const numericValue = Number (value );
58- if (numericValue !== - 1 ) {
84+ if (numericValue && numericValue !== - 1 ) {
5985 const chain = getChainById (numericValue );
6086 if (chain ) {
6187 userStore .setSelectedChain (chain );
88+ await requestChainChange (numericValue );
6289 }
6390 }
6491 },
6592});
6693
67- // Methods
68- async function handleChainChange(value : string ) {
69- const numericValue = Number (value );
70-
71- if (numericValue === - 1 ) return ;
72-
73- const chain = getChainById (numericValue );
74- if (chain ) {
75- userStore .setSelectedChain (chain );
76-
77- await requestChainChange (numericValue );
94+ watch (chainId , (newChainId ) => {
95+ if (newChainId ) {
96+ const chain = getChainById (newChainId );
97+ if (chain ) {
98+ userStore .setSelectedChain (chain );
99+ }
78100 }
79- }
101+ });
80102 </script >
0 commit comments