-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathConnection.tsx
136 lines (128 loc) · 3.8 KB
/
Connection.tsx
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
import { Button, Flex, Loader, Text, useMatches } from "@mantine/core";
import { useLocalStorage } from "@mantine/hooks";
import type { ClientOptions, XmtpEnv } from "@xmtp/browser-sdk";
import { useEffect, useRef } from "react";
import { hexToUint8Array, uint8ArrayToHex } from "uint8array-extras";
import { type Hex } from "viem";
import { generatePrivateKey } from "viem/accounts";
import { useConnect, useDisconnect, useWalletClient } from "wagmi";
import { injected } from "wagmi/connectors";
import { Settings } from "@/components/Settings/Settings";
import { useRefManager } from "@/contexts/RefManager";
import { createEphemeralSigner, createSigner } from "@/helpers/createSigner";
import { useClient } from "@/hooks/useClient";
import { IconLogout } from "@/icons/IconLogout";
import { IconUser } from "@/icons/IconUser";
export const Disconnect: React.FC = () => {
const { disconnect } = useDisconnect();
const { disconnect: disconnectClient } = useClient();
const label: React.ReactNode = useMatches({
base: <IconLogout size={24} />,
sm: "Disconnect",
});
const px = useMatches({
base: "xs",
sm: "md",
});
const handleDisconnect = () => {
disconnect(undefined, {
onSuccess: () => {
disconnectClient();
},
});
};
return (
<Button onClick={handleDisconnect} px={px}>
{label}
</Button>
);
};
export const Connect: React.FC = () => {
const { setRef } = useRefManager();
const ref = useRef<HTMLButtonElement>(null);
const { initialize, initializing } = useClient();
const [encryptionKey] = useLocalStorage({
key: "XMTP_ENCRYPTION_KEY",
defaultValue: uint8ArrayToHex(crypto.getRandomValues(new Uint8Array(32))),
});
const [env] = useLocalStorage<XmtpEnv>({
key: "XMTP_NETWORK",
defaultValue: "dev",
});
const [useEphemeralAccount] = useLocalStorage({
key: "XMTP_USE_EPHEMERAL_ACCOUNT",
defaultValue: false,
});
const [ephemeralAccountKey, setEphemeralAccountKey] =
useLocalStorage<Hex | null>({
key: "XMTP_EPHEMERAL_ACCOUNT_KEY",
defaultValue: null,
});
const [loggingLevel] = useLocalStorage<ClientOptions["loggingLevel"]>({
key: "XMTP_LOGGING_LEVEL",
defaultValue: "off",
});
const { connect } = useConnect();
const { data } = useWalletClient();
const label: React.ReactNode = useMatches({
base: <IconUser size={24} />,
sm: "Connect",
});
const px = useMatches({
base: "xs",
sm: "md",
});
useEffect(() => {
setRef("connect-wallet-button", ref);
}, []);
useEffect(() => {
if (data?.account) {
void initialize({
encryptionKey: hexToUint8Array(encryptionKey),
env,
loggingLevel,
signer: createSigner(data.account.address, data),
});
}
}, [data, env]);
const handleConnect = () => {
const connectEphemeralAccount = async () => {
const key = ephemeralAccountKey || generatePrivateKey();
if (!ephemeralAccountKey) {
setEphemeralAccountKey(key);
}
const signer = createEphemeralSigner(key);
await initialize({
encryptionKey: hexToUint8Array(encryptionKey),
env,
loggingLevel,
signer,
});
};
if (!useEphemeralAccount) {
connect({ connector: injected() });
} else {
void connectEphemeralAccount();
}
};
return initializing ? (
<Flex align="center" gap="xs">
<Loader color="var(--mantine-primary-color-filled)" size="sm" />
<Text size="sm">Connecting...</Text>
</Flex>
) : (
<Button onClick={handleConnect} px={px} ref={ref}>
{label}
</Button>
);
};
export const Connection: React.FC = () => {
const { client } = useClient();
return (
<Flex align="center" gap="xs" ml="auto">
{!client && <Connect />}
{client && <Disconnect />}
<Settings />
</Flex>
);
};