forked from solana-foundation/templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster-select.tsx
More file actions
78 lines (73 loc) · 2.45 KB
/
cluster-select.tsx
File metadata and controls
78 lines (73 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"use client";
import { useState, useRef, useEffect } from "react";
import { useCluster, CLUSTERS } from "./cluster-context";
export function ClusterSelect() {
const { cluster, setCluster } = useCluster();
const [isOpen, setIsOpen] = useState(false);
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
function handleClickOutside(e: MouseEvent) {
if (ref.current && !ref.current.contains(e.target as Node)) {
setIsOpen(false);
}
}
document.addEventListener("mousedown", handleClickOutside);
return () => document.removeEventListener("mousedown", handleClickOutside);
}, []);
return (
<div className="relative" ref={ref}>
<button
onClick={() => setIsOpen(!isOpen)}
className="flex cursor-pointer items-center gap-2 rounded-lg border border-border-low bg-card px-3 py-2 text-xs font-medium transition hover:bg-cream"
>
<span
className="h-2 w-2 rounded-full"
style={{
backgroundColor:
cluster === "mainnet"
? "#22c55e"
: cluster === "devnet"
? "#3b82f6"
: cluster === "testnet"
? "#eab308"
: "#a3a3a3",
}}
/>
{cluster}
</button>
{isOpen && (
<div className="absolute right-0 top-full z-50 mt-2 w-40 rounded-xl border border-border-low bg-card p-2 shadow-lg">
<div className="space-y-1">
{CLUSTERS.map((c) => (
<button
key={c}
onClick={() => {
setCluster(c);
setIsOpen(false);
}}
className={`flex w-full cursor-pointer items-center gap-2 rounded-lg px-3 py-2 text-left text-xs font-medium transition hover:bg-cream ${
c === cluster ? "bg-cream" : ""
}`}
>
<span
className="h-2 w-2 rounded-full"
style={{
backgroundColor:
c === "mainnet"
? "#22c55e"
: c === "devnet"
? "#3b82f6"
: c === "testnet"
? "#eab308"
: "#a3a3a3",
}}
/>
{c}
</button>
))}
</div>
</div>
)}
</div>
);
}