From 18ea1d5921246d7ae8a91fcb347dd67736f8d8f0 Mon Sep 17 00:00:00 2001 From: Adam Carpenter Date: Tue, 2 Aug 2022 17:10:00 -0600 Subject: [PATCH 1/7] Add configuration to hide/show sidebar --- packages/core/src/constants.ts | 5 +++- packages/core/src/index.ts | 10 ++++++-- packages/core/src/store/actions.ts | 27 +++++++++++++++++--- packages/core/src/store/constants.ts | 1 + packages/core/src/store/index.ts | 16 +++++++++++- packages/core/src/types.ts | 15 +++++++++++ packages/core/src/validation.ts | 16 +++++++++++- packages/core/src/views/connect/Index.svelte | 6 ++--- packages/demo/src/App.svelte | 3 +++ 9 files changed, 88 insertions(+), 11 deletions(-) diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts index b591d5e3a..c5427dbd1 100644 --- a/packages/core/src/constants.ts +++ b/packages/core/src/constants.ts @@ -18,7 +18,10 @@ export const APP_INITIAL_STATE: AppState = { position: 'topRight' }, notifications: [], - locale: '' + locale: '', + connect : { + showSidebar: true + } } export const STORAGE_KEYS = { diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 46cd0d349..822711831 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -26,7 +26,8 @@ import { customNotification, setLocale, setPrimaryWallet, - setWalletModules + setWalletModules, + updateConnectModal } from './store/actions' const API = { @@ -87,13 +88,18 @@ function init(options: InitOptions): OnboardAPI { accountCenter, apiKey, notify, - gas + gas, + connect } = options initI18N(i18n) addChains(chainIdToHex(chains)) const { device, svelteInstance } = configuration + if (typeof connect !== undefined) { + updateConnectModal(connect) + } + // update accountCenter if (typeof accountCenter !== 'undefined') { let accountCenterUpdate diff --git a/packages/core/src/store/actions.ts b/packages/core/src/store/actions.ts index 91f5c8f07..b664685f0 100644 --- a/packages/core/src/store/actions.ts +++ b/packages/core/src/store/actions.ts @@ -24,7 +24,9 @@ import type { CustomNotification, UpdateNotification, CustomNotificationUpdate, - Notify + Notify, + ConnectModalOptions, + UpdateConnectModalAction } from '../types' import { @@ -37,7 +39,8 @@ import { validateWallet, validateWalletInit, validateUpdateBalances, - validateNotify + validateNotify, + validateConnectModalUpdate } from '../validation' import { @@ -53,7 +56,8 @@ import { SET_LOCALE, ADD_NOTIFICATION, REMOVE_NOTIFICATION, - UPDATE_ALL_WALLETS + UPDATE_ALL_WALLETS, + UPDATE_CONNECT_MODAL } from './constants' export function addChains(chains: Chain[]): void { @@ -180,6 +184,23 @@ export function updateAccountCenter( dispatch(action as UpdateAccountCenterAction) } +export function updateConnectModal( + update: ConnectModalOptions | Partial +): void { + const error = validateConnectModalUpdate(update) + + if (error) { + throw error + } + + const action = { + type: UPDATE_CONNECT_MODAL, + payload: update + } + + dispatch(action as UpdateConnectModalAction) +} + export function updateNotify(update: Partial): void { const error = validateNotify(update) diff --git a/packages/core/src/store/constants.ts b/packages/core/src/store/constants.ts index ddf8e02fd..80da33a55 100644 --- a/packages/core/src/store/constants.ts +++ b/packages/core/src/store/constants.ts @@ -5,6 +5,7 @@ export const UPDATE_WALLET = 'update_wallet' export const REMOVE_WALLET = 'remove_wallet' export const UPDATE_ACCOUNT = 'update_account' export const UPDATE_ACCOUNT_CENTER = 'update_account_center' +export const UPDATE_CONNECT_MODAL = 'update_connect_modal' export const SET_WALLET_MODULES = 'set_wallet_modules' export const SET_LOCALE = 'set_locale' export const UPDATE_NOTIFY = 'update_notify' diff --git a/packages/core/src/store/index.ts b/packages/core/src/store/index.ts index bafe3c580..66c98aaef 100644 --- a/packages/core/src/store/index.ts +++ b/packages/core/src/store/index.ts @@ -17,7 +17,8 @@ import type { UpdateNotifyAction, AddNotificationAction, RemoveNotificationAction, - UpdateAllWalletsAction + UpdateAllWalletsAction, + UpdateConnectModalAction } from '../types' import { @@ -27,6 +28,7 @@ import { REMOVE_WALLET, RESET_STORE, UPDATE_ACCOUNT, + UPDATE_CONNECT_MODAL, UPDATE_ACCOUNT_CENTER, UPDATE_NOTIFY, SET_WALLET_MODULES, @@ -118,6 +120,18 @@ function reducer(state: AppState, action: Action): AppState { } } + case UPDATE_CONNECT_MODAL: { + const update = payload as UpdateConnectModalAction['payload'] + + return { + ...state, + connect: { + ...state.connect, + ...update + } + } + } + case UPDATE_ACCOUNT_CENTER: { const update = payload as UpdateAccountCenterAction['payload'] diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index fd61471b7..e10413151 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -33,6 +33,10 @@ export interface InitOptions { * Define custom copy for the 'en' locale or add locales to i18n your app */ i18n?: i18nOptions + /** + * Customize the connect modal + */ + connect?: ConnectModalOptions /** * Customize the account center UI */ @@ -117,6 +121,7 @@ export interface AppState { locale: Locale notify: Notify notifications: Notification[] + connect: ConnectModalOptions } export type Configuration = { @@ -132,6 +137,10 @@ export type Locale = string export type i18nOptions = Record export type i18n = typeof en +export type ConnectModalOptions = { + showSidebar?: boolean +} + export type CommonPositions = | 'topRight' | 'bottomRight' @@ -245,6 +254,7 @@ export type Action = | AddNotificationAction | RemoveNotificationAction | UpdateAllWalletsAction + | UpdateConnectModalAction export type AddChainsAction = { type: 'add_chains'; payload: Chain[] } export type AddWalletAction = { type: 'add_wallet'; payload: WalletState } @@ -274,6 +284,11 @@ export type UpdateAccountCenterAction = { payload: AccountCenter | Partial } +export type UpdateConnectModalAction = { + type: 'update_connect_modal' + payload: ConnectModalOptions | Partial +} + export type SetWalletModulesAction = { type: 'set_wallet_modules' payload: WalletModule[] diff --git a/packages/core/src/validation.ts b/packages/core/src/validation.ts index 8b0097acf..3fca78ca4 100644 --- a/packages/core/src/validation.ts +++ b/packages/core/src/validation.ts @@ -20,7 +20,8 @@ import type { CustomNotification, CustomNotificationUpdate, Notify, - PreflightNotificationsOptions + PreflightNotificationsOptions, + ConnectModalOptions } from './types' // const chainId = Joi.string().pattern(/^0x[0-9a-fA-F]+$/) @@ -184,6 +185,9 @@ const initOptions = Joi.object({ gas: Joi.object({ get: Joi.function().required(), stream: Joi.function().required() + }), + connect: Joi.object({ + showSidebar: Joi.boolean() }) }) @@ -199,6 +203,10 @@ const connectOptions = Joi.object({ .required() }) +const connectModalOptions = Joi.object({ + showSidebar: Joi.boolean() +}) + const disconnectOptions = Joi.object({ label: Joi.string().required() }).required() @@ -318,6 +326,12 @@ export function validateAccountCenterUpdate( return validate(accountCenter, data) } +export function validateConnectModalUpdate( + data: ConnectModalOptions | Partial +): ValidateReturn { + return validate(connectModalOptions, data) +} + export function validateWalletInit(data: WalletInit[]): ValidateReturn { return validate(walletInit, data) } diff --git a/packages/core/src/views/connect/Index.svelte b/packages/core/src/views/connect/Index.svelte index 4328664bf..a3aae7110 100644 --- a/packages/core/src/views/connect/Index.svelte +++ b/packages/core/src/views/connect/Index.svelte @@ -1,6 +1,6 @@