Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ unsubscribe()
```

```typescript

export type NotifyOptions = {
desktop: Notify
mobile: Notify
}
export type Notify = {
enabled: boolean // default: true
/**
* Callback that receives all transaction events
Expand All @@ -158,8 +163,15 @@ export type NotifyOptions = {
transactionHandler?: (
event: EthereumTransactionData
) => TransactionHandlerReturn
position: CommonPositions
}

export type CommonPositions =
| 'topRight'
| 'bottomRight'
| 'bottomLeft'
| 'topLeft'

export type TransactionHandlerReturn = CustomNotification | boolean | void

export type CustomNotification = Partial<Omit<Notification, 'id' | 'startTime'>>
Expand Down Expand Up @@ -285,15 +297,31 @@ const onboard = Onboard({
},
apiKey: 'xxx387fb-bxx1-4xxc-a0x3-9d37e426xxxx'
notify: {
enabled: true,
transactionHandler: transaction => {
console.log({ transaction })
if (transaction.eventCode === 'txPool') {
return {
type: 'success',
message: 'Your transaction from #1 DApp is in the mempool',
desktop: {
enabled: true,
transactionHandler: transaction => {
console.log({ transaction })
if (transaction.eventCode === 'txPool') {
return {
type: 'success',
message: 'Your transaction from #1 DApp is in the mempool',
}
}
}
},
position: 'bottomLeft'
},
mobile: {
enabled: true,
transactionHandler: transaction => {
console.log({ transaction })
if (transaction.eventCode === 'txPool') {
return {
type: 'success',
message: 'Your transaction from #1 DApp is in the mempool',
}
}
},
position: 'topRight'
}
},
accountCenter: {
Expand Down Expand Up @@ -420,7 +448,7 @@ type AppState = {
accountCenter: AccountCenter
walletModules: WalletModule[]
locale: Locale
notify: NotifyOptions
notify: Notify
notifications: Notification[]
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@web3-onboard/core",
"version": "2.3.1-alpha.3",
"version": "2.3.1-alpha.4",
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const APP_INITIAL_STATE: AppState = {
},
notify: {
enabled: true,
transactionHandler: () => {}
transactionHandler: () => {},
position: 'topRight'
},
notifications: [],
locale: ''
Expand Down
53 changes: 48 additions & 5 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import disconnectWallet from './disconnect'
import setChain from './chain'
import { state } from './store'
import { reset$ } from './streams'
import { validateInitOptions } from './validation'
import { validateInitOptions, validateNotify, validateNotifyOptions } from './validation'
import initI18N from './i18n'
import App from './views/Index.svelte'
import type { InitOptions, OnboardAPI } from './types'
import type { InitOptions, OnboardAPI, Notify } from './types'
import { APP_INITIAL_STATE } from './constants'
import { configuration, updateConfiguration } from './configuration'

Expand All @@ -22,7 +22,7 @@ import {

import updateBalances from './updateBalances'

const API = {
const API: OnboardAPI = {
connectWallet,
disconnectWallet,
setChain,
Expand Down Expand Up @@ -51,7 +51,7 @@ export type {
AppState,
CustomNotification,
Notification,
NotifyOptions,
Notify,
UpdateNotification
} from './types'

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

// update notify
if (typeof notify !== undefined) {
updateNotify(notify)
if ('desktop' in notify || 'mobile' in notify) {
const error = validateNotifyOptions(notify)

if (error) {
throw error
}

if (
(!notify.desktop || (notify.desktop && !notify.desktop.position)) &&
accountCenter &&
accountCenter.desktop &&
accountCenter.desktop.position
) {
notify.desktop.position = accountCenter.desktop.position
}
if (
(!notify.mobile || (notify.mobile && !notify.mobile.position)) &&
accountCenter &&
accountCenter.mobile &&
accountCenter.mobile.position
) {
notify.mobile.position = accountCenter.mobile.position
}
let notifyUpdate: Partial<Notify>
if (device.type === 'mobile' && notify.mobile) {
notifyUpdate = {
...APP_INITIAL_STATE.notify,
...notify.mobile
}
} else if (notify.desktop) {
notifyUpdate = {
...APP_INITIAL_STATE.notify,
...notify.desktop
}
}
updateNotify(notifyUpdate)
} else {
const error = validateNotify(notify as Notify)

if (error) {
throw error
}
updateNotify(notify as Notify)
}
}

if (svelteInstance) {
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import type {
UpdateAccountCenterAction,
UpdateWalletAction,
WalletState,
NotifyOptions,
UpdateNotifyAction,
Notification,
AddNotificationAction,
RemoveNotificationAction,
UpdateAllWalletsAction,
CustomNotification,
UpdateNotification,
CustomNotificationUpdate
CustomNotificationUpdate,
Notify
} from '../types'

import {
Expand All @@ -33,11 +33,11 @@ import {
validateNotification,
validateCustomNotification,
validateCustomNotificationUpdate,
validateNotifyOptions,
validateString,
validateWallet,
validateWalletInit,
validateUpdateBalances
validateUpdateBalances,
validateNotify
} from '../validation'

import {
Expand Down Expand Up @@ -156,8 +156,8 @@ export function updateAccountCenter(
dispatch(action as UpdateAccountCenterAction)
}

export function updateNotify(update: Partial<NotifyOptions>): void {
const error = validateNotifyOptions(update)
export function updateNotify(update: Partial<Notify>): void {
const error = validateNotify(update)

if (error) {
throw error
Expand Down
35 changes: 25 additions & 10 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface InitOptions {
/**
* Transaction notification options
*/
notify?: Partial<NotifyOptions>
notify?: Partial<NotifyOptions> | Partial<Notify>
}

export interface OnboardAPI {
Expand All @@ -63,7 +63,7 @@ export interface OnboardAPI {
interface ExposedActions {
setWalletModules: (wallets: WalletInit[]) => void
setLocale: (locale: string) => void
updateNotify: (update: Partial<NotifyOptions>) => void
updateNotify: (update: Partial<Notify>) => void
customNotification: (
updatedNotification: CustomNotification
) => {
Expand Down Expand Up @@ -141,7 +141,7 @@ export interface AppState {
wallets: WalletState[]
accountCenter: AccountCenter
locale: Locale
notify: NotifyOptions
notify: Notify
notifications: Notification[]
}

Expand All @@ -156,11 +156,15 @@ export type Locale = string
export type i18nOptions = Record<Locale, i18n>
export type i18n = typeof en

export type AccountCenterPosition =
| 'topRight'
| 'bottomRight'
| 'bottomLeft'
| 'topLeft'
export type CommonPositions =
| 'topRight'
| 'bottomRight'
| 'bottomLeft'
| 'topLeft'

export type AccountCenterPosition = CommonPositions

export type NotificationPosition = CommonPositions

export type AccountCenter = {
enabled: boolean
Expand All @@ -174,7 +178,7 @@ export type AccountCenterOptions = {
mobile: Omit<AccountCenter, 'expanded'>
}

export type NotifyOptions = {
export type Notify = {
/**
* Defines whether whether to subscribe to transaction events or not
* default: true
Expand All @@ -189,6 +193,17 @@ export type NotifyOptions = {
transactionHandler: (
event: EthereumTransactionData
) => TransactionHandlerReturn
/**
* Position of notifications that defaults to the same position as the
* Account Center (if enabled) of the top right if AC is disabled
* and notifications are enabled (enabled by default with API key)
*/
position?: NotificationPosition
}

export type NotifyOptions = {
desktop: Notify
mobile: Notify
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Adamj1232 Could we possible discuss naming conventions here? Notify vs NotifyOptions
why are they separate? and then possibly leave the Notify namespace open in the event that we decouple notify from w3o and need to use the Notify type? Just a thought

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@taylorjdawson I see what you are saying, I was following the pattern we have used for AccountCenter where ...Options are passed in on init and Notify in this case is used internally for Notify configurations. If we were to decouple I dont see a challenge and updating the types but def want to have similar patterns with the different components

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay no worries, is there a way to make the name more descriptive? When I saw the Notify type I thought it excepted a notify instance.

}

export type Notification = {
Expand Down Expand Up @@ -279,7 +294,7 @@ export type SetLocaleAction = {

export type UpdateNotifyAction = {
type: 'update_notify'
payload: Partial<NotifyOptions>
payload: Partial<Notify>
}

export type AddNotificationAction = {
Expand Down
27 changes: 19 additions & 8 deletions packages/core/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import type {
NotifyOptions,
Notification,
CustomNotification,
CustomNotificationUpdate
CustomNotificationUpdate,
Notify
} from './types'

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

const locale = Joi.string()

const accountCenterPosition = Joi.string().valid(
const commonPositions = Joi.string().valid(
'topRight',
'bottomRight',
'bottomLeft',
Expand All @@ -137,7 +138,13 @@ const accountCenterPosition = Joi.string().valid(

const notify = Joi.object({
transactionHandler: Joi.function(),
enabled: Joi.boolean()
enabled: Joi.boolean(),
position: commonPositions
})

const notifyOptions = Joi.object({
desktop: notify,
mobile: notify
})

const initOptions = Joi.object({
Expand All @@ -150,15 +157,15 @@ const initOptions = Joi.object({
desktop: Joi.object({
enabled: Joi.boolean(),
minimal: Joi.boolean(),
position: accountCenterPosition
position: commonPositions
}),
mobile: Joi.object({
enabled: Joi.boolean(),
minimal: Joi.boolean(),
position: accountCenterPosition
position: commonPositions
})
}),
notify
notify: [notifyOptions, notify]
})

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

const accountCenter = Joi.object({
enabled: Joi.boolean(),
position: accountCenterPosition,
position: commonPositions,
expanded: Joi.boolean(),
minimal: Joi.boolean()
})
Expand Down Expand Up @@ -287,10 +294,14 @@ export function validateLocale(data: string): ValidateReturn {
return validate(locale, data)
}

export function validateNotify(data: Partial<Notify>): ValidateReturn {
return validate(notify, data)
}

export function validateNotifyOptions(
data: Partial<NotifyOptions>
): ValidateReturn {
return validate(notify, data)
return validate(notifyOptions, data)
}

export function validateTransactionHandlerReturn(
Expand Down
Loading