Skip to content

Commit adcc3cc

Browse files
authored
[core-v2.3.1-alpha.4, react-v2.2.1-alpha.4, vue-v2.1.1-alpha.3] : Update - Decouple Notify and AC positioning (#1098)
* Update react package version * Setup and progress on decoupling - working with init functionality * working with cleanup * Update docs to handle new positioning and types * Update versions * fix validation * Update container height on mobile, validation DRY
1 parent 8f5d155 commit adcc3cc

File tree

14 files changed

+234
-88
lines changed

14 files changed

+234
-88
lines changed

packages/core/README.md

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ unsubscribe()
148148
```
149149

150150
```typescript
151+
151152
export type NotifyOptions = {
153+
desktop: Notify
154+
mobile: Notify
155+
}
156+
export type Notify = {
152157
enabled: boolean // default: true
153158
/**
154159
* Callback that receives all transaction events
@@ -159,8 +164,15 @@ export type NotifyOptions = {
159164
transactionHandler?: (
160165
event: EthereumTransactionData
161166
) => TransactionHandlerReturn
167+
position: CommonPositions
162168
}
163169

170+
export type CommonPositions =
171+
| 'topRight'
172+
| 'bottomRight'
173+
| 'bottomLeft'
174+
| 'topLeft'
175+
164176
export type TransactionHandlerReturn = CustomNotification | boolean | void
165177

166178
export type CustomNotification = Partial<Omit<Notification, 'id' | 'startTime'>>
@@ -286,15 +298,31 @@ const onboard = Onboard({
286298
},
287299
apiKey: 'xxx387fb-bxx1-4xxc-a0x3-9d37e426xxxx'
288300
notify: {
289-
enabled: true,
290-
transactionHandler: transaction => {
291-
console.log({ transaction })
292-
if (transaction.eventCode === 'txPool') {
293-
return {
294-
type: 'success',
295-
message: 'Your transaction from #1 DApp is in the mempool',
301+
desktop: {
302+
enabled: true,
303+
transactionHandler: transaction => {
304+
console.log({ transaction })
305+
if (transaction.eventCode === 'txPool') {
306+
return {
307+
type: 'success',
308+
message: 'Your transaction from #1 DApp is in the mempool',
309+
}
296310
}
297-
}
311+
},
312+
position: 'bottomLeft'
313+
},
314+
mobile: {
315+
enabled: true,
316+
transactionHandler: transaction => {
317+
console.log({ transaction })
318+
if (transaction.eventCode === 'txPool') {
319+
return {
320+
type: 'success',
321+
message: 'Your transaction from #1 DApp is in the mempool',
322+
}
323+
}
324+
},
325+
position: 'topRight'
298326
}
299327
},
300328
accountCenter: {
@@ -421,7 +449,7 @@ type AppState = {
421449
accountCenter: AccountCenter
422450
walletModules: WalletModule[]
423451
locale: Locale
424-
notify: NotifyOptions
452+
notify: Notify
425453
notifications: Notification[]
426454
}
427455

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/core",
3-
"version": "2.3.1-alpha.3",
3+
"version": "2.3.1-alpha.4",
44
"scripts": {
55
"build": "rollup -c",
66
"dev": "rollup -c -w",

packages/core/src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export const APP_INITIAL_STATE: AppState = {
1313
},
1414
notify: {
1515
enabled: true,
16-
transactionHandler: () => {}
16+
transactionHandler: () => {},
17+
position: 'topRight'
1718
},
1819
notifications: [],
1920
locale: ''

packages/core/src/index.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import disconnectWallet from './disconnect'
44
import setChain from './chain'
55
import { state } from './store'
66
import { reset$ } from './streams'
7-
import { validateInitOptions } from './validation'
7+
import { validateInitOptions, validateNotify, validateNotifyOptions } from './validation'
88
import initI18N from './i18n'
99
import App from './views/Index.svelte'
10-
import type { InitOptions, OnboardAPI } from './types'
10+
import type { InitOptions, OnboardAPI, Notify } from './types'
1111
import { APP_INITIAL_STATE } from './constants'
1212
import { configuration, updateConfiguration } from './configuration'
1313

@@ -22,7 +22,7 @@ import {
2222

2323
import updateBalances from './updateBalances'
2424

25-
const API = {
25+
const API: OnboardAPI = {
2626
connectWallet,
2727
disconnectWallet,
2828
setChain,
@@ -51,7 +51,7 @@ export type {
5151
AppState,
5252
CustomNotification,
5353
Notification,
54-
NotifyOptions,
54+
Notify,
5555
UpdateNotification
5656
} from './types'
5757

@@ -104,7 +104,50 @@ function init(options: InitOptions): OnboardAPI {
104104

105105
// update notify
106106
if (typeof notify !== undefined) {
107-
updateNotify(notify)
107+
if ('desktop' in notify || 'mobile' in notify) {
108+
const error = validateNotifyOptions(notify)
109+
110+
if (error) {
111+
throw error
112+
}
113+
114+
if (
115+
(!notify.desktop || (notify.desktop && !notify.desktop.position)) &&
116+
accountCenter &&
117+
accountCenter.desktop &&
118+
accountCenter.desktop.position
119+
) {
120+
notify.desktop.position = accountCenter.desktop.position
121+
}
122+
if (
123+
(!notify.mobile || (notify.mobile && !notify.mobile.position)) &&
124+
accountCenter &&
125+
accountCenter.mobile &&
126+
accountCenter.mobile.position
127+
) {
128+
notify.mobile.position = accountCenter.mobile.position
129+
}
130+
let notifyUpdate: Partial<Notify>
131+
if (device.type === 'mobile' && notify.mobile) {
132+
notifyUpdate = {
133+
...APP_INITIAL_STATE.notify,
134+
...notify.mobile
135+
}
136+
} else if (notify.desktop) {
137+
notifyUpdate = {
138+
...APP_INITIAL_STATE.notify,
139+
...notify.desktop
140+
}
141+
}
142+
updateNotify(notifyUpdate)
143+
} else {
144+
const error = validateNotify(notify as Notify)
145+
146+
if (error) {
147+
throw error
148+
}
149+
updateNotify(notify as Notify)
150+
}
108151
}
109152

110153
if (svelteInstance) {

packages/core/src/store/actions.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ import type {
1616
UpdateAccountCenterAction,
1717
UpdateWalletAction,
1818
WalletState,
19-
NotifyOptions,
2019
UpdateNotifyAction,
2120
Notification,
2221
AddNotificationAction,
2322
RemoveNotificationAction,
2423
UpdateAllWalletsAction,
2524
CustomNotification,
2625
UpdateNotification,
27-
CustomNotificationUpdate
26+
CustomNotificationUpdate,
27+
Notify
2828
} from '../types'
2929

3030
import {
@@ -33,11 +33,11 @@ import {
3333
validateNotification,
3434
validateCustomNotification,
3535
validateCustomNotificationUpdate,
36-
validateNotifyOptions,
3736
validateString,
3837
validateWallet,
3938
validateWalletInit,
40-
validateUpdateBalances
39+
validateUpdateBalances,
40+
validateNotify
4141
} from '../validation'
4242

4343
import {
@@ -156,8 +156,8 @@ export function updateAccountCenter(
156156
dispatch(action as UpdateAccountCenterAction)
157157
}
158158

159-
export function updateNotify(update: Partial<NotifyOptions>): void {
160-
const error = validateNotifyOptions(update)
159+
export function updateNotify(update: Partial<Notify>): void {
160+
const error = validateNotify(update)
161161

162162
if (error) {
163163
throw error

packages/core/src/types.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface InitOptions {
4646
/**
4747
* Transaction notification options
4848
*/
49-
notify?: Partial<NotifyOptions>
49+
notify?: Partial<NotifyOptions> | Partial<Notify>
5050
}
5151

5252
export interface OnboardAPI {
@@ -63,7 +63,7 @@ export interface OnboardAPI {
6363
interface ExposedActions {
6464
setWalletModules: (wallets: WalletInit[]) => void
6565
setLocale: (locale: string) => void
66-
updateNotify: (update: Partial<NotifyOptions>) => void
66+
updateNotify: (update: Partial<Notify>) => void
6767
customNotification: (
6868
updatedNotification: CustomNotification
6969
) => {
@@ -141,7 +141,7 @@ export interface AppState {
141141
wallets: WalletState[]
142142
accountCenter: AccountCenter
143143
locale: Locale
144-
notify: NotifyOptions
144+
notify: Notify
145145
notifications: Notification[]
146146
}
147147

@@ -156,11 +156,15 @@ export type Locale = string
156156
export type i18nOptions = Record<Locale, i18n>
157157
export type i18n = typeof en
158158

159-
export type AccountCenterPosition =
160-
| 'topRight'
161-
| 'bottomRight'
162-
| 'bottomLeft'
163-
| 'topLeft'
159+
export type CommonPositions =
160+
| 'topRight'
161+
| 'bottomRight'
162+
| 'bottomLeft'
163+
| 'topLeft'
164+
165+
export type AccountCenterPosition = CommonPositions
166+
167+
export type NotificationPosition = CommonPositions
164168

165169
export type AccountCenter = {
166170
enabled: boolean
@@ -174,7 +178,7 @@ export type AccountCenterOptions = {
174178
mobile: Omit<AccountCenter, 'expanded'>
175179
}
176180

177-
export type NotifyOptions = {
181+
export type Notify = {
178182
/**
179183
* Defines whether whether to subscribe to transaction events or not
180184
* default: true
@@ -189,6 +193,17 @@ export type NotifyOptions = {
189193
transactionHandler: (
190194
event: EthereumTransactionData
191195
) => TransactionHandlerReturn
196+
/**
197+
* Position of notifications that defaults to the same position as the
198+
* Account Center (if enabled) of the top right if AC is disabled
199+
* and notifications are enabled (enabled by default with API key)
200+
*/
201+
position?: NotificationPosition
202+
}
203+
204+
export type NotifyOptions = {
205+
desktop: Notify
206+
mobile: Notify
192207
}
193208

194209
export type Notification = {
@@ -279,7 +294,7 @@ export type SetLocaleAction = {
279294

280295
export type UpdateNotifyAction = {
281296
type: 'update_notify'
282-
payload: Partial<NotifyOptions>
297+
payload: Partial<Notify>
283298
}
284299

285300
export type AddNotificationAction = {

packages/core/src/validation.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import type {
1717
NotifyOptions,
1818
Notification,
1919
CustomNotification,
20-
CustomNotificationUpdate
20+
CustomNotificationUpdate,
21+
Notify
2122
} from './types'
2223

2324
const chainId = Joi.string().pattern(/^0x[0-9a-fA-F]+$/)
@@ -128,7 +129,7 @@ const walletInit = Joi.array().items(Joi.function()).required()
128129

129130
const locale = Joi.string()
130131

131-
const accountCenterPosition = Joi.string().valid(
132+
const commonPositions = Joi.string().valid(
132133
'topRight',
133134
'bottomRight',
134135
'bottomLeft',
@@ -137,7 +138,13 @@ const accountCenterPosition = Joi.string().valid(
137138

138139
const notify = Joi.object({
139140
transactionHandler: Joi.function(),
140-
enabled: Joi.boolean()
141+
enabled: Joi.boolean(),
142+
position: commonPositions
143+
})
144+
145+
const notifyOptions = Joi.object({
146+
desktop: notify,
147+
mobile: notify
141148
})
142149

143150
const initOptions = Joi.object({
@@ -150,15 +157,15 @@ const initOptions = Joi.object({
150157
desktop: Joi.object({
151158
enabled: Joi.boolean(),
152159
minimal: Joi.boolean(),
153-
position: accountCenterPosition
160+
position: commonPositions
154161
}),
155162
mobile: Joi.object({
156163
enabled: Joi.boolean(),
157164
minimal: Joi.boolean(),
158-
position: accountCenterPosition
165+
position: commonPositions
159166
})
160167
}),
161-
notify
168+
notify: [notifyOptions, notify]
162169
})
163170

164171
const connectOptions = Joi.object({
@@ -183,7 +190,7 @@ const setChainOptions = Joi.object({
183190

184191
const accountCenter = Joi.object({
185192
enabled: Joi.boolean(),
186-
position: accountCenterPosition,
193+
position: commonPositions,
187194
expanded: Joi.boolean(),
188195
minimal: Joi.boolean()
189196
})
@@ -287,10 +294,14 @@ export function validateLocale(data: string): ValidateReturn {
287294
return validate(locale, data)
288295
}
289296

297+
export function validateNotify(data: Partial<Notify>): ValidateReturn {
298+
return validate(notify, data)
299+
}
300+
290301
export function validateNotifyOptions(
291302
data: Partial<NotifyOptions>
292303
): ValidateReturn {
293-
return validate(notify, data)
304+
return validate(notifyOptions, data)
294305
}
295306

296307
export function validateTransactionHandlerReturn(

0 commit comments

Comments
 (0)