Skip to content

Commit e9aa94d

Browse files
authored
Enhancement: Optional dappId (#337)
* Make dappId optional in Initialization interface * Make dappId optional * Add branding component * Render branding in all modals * Add branding logic and types Closes #334
1 parent 42579bb commit e9aa94d

File tree

7 files changed

+180
-63
lines changed

7 files changed

+180
-63
lines changed

src/components/Modal.svelte

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script lang="ts">
22
import { fade } from 'svelte/transition'
33
import { app } from '../stores'
4+
import Branding from '../elements/Branding.svelte'
45
export let closeModal: () => void
56
export let closeable: boolean = true
67
@@ -78,6 +79,10 @@
7879
.bn-onboard-dark-mode-close-background:hover {
7980
background: #00222c;
8081
}
82+
83+
.no-padding-branding {
84+
padding-bottom: 0;
85+
}
8186
</style>
8287

8388
<aside
@@ -86,9 +91,13 @@
8691
on:click={closeModal}>
8792
<section
8893
class:bn-onboard-dark-mode={$app.darkMode}
94+
class:no-padding-branding={$app.displayBranding}
8995
class="bn-onboard-custom bn-onboard-modal-content"
9096
on:click={e => e.stopPropagation()}>
9197
<slot />
98+
{#if $app.displayBranding}
99+
<Branding darkMode={$app.darkMode} />
100+
{/if}
92101
{#if closeable}
93102
<div
94103
class="bn-onboard-custom bn-onboard-modal-content-close"

src/elements/Branding.svelte

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<script>
2+
export let darkMode
3+
</script>
4+
5+
<style>
6+
div {
7+
font-size: 0.66rem;
8+
height: 1.3rem;
9+
margin: 0.4rem;
10+
display: flex;
11+
}
12+
13+
a {
14+
display: flex;
15+
justify-content: center;
16+
width: 100%;
17+
opacity: 0.8;
18+
color: inherit;
19+
}
20+
21+
img {
22+
height: 100%;
23+
width: auto;
24+
}
25+
</style>
26+
27+
<div>
28+
<a
29+
href="https://blocknative.com"
30+
class="bn-onboard-clickable"
31+
target="_blank"
32+
rel="noopener noreferrer">
33+
<img
34+
alt="Blocknative Logo"
35+
src={darkMode ? ' https://www.blocknative.com/hubfs/Infrastructure%20Rebrand/bn-structure-logo-white.svg' : 'https://www.blocknative.com/hubfs/Infrastructure%20Rebrand/bn-structure-logo-black.svg'} />
36+
</a>
37+
</div>

src/interfaces.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
export interface Initialization {
2-
dappId: string
2+
dappId?: string
33
networkId: number
44
subscriptions?: Subscriptions
55
walletSelect?: WalletSelectModuleOptions
66
walletCheck?: Array<WalletCheckModule | WalletCheckInit>
77
darkMode?: boolean
88
apiUrl?: string
9+
hideBranding?: boolean
910
}
1011

1112
export interface Subscriptions {
@@ -93,7 +94,7 @@ export interface StateAndHelpers extends UserState {
9394
stateStore: {
9495
address: WalletStateSliceStore
9596
network: WalletStateSliceStore
96-
balance: BalanceStore
97+
balance: BalanceStore | WalletStateSliceStore
9798
}
9899
}
99100

@@ -286,6 +287,10 @@ export interface WritableStore {
286287
subscribe: (subscriber: (store: any) => any) => () => void
287288
}
288289

290+
export interface ReadableStore {
291+
subscribe: (subscriber: (store: any) => any) => () => void
292+
}
293+
289294
export interface WalletInterfaceStore {
290295
subscribe: (subscriber: (store: any) => void) => () => void
291296
update: (
@@ -325,6 +330,7 @@ export interface AppState {
325330
accountSelectInProgress: boolean
326331
walletSelectDisplayedUI: boolean
327332
walletCheckDisplayedUI: boolean
333+
displayBranding: boolean
328334
}
329335

330336
export interface CancelablePromise extends Promise<any> {

src/onboard.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
wallet,
1313
state,
1414
walletInterface,
15-
resetWalletState
15+
resetWalletState,
16+
initializeStores
1617
} from './stores'
1718

1819
import { getDeviceInfo } from './utilities'
@@ -42,9 +43,14 @@ function init(initialization: Initialization): API {
4243

4344
validateInit(initialization)
4445

45-
const { subscriptions, dappId, networkId, darkMode, apiUrl } = initialization
46-
47-
initializeBlocknative(dappId, networkId, apiUrl)
46+
const {
47+
subscriptions,
48+
dappId,
49+
networkId,
50+
darkMode,
51+
apiUrl,
52+
hideBranding
53+
} = initialization
4854

4955
const { os, isMobile } = getDeviceInfo()
5056

@@ -54,6 +60,22 @@ function init(initialization: Initialization): API {
5460
initialization.walletCheck
5561
)
5662

63+
let displayBranding: boolean
64+
65+
if (dappId) {
66+
if (hideBranding !== false) {
67+
displayBranding = false
68+
} else {
69+
displayBranding = true
70+
}
71+
} else {
72+
if (hideBranding !== true) {
73+
displayBranding = true
74+
} else {
75+
displayBranding = false
76+
}
77+
}
78+
5779
app.update((store: AppState) => ({
5880
...store,
5981
dappId,
@@ -62,9 +84,16 @@ function init(initialization: Initialization): API {
6284
mobileDevice: isMobile,
6385
os,
6486
darkMode,
87+
displayBranding,
6588
checkModules: initializedModules.walletCheck
6689
}))
6790

91+
initializeStores()
92+
93+
if (dappId) {
94+
initializeBlocknative(dappId, networkId, apiUrl)
95+
}
96+
6897
onboard = new Onboard({
6998
target: document.body,
7099
props: {

src/stores.ts

Lines changed: 76 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { wait, makeCancelable, createInterval } from './utilities'
44
import { validateWalletInterface, validateType } from './validation'
55
import {
66
WritableStore,
7+
ReadableStore,
78
WalletInterfaceStore,
89
WalletInterface,
910
WalletStateSliceStore,
@@ -28,7 +29,8 @@ export const app: WritableStore = writable({
2829
autoSelectWallet: '',
2930
checkModules: [],
3031
walletSelectDisplayedUI: false,
31-
walletCheckDisplayedUI: false
32+
walletCheckDisplayedUI: false,
33+
displayBranding: false
3234
})
3335

3436
export const stateSyncStatus: {
@@ -47,37 +49,54 @@ export const stateSyncStatus: {
4749
network: null
4850
}
4951

50-
export const address: WalletStateSliceStore = createWalletStateSliceStore({
51-
parameter: 'address',
52-
initialState: null
53-
})
54-
export const network: WalletStateSliceStore = createWalletStateSliceStore({
55-
parameter: 'network',
56-
initialState: null
57-
})
58-
export const balance: BalanceStore = createBalanceStore(null)
59-
export const wallet: WritableStore = writable({
60-
name: null,
61-
provider: null,
62-
connect: null,
63-
instance: null,
64-
dashboard: null,
65-
type: null
66-
})
52+
export let address: WalletStateSliceStore
53+
export let network: WalletStateSliceStore
54+
export let balance: BalanceStore | WalletStateSliceStore
55+
export let wallet: WritableStore
56+
export let state: ReadableStore
6757

68-
export const state = derived(
69-
[address, network, balance, wallet, app],
70-
([$address, $network, $balance, $wallet, $app]) => {
71-
return {
72-
address: $address,
73-
network: $network,
74-
balance: $balance,
75-
wallet: $wallet,
76-
mobileDevice: $app.mobileDevice,
77-
appNetworkId: $app.networkId
58+
export function initializeStores() {
59+
address = createWalletStateSliceStore({
60+
parameter: 'address',
61+
initialState: null
62+
})
63+
64+
network = createWalletStateSliceStore({
65+
parameter: 'network',
66+
initialState: null
67+
})
68+
69+
balance = get(app).dappId
70+
? createBalanceStore(null)
71+
: createWalletStateSliceStore({
72+
parameter: 'balance',
73+
initialState: null,
74+
intervalSetting: 1000
75+
})
76+
77+
wallet = writable({
78+
name: null,
79+
provider: null,
80+
connect: null,
81+
instance: null,
82+
dashboard: null,
83+
type: null
84+
})
85+
86+
state = derived(
87+
[address, network, balance, wallet, app],
88+
([$address, $network, $balance, $wallet, $app]) => {
89+
return {
90+
address: $address,
91+
network: $network,
92+
balance: $balance,
93+
wallet: $wallet,
94+
mobileDevice: $app.mobileDevice,
95+
appNetworkId: $app.networkId
96+
}
7897
}
79-
}
80-
)
98+
)
99+
}
81100

82101
// keep track of intervals that are syncing state so they can be cleared
83102
let currentSyncerIntervals: ({ clear: () => void } | undefined)[] = []
@@ -87,29 +106,32 @@ export const walletInterface: WalletInterfaceStore = createWalletInterfaceStore(
87106
)
88107

89108
walletInterface.subscribe((walletInterface: WalletInterface | null) => {
90-
// clear all current intervals if they exist
91-
currentSyncerIntervals.forEach(
92-
(interval: { clear: () => void } | undefined) =>
93-
interval && interval.clear()
94-
)
95-
96-
const currentState = get(state)
97-
98-
// reset state
99-
currentState.balance && balance.reset()
100-
currentState.address && address.reset()
101-
currentState.network && network.reset()
109+
// make sure that stores have been initialized
110+
if (state) {
111+
// clear all current intervals if they exist
112+
currentSyncerIntervals.forEach(
113+
(interval: { clear: () => void } | undefined) =>
114+
interval && interval.clear()
115+
)
116+
117+
const currentState = get(state)
118+
119+
// reset state
120+
currentState.balance && balance.reset()
121+
currentState.address && address.reset()
122+
currentState.network && network.reset()
123+
124+
if (walletInterface) {
125+
// start syncing state and save intervals
126+
currentSyncerIntervals = [
127+
address.setStateSyncer(walletInterface.address),
128+
network.setStateSyncer(walletInterface.network),
129+
balance.setStateSyncer(walletInterface.balance)
130+
]
131+
}
102132

103-
if (walletInterface) {
104-
// start syncing state and save intervals
105-
currentSyncerIntervals = [
106-
address.setStateSyncer(walletInterface.address),
107-
network.setStateSyncer(walletInterface.network),
108-
balance.setStateSyncer(walletInterface.balance)
109-
]
133+
resetCheckModules()
110134
}
111-
112-
resetCheckModules()
113135
})
114136

115137
export function resetWalletState(options?: {
@@ -198,8 +220,9 @@ function createWalletInterfaceStore(
198220
function createWalletStateSliceStore(options: {
199221
parameter: string
200222
initialState: string | number | null | undefined
223+
intervalSetting?: number
201224
}): WalletStateSliceStore {
202-
const { parameter, initialState } = options
225+
const { parameter, initialState, intervalSetting } = options
203226
const { subscribe, set } = writable(initialState)
204227

205228
let currentState: string | number | null | undefined
@@ -259,7 +282,7 @@ function createWalletStateSliceStore(options: {
259282
)
260283
stateSyncStatus[parameter] = null
261284
})
262-
}, 200)
285+
}, intervalSetting || 200)
263286

264287
return interval
265288
}

src/validation.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export function validateInit(init: Initialization): never | void {
6363
walletCheck,
6464
darkMode,
6565
apiUrl,
66+
hideBranding,
6667
...otherParams
6768
} = init
6869

@@ -75,12 +76,18 @@ export function validateInit(init: Initialization): never | void {
7576
'walletSelect',
7677
'walletCheck',
7778
'darkMode',
78-
'apiUrl'
79+
'apiUrl',
80+
'hideBranding'
7981
],
8082
'init'
8183
)
8284

83-
validateType({ name: 'dappId', value: dappId, type: 'string' })
85+
validateType({
86+
name: 'dappId',
87+
value: dappId,
88+
type: 'string',
89+
optional: true
90+
})
8491
validateType({ name: 'networkId', value: networkId, type: 'number' })
8592
validateType({
8693
name: 'darkMode',
@@ -94,6 +101,12 @@ export function validateInit(init: Initialization): never | void {
94101
type: 'string',
95102
optional: true
96103
})
104+
validateType({
105+
name: 'hideBranding',
106+
value: hideBranding,
107+
type: 'boolean',
108+
optional: true
109+
})
97110

98111
validateType({
99112
name: 'subscriptions',

0 commit comments

Comments
 (0)