forked from Dshield-xyz/Dshield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeader.tsx
More file actions
286 lines (264 loc) · 10.5 KB
/
Copy pathHeader.tsx
File metadata and controls
286 lines (264 loc) · 10.5 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"use client";
import { useState, useEffect, useRef } from "react";
import Image from "next/image";
import Link from "next/link";
import { usePathname } from "next/navigation";
import * as StellarSdk from "@stellar/stellar-sdk";
import { useWallet } from "./WalletProvider";
import { Button } from "./ui/Button";
import { MenuIcon, CloseIcon } from "./icons";
import { truncateMiddle } from "@/lib/format";
import { cn } from "@/lib/cn";
import { getRpcServer, queryContract, getUsdcSacId } from "@/lib/stellar";
const NAV_ITEMS = [
{ href: "/deposit", label: "Deposit" },
{ href: "/withdraw", label: "Withdraw" },
{ href: "/compliance", label: "Compliance" },
{ href: "/history", label: "History" },
];
interface Balances {
xlm: string;
usdc: string;
}
async function fetchBalances(address: string): Promise<Balances> {
const server = getRpcServer();
const accountKey = StellarSdk.xdr.LedgerKey.account(
new StellarSdk.xdr.LedgerKeyAccount({
accountId: StellarSdk.Keypair.fromPublicKey(address).xdrAccountId(),
}),
);
const [ledger, usdcVal] = await Promise.all([
server.getLedgerEntries(accountKey).catch(() => null),
(async () => {
const sacId = getUsdcSacId();
if (!sacId) return null;
return queryContract(sacId, "balance", [
StellarSdk.nativeToScVal(address, { type: "address" }),
]);
})(),
]);
let xlm = "0.0000";
if (ledger && ledger.entries.length > 0) {
const stroops = ledger.entries[0].val.account().balance();
xlm = (Number(stroops) / 1e7).toFixed(4);
}
let usdc = "0.00";
if (usdcVal) {
const raw = BigInt(StellarSdk.scValToNative(usdcVal) as string | number);
usdc = (Number(raw) / 1e7).toFixed(2);
}
return { xlm, usdc };
}
export function Header() {
const { address, connect, disconnect, isConnecting } = useWallet();
const pathname = usePathname();
const [mobileOpen, setMobileOpen] = useState(false);
const [balanceOpen, setBalanceOpen] = useState(false);
const [balances, setBalances] = useState<Balances | null>(null);
const [loadingBalances, setLoadingBalances] = useState(false);
const dropdownRef = useRef<HTMLDivElement>(null);
const isLanding = pathname === "/";
const [hidden, setHidden] = useState(false);
useEffect(() => {
let lastY = window.scrollY;
function onScroll() {
const y = window.scrollY;
setHidden(y > lastY && y > 80);
lastY = y;
}
window.addEventListener("scroll", onScroll, { passive: true });
return () => window.removeEventListener("scroll", onScroll);
}, []);
/* eslint-disable react-hooks/set-state-in-effect -- loading state must
* flip true synchronously when the dropdown opens, before the async
* fetch resolves, so the UI shows a loading state immediately. */
useEffect(() => {
if (!balanceOpen || !address) return;
setLoadingBalances(true);
fetchBalances(address)
.then(setBalances)
.catch(() => setBalances(null))
.finally(() => setLoadingBalances(false));
}, [balanceOpen, address]);
/* eslint-enable react-hooks/set-state-in-effect */
useEffect(() => {
if (!balanceOpen) return;
function handleClickOutside(e: MouseEvent) {
if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) {
setBalanceOpen(false);
}
}
document.addEventListener("mousedown", handleClickOutside);
return () => document.removeEventListener("mousedown", handleClickOutside);
}, [balanceOpen]);
return (
<header
className={cn(
"sticky top-0 z-50 px-3 pt-3 sm:px-4 sm:pt-4 transition-transform duration-300",
hidden && "-translate-y-full",
)}
>
<div className="aurora-border mx-auto max-w-5xl rounded-2xl border border-zinc-800/80 bg-zinc-950/70 shadow-lg shadow-black/20 backdrop-blur-xl">
<div className="flex h-14 items-center justify-between px-3 sm:px-4">
{/* Logo */}
<Link
href="/"
className="flex items-center gap-2 text-lg font-bold text-white"
>
<Image
src="/dshield-mark.png"
alt="DShield"
width={25}
height={28}
priority
/>
<span className="hidden xs:inline">DShield</span>
</Link>
{/* Landing page: live-status pill sits where the nav would be */}
{isLanding && (
<div className="hidden items-center gap-2.5 rounded-full border border-brand-500/25 bg-brand-500/10 px-3.5 py-1.5 text-xs font-medium text-brand-200 sm:inline-flex">
<span className="relative flex h-2 w-2" aria-hidden="true">
<span className="absolute inline-flex h-full w-full animate-ping rounded-full bg-green-400 opacity-60" />
<span className="relative inline-flex h-2 w-2 rounded-full bg-green-400" />
</span>
Live on Stellar Testnet
</div>
)}
{/* Desktop nav — hidden on landing page */}
{!isLanding && (
<nav className="hidden gap-1 md:flex">
{NAV_ITEMS.map(({ href, label }) => (
<Link
key={href}
href={href}
className={cn(
"rounded-lg px-3 py-2 text-sm font-medium transition-colors",
pathname === href
? "bg-zinc-800 text-white"
: "text-zinc-400 hover:bg-zinc-800/50 hover:text-white",
)}
>
{label}
</Link>
))}
</nav>
)}
<div className="flex items-center gap-2">
{/* Wallet */}
{address ? (
<div className="flex items-center gap-2">
{/* Clickable address pill with balance dropdown */}
<div className="relative hidden sm:block" ref={dropdownRef}>
<button
onClick={() => setBalanceOpen((o) => !o)}
aria-expanded={balanceOpen}
aria-haspopup="true"
className="focus-ring rounded-full bg-zinc-800 px-3 py-1.5 font-mono text-xs text-zinc-300 transition-colors hover:bg-zinc-700 hover:text-white"
>
{truncateMiddle(address, 4, 4)}
</button>
{balanceOpen && (
<div className="absolute right-0 top-full mt-2 w-48 rounded-xl border border-zinc-800 bg-zinc-950 p-3 shadow-xl shadow-black/40">
<p className="mb-2 text-[10px] font-semibold uppercase tracking-widest text-zinc-500">
Balances
</p>
{loadingBalances ? (
<p className="text-xs text-zinc-500">Loading…</p>
) : balances ? (
<div className="space-y-2">
<div className="flex items-center justify-between">
<span className="text-xs text-zinc-400">XLM</span>
<span className="font-mono text-xs text-white">
{balances.xlm}
</span>
</div>
<div className="flex items-center justify-between">
<span className="text-xs text-zinc-400">USDC</span>
<span className="font-mono text-xs text-white">
{balances.usdc}
</span>
</div>
</div>
) : (
<p className="text-xs text-zinc-500">Unavailable</p>
)}
<div className="mt-3 border-t border-zinc-800 pt-2">
<button
onClick={() => { disconnect(); setBalanceOpen(false); }}
className="w-full text-left text-xs text-zinc-500 transition-colors hover:text-red-400"
>
Disconnect
</button>
</div>
</div>
)}
</div>
<Button
variant="ghost"
size="sm"
onClick={disconnect}
className="text-zinc-500 sm:hidden"
>
Disconnect
</Button>
</div>
) : (
<Button
size="sm"
onClick={connect}
disabled={isConnecting}
className="sm:px-4 sm:py-2 sm:text-sm"
>
{isConnecting ? "Connecting..." : "Connect Wallet"}
</Button>
)}
{/* Mobile hamburger — hidden on landing page */}
{!isLanding && (
<button
onClick={() => setMobileOpen(!mobileOpen)}
aria-expanded={mobileOpen}
className="focus-ring flex items-center justify-center rounded-lg p-2 text-zinc-400 transition-colors hover:text-white md:hidden"
aria-label="Toggle menu"
>
{mobileOpen ? (
<CloseIcon className="h-5 w-5" />
) : (
<MenuIcon className="h-5 w-5" />
)}
</button>
)}
</div>
</div>
{/* Mobile nav dropdown — hidden on landing page */}
{!isLanding && mobileOpen && (
<nav className="border-t border-zinc-800 px-3 py-3 md:hidden">
<div className="flex flex-col gap-1">
{NAV_ITEMS.map(({ href, label }) => (
<Link
key={href}
href={href}
onClick={() => setMobileOpen(false)}
className={cn(
"rounded-lg px-3 py-2.5 text-sm font-medium transition-colors",
pathname === href
? "bg-zinc-800 text-white"
: "text-zinc-400 hover:bg-zinc-800/50 hover:text-white",
)}
>
{label}
</Link>
))}
</div>
{address && (
<div className="mt-2 border-t border-zinc-800/50 pt-2">
<span className="block rounded-lg px-3 py-2 font-mono text-xs text-zinc-500">
{truncateMiddle(address, 8, 8)}
</span>
</div>
)}
</nav>
)}
</div>
</header>
);
}