11import * as DropdownMenu from "@radix-ui/react-dropdown-menu" ;
2+ import { useState } from "react" ;
23import { useSorokit } from "@/context/useSorokit" ;
34import { cn } from "@/lib/utils" ;
45import { HugeiconsIcon } from "@hugeicons/react" ;
@@ -14,23 +15,41 @@ const NETWORKS: { name: NetworkName; label: string; dot: string }[] = [
1415
1516export function NetworkSwitcher ( ) {
1617 const { network, switchNetwork } = useSorokit ( ) ;
18+ const [ isSwitching , setIsSwitching ] = useState ( false ) ;
1719 const current = NETWORKS . find ( ( n ) => n . name === network ?. name ) ?? NETWORKS [ 1 ] ;
1820
21+ const handleSelect = async ( name : NetworkName ) => {
22+ if ( isSwitching ) return ;
23+ setIsSwitching ( true ) ;
24+ try {
25+ await switchNetwork ( name ) ;
26+ } finally {
27+ setIsSwitching ( false ) ;
28+ }
29+ } ;
30+
1931 return (
2032 < DropdownMenu . Root >
2133 < DropdownMenu . Trigger asChild >
22- < button className = "inline-flex items-center gap-2 h-8 px-3.5 rounded-lg bg-surface-2 border border-line hover:border-line-2 transition-colors cursor-pointer text-[12px] text-ink-2 focus-visible:outline-none" >
34+ < button
35+ disabled = { isSwitching }
36+ className = "inline-flex items-center gap-2 h-8 px-3.5 rounded-lg bg-surface-2 border border-line hover:border-line-2 transition-colors cursor-pointer text-[12px] text-ink-2 focus-visible:outline-none disabled:opacity-50 disabled:cursor-not-allowed"
37+ >
2338 < span
2439 className = { cn ( "w-1.5 h-1.5 rounded-full shrink-0" , current . dot ) }
2540 />
2641 { current . label }
27- < HugeiconsIcon
28- icon = { ArrowDown01Icon }
29- size = { 10 }
30- color = "currentColor"
31- strokeWidth = { 2 }
32- className = "opacity-40 ml-0.5"
33- />
42+ { isSwitching ? (
43+ < span className = "ml-0.5 h-3 w-3 animate-spin rounded-full border-2 border-ink-2 border-t-transparent" />
44+ ) : (
45+ < HugeiconsIcon
46+ icon = { ArrowDown01Icon }
47+ size = { 10 }
48+ color = "currentColor"
49+ strokeWidth = { 2 }
50+ className = "opacity-40 ml-0.5"
51+ />
52+ ) }
3453 </ button >
3554 </ DropdownMenu . Trigger >
3655
@@ -43,7 +62,8 @@ export function NetworkSwitcher() {
4362 { NETWORKS . map ( ( net ) => (
4463 < DropdownMenu . Item
4564 key = { net . name }
46- onSelect = { ( ) => switchNetwork ( net . name ) }
65+ disabled = { isSwitching }
66+ onSelect = { ( ) => handleSelect ( net . name ) }
4767 className = { cn (
4868 "flex items-center gap-2.5 px-3 py-2 rounded-lg text-[12px] cursor-pointer outline-none transition-colors" ,
4969 network ?. name === net . name
0 commit comments