Skip to content

Commit 99b4d9e

Browse files
authored
Merge pull request #73 from aabxtract/main
Fix network switching, banner fallback, and asset badge color scaling
2 parents 1896225 + 71c2981 commit 99b4d9e

3 files changed

Lines changed: 50 additions & 13 deletions

File tree

src/components/NetworkBanner.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ const CONFIG = {
3030
text: "text-ink-2",
3131
show: true,
3232
},
33+
custom: {
34+
label: null,
35+
dot: "bg-ink-3",
36+
bar: "bg-surface-2 border-b border-line",
37+
text: "text-ink-2",
38+
show: true,
39+
},
3340
};
3441

3542
interface NetworkBannerProps {
@@ -45,9 +52,11 @@ export function NetworkBanner({
4552
const { network } = useSorokit();
4653
if (!network) return null;
4754

48-
const cfg = CONFIG[network.name] ?? CONFIG.testnet;
55+
const cfg = CONFIG[network.name] ?? CONFIG.custom;
4956
if (!alwaysShow && !cfg.show) return null;
5057

58+
const label = cfg.label ?? network.name;
59+
5160
return (
5261
<div
5362
className={cn(
@@ -58,7 +67,7 @@ export function NetworkBanner({
5867
>
5968
<span className={cn("w-1.5 h-1.5 rounded-full shrink-0", cfg.dot)} />
6069
<span className={cfg.text}>
61-
You are on <strong>{cfg.label}</strong>
70+
You are on <strong>{label}</strong>
6271
{network.name !== "mainnet" && " — transactions use test funds only"}
6372
</span>
6473
</div>

src/components/NetworkSwitcher.tsx

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as DropdownMenu from "@radix-ui/react-dropdown-menu";
2+
import { useState } from "react";
23
import { useSorokit } from "@/context/useSorokit";
34
import { cn } from "@/lib/utils";
45
import { HugeiconsIcon } from "@hugeicons/react";
@@ -14,23 +15,41 @@ const NETWORKS: { name: NetworkName; label: string; dot: string }[] = [
1415

1516
export 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

src/context/SorokitProvider.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,16 @@ export function SorokitProvider({ client, children }: SorokitProviderProps) {
8686
const switchNetwork = useCallback(
8787
async (name: NetworkName) => {
8888
const { data, error } = await client.network.switchNetwork(name);
89-
if (data) setNetwork(data);
90-
if (error) setError(error);
89+
if (error) {
90+
setError(error);
91+
return;
92+
}
93+
if (data) {
94+
setNetwork(data);
95+
setAddress(null);
96+
setAccount(null);
97+
setBalances([]);
98+
}
9199
},
92100
[client],
93101
);

0 commit comments

Comments
 (0)