Skip to content
This repository was archived by the owner on Aug 9, 2021. It is now read-only.

Commit e714c2e

Browse files
committed
feat: optional browser feat detect alert
1 parent 2e8c86c commit e714c2e

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

package-lock.json

Lines changed: 5 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,10 @@
9797
"web3modal": "^1.6.3",
9898
"webpack": "^4.32.2",
9999
"webpack-cli": "^3.3.2"
100+
},
101+
"standard": {
102+
"ignore": [
103+
"/src/modernizr.js"
104+
]
100105
}
101106
}

src/3box.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ const ADDRESS_SERVER_URL = config.address_server_url
2323
const IPFS_OPTIONS = config.ipfs_options
2424
const RENDEZVOUS_ADDRESS = config.rendezvous_address
2525
const IFRAME_STORE_URL = 'https://connect.3box.io/v1/index.html'
26+
const supportAlert = 'This site uses local data storage to give you control of your data. Please enable web APIs like localstorage, indexxeddb, etc in your browser settings.'
2627

2728
let globalIPFS, globalIPFSPromise, threeIdConnect
2829

2930
const browserHuh = typeof window !== 'undefined' && typeof document !== 'undefined'
31+
if (browserHuh) require('./modernizr.js')
3032
if (browserHuh) threeIdConnect = new ThreeIdConnect(IFRAME_STORE_URL)
3133
/**
3234
* @extends BoxApi
@@ -122,16 +124,35 @@ class Box extends BoxApi {
122124
* @param {Object} opts.ipfs A js-ipfs ipfs object
123125
* @param {String} opts.addressServer URL of the Address Server
124126
* @param {String} opts.ghostPinbot MultiAddress of a Ghost Pinbot node
127+
* @param {String} opts.supportCheck Gives browser alert if 3boxjs/ipfs not supported in browser env, defaults to true. You can also set to false to implement your own alert and call Box.support to check if supported.
125128
* @return {Box} the 3Box session instance
126129
*/
127130
static async create (provider, opts = {}) {
131+
if (opts.supportCheck !== false && browserHuh) await this._supportAlert()
128132
const ipfs = await Box.getIPFS(opts)
129133
const box = new Box(provider, ipfs, opts)
130134
await box._setProvider(provider)
131135
await box._init(opts)
132136
return box
133137
}
134138

139+
static async _supportAlert () {
140+
const supported = await this.supported()
141+
if (!supported) window.alert(supportAlert)
142+
}
143+
144+
/**
145+
* Determines if this browser environment supports 3boxjs and ipfs.
146+
*
147+
* @return {Boolean}
148+
*/
149+
static async supported () {
150+
return new Promise((resolve, reject) => {
151+
if (!browserHuh) throw new Error('Supported only detects browser feature support')
152+
window.Modernizr.on('indexeddb', resolve)
153+
})
154+
}
155+
135156
static get3idConnectProvider () {
136157
// TODO Could add blogpost link
137158
throw new Error('3idConnectProvider now consumes an eth provider, initialize 3box as you did before 3idconnectProvider, and 3box will create a 3idConnectProvider by default')

src/modernizr.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)