forked from MergeFi/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWalletContext.tsx
More file actions
95 lines (84 loc) · 2.71 KB
/
Copy pathWalletContext.tsx
File metadata and controls
95 lines (84 loc) · 2.71 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
"use client";
import {
createContext,
useCallback,
useContext,
useEffect,
useState,
} from "react";
import { connectWallet as freighterConnect } from "@/lib/wallet";
import { apiRequest } from "@/lib/api";
import { useAuth } from "@/context/AuthContext";
const WALLET_KEY = "mergefi_wallet_address";
interface WalletContextValue {
address: string | null;
network: string | null;
connecting: boolean;
error: string | null;
connect: () => Promise<string | null>;
disconnect: () => void;
}
const WalletContext = createContext<WalletContextValue | null>(null);
export function WalletProvider({ children }: { children: React.ReactNode }) {
const { user, refresh } = useAuth();
const [address, setAddress] = useState<string | null>(null);
const [network, setNetwork] = useState<string | null>(null);
const [connecting, setConnecting] = useState(false);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
// localStorage is unavailable during SSR, so this can't be a lazy
// useState initializer — it must run after mount on the client.
const stored = window.localStorage.getItem(WALLET_KEY);
// eslint-disable-next-line react-hooks/set-state-in-effect
if (stored) setAddress(stored);
}, []);
const connect = useCallback(async () => {
setError(null);
setConnecting(true);
try {
const connection = await freighterConnect();
setAddress(connection.address);
setNetwork(connection.network);
window.localStorage.setItem(WALLET_KEY, connection.address);
if (user) {
try {
await apiRequest(`/users/${user.id}/stellar-address`, {
method: "PATCH",
body: JSON.stringify({ stellarAddress: connection.address }),
});
await refresh();
} catch {
// Linking to the profile is best-effort; the wallet is still usable
// for signing this session even if the backend write failed.
}
}
return connection.address;
} catch (err) {
setError(
err instanceof Error
? err.message
: "Install the Freighter wallet extension to continue.",
);
return null;
} finally {
setConnecting(false);
}
}, [user, refresh]);
const disconnect = useCallback(() => {
window.localStorage.removeItem(WALLET_KEY);
setAddress(null);
setNetwork(null);
}, []);
return (
<WalletContext.Provider
value={{ address, network, connecting, error, connect, disconnect }}
>
{children}
</WalletContext.Provider>
);
}
export function useWallet() {
const ctx = useContext(WalletContext);
if (!ctx) throw new Error("useWallet must be used within a WalletProvider");
return ctx;
}