Skip to content

Commit 7077ddd

Browse files
committed
revert react package changes
1 parent c835f13 commit 7077ddd

File tree

3 files changed

+198
-20
lines changed

3 files changed

+198
-20
lines changed

packages/react/package.json

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/react",
3-
"version": "2.2.5-alpha.7",
3+
"version": "2.2.6-alpha.1",
44
"description": "A collection of React hooks for integrating Web3-Onboard in to React and Next.js projects. Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardised spec compliant web3 providers for all supported wallets, modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
55
"keywords": [
66
"Ethereum",
@@ -50,23 +50,20 @@
5050
"dist"
5151
],
5252
"scripts": {
53-
"build": "yarn clean && tsc",
54-
"clean": "rimraf dist",
55-
"prepublishOnly": "yarn build",
53+
"build": "tsc",
5654
"dev": "tsc -w",
5755
"type-check": "tsc --noEmit"
5856
},
5957
"license": "MIT",
6058
"devDependencies": {
59+
"@types/react": "^18.0.2",
6160
"@types/use-sync-external-store": "^0.0.3",
62-
"rimraf": "^3.0.2",
61+
"react": "^18.0.0",
6362
"typescript": "^4.5.5"
6463
},
6564
"dependencies": {
66-
"@types/react": "^18.0.15",
67-
"@web3-onboard/common": "^2.1.7-alpha.4",
68-
"@web3-onboard/core": "^2.6.0-alpha.7",
69-
"react": "^18.2.0",
65+
"@web3-onboard/core": "^2.7.0-alpha.1",
66+
"@web3-onboard/common": "^2.1.7",
7067
"use-sync-external-store": "1.0.0"
7168
},
7269
"peerDependencies": {

packages/react/src/index.ts

Lines changed: 192 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,193 @@
1-
export {
2-
useAccountCenter,
3-
useAppState,
4-
useConnectWallet,
5-
useNotifications,
6-
useSetChain,
7-
useSetLocale,
8-
useWallets
9-
} from './hooks'
10-
export { init, Web3OnboardProvider, Web3OnboardProviderProps } from './context'
1+
import { useState, useCallback } from 'react'
2+
import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js'
113

4+
import Web3Onboard from '@web3-onboard/core'
5+
import type {
6+
InitOptions,
7+
OnboardAPI,
8+
ConnectOptions,
9+
DisconnectOptions,
10+
WalletState,
11+
ConnectedChain,
12+
AccountCenter,
13+
AppState,
14+
CustomNotification,
15+
Notification,
16+
Notify,
17+
UpdateNotification,
18+
PreflightNotificationsOptions
19+
} from '@web3-onboard/core'
20+
import type { Chain, WalletInit } from '@web3-onboard/common'
21+
22+
export let web3Onboard: OnboardAPI | null = null
23+
24+
export const init = (options: InitOptions): OnboardAPI => {
25+
web3Onboard = Web3Onboard(options)
26+
return web3Onboard
27+
}
28+
29+
const HOOK_ERROR_MESSAGE = 'Must initialize before using hooks.'
30+
31+
const useAppState: {
32+
(): AppState
33+
<K extends keyof AppState>(stateKey?: K): AppState[K]
34+
} = (stateKey = undefined) => {
35+
if (!web3Onboard) throw new Error(HOOK_ERROR_MESSAGE)
36+
37+
const { select, get } = web3Onboard.state
38+
39+
const subscribe = useCallback(
40+
(onStoreChange: () => void) => {
41+
const { unsubscribe } = stateKey
42+
? select(stateKey).subscribe(onStoreChange)
43+
: select().subscribe(onStoreChange)
44+
45+
return () => unsubscribe
46+
},
47+
[stateKey]
48+
)
49+
50+
const getSnapshot = useCallback(() => {
51+
const snapshot = get()
52+
return stateKey ? snapshot[stateKey] : snapshot
53+
}, [stateKey])
54+
55+
const getServerSnapshot = () => get() || getSnapshot
56+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot)
57+
}
58+
59+
export const useConnectWallet = (): [
60+
{ wallet: WalletState | null; connecting: boolean },
61+
(options?: ConnectOptions) => Promise<void>,
62+
(wallet: DisconnectOptions) => Promise<void>,
63+
(addresses?: string[]) => Promise<void>,
64+
(wallets: WalletInit[]) => void,
65+
(wallet: WalletState, address?: string) => void
66+
] => {
67+
if (!web3Onboard) throw new Error(HOOK_ERROR_MESSAGE)
68+
69+
const { connectWallet, disconnectWallet } = web3Onboard
70+
71+
const wallets = useAppState('wallets')
72+
const wallet = wallets[0] || null
73+
74+
const [connecting, setConnecting] = useState<boolean>(false)
75+
76+
const connect = useCallback(async (options?: ConnectOptions) => {
77+
setConnecting(true)
78+
79+
await connectWallet(options)
80+
81+
setConnecting(false)
82+
}, [])
83+
84+
const disconnect = useCallback(async ({ label }: DisconnectOptions) => {
85+
setConnecting(true)
86+
87+
await disconnectWallet({ label })
88+
89+
setConnecting(false)
90+
}, [])
91+
92+
const updateBalances = web3Onboard.state.actions.updateBalances
93+
const setWalletModules = web3Onboard.state.actions.setWalletModules
94+
const setPrimaryWallet = web3Onboard.state.actions.setPrimaryWallet
95+
96+
return [
97+
{ wallet, connecting },
98+
connect,
99+
disconnect,
100+
updateBalances,
101+
setWalletModules,
102+
setPrimaryWallet
103+
]
104+
}
105+
106+
type SetChainOptions = {
107+
chainId: string
108+
chainNamespace?: string
109+
}
110+
111+
export const useSetChain = (
112+
walletLabel?: string
113+
): [
114+
{
115+
chains: Chain[]
116+
connectedChain: ConnectedChain | null
117+
settingChain: boolean
118+
},
119+
(options: SetChainOptions) => Promise<boolean>
120+
] => {
121+
if (!web3Onboard) throw new Error(HOOK_ERROR_MESSAGE)
122+
123+
const { setChain } = web3Onboard
124+
125+
const { wallets, chains } = useAppState()
126+
127+
const getChain = () => {
128+
const wallet = walletLabel
129+
? wallets.find(({ label }) => label === walletLabel)
130+
: wallets[0]
131+
return wallet && wallet.chains ? wallet.chains[0] : null
132+
}
133+
134+
const connectedChain = getChain()
135+
136+
const [settingChain, setInProgress] = useState<boolean>(false)
137+
138+
const set = useCallback(async (options: SetChainOptions) => {
139+
setInProgress(true)
140+
141+
const success = await setChain({ ...options, wallet: walletLabel })
142+
143+
setInProgress(false)
144+
145+
return success
146+
}, [])
147+
148+
return [{ chains, connectedChain, settingChain }, set]
149+
}
150+
151+
export const useWallets = (): WalletState[] => {
152+
if (!web3Onboard) throw new Error(HOOK_ERROR_MESSAGE)
153+
154+
return useAppState('wallets')
155+
}
156+
157+
export const useNotifications = (): [
158+
Notification[],
159+
(updatedNotification: CustomNotification) => {
160+
dismiss: () => void
161+
update: UpdateNotification
162+
},
163+
(update: Partial<Notify>) => void,
164+
(options: PreflightNotificationsOptions) => Promise<void | string>
165+
] => {
166+
if (!web3Onboard) throw new Error(HOOK_ERROR_MESSAGE)
167+
168+
const customNotification = web3Onboard.state.actions.customNotification
169+
const updateNotify = web3Onboard.state.actions.updateNotify
170+
const preflightNotifications =
171+
web3Onboard.state.actions.preflightNotifications
172+
173+
return [
174+
useAppState('notifications'),
175+
customNotification,
176+
updateNotify,
177+
preflightNotifications
178+
]
179+
}
180+
181+
export const useSetLocale = (): ((locale: string) => void) => {
182+
if (!web3Onboard) throw new Error(HOOK_ERROR_MESSAGE)
183+
184+
return web3Onboard.state.actions.setLocale
185+
}
186+
187+
export const useAccountCenter = (): ((
188+
update: AccountCenter | Partial<AccountCenter>
189+
) => void) => {
190+
if (!web3Onboard) throw new Error(HOOK_ERROR_MESSAGE)
191+
192+
return web3Onboard.state.actions.updateAccountCenter
193+
}

packages/react/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"include": ["src/**/*"],
44

55
"compilerOptions": {
6-
"jsx": "react",
76
"outDir": "dist",
87
"rootDir": "src",
98
"declarationDir": "dist",

0 commit comments

Comments
 (0)