1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-01-10 12:24:20 +01:00

bugfixing and better limited mode

This commit is contained in:
Enrico Fasoli 2015-12-20 21:10:48 +01:00
parent 6f71134301
commit 6ab84ba064
3 changed files with 67 additions and 57 deletions

View File

@ -748,13 +748,22 @@ BoardsAPI.prototype.isNode = function () {
// Initialize API // Initialize API
BoardsAPI.prototype.init = function (done) { BoardsAPI.prototype.init = function (done) {
if (this.isInit) return if (this.isInit) return
this.ipfs.version((err, res) => {
if (err) {
this.limited = this.isRunningFromGateway() ? 2 : false
this.ee.emit('error', err)
this.ee.emit('init', err, this.limited)
console.log('Error while getting ipfs version:', err)
if (done && done.apply) done(err, this.limited)
} else {
this.ipfs_version = res.Version.split('-')[0]
console.log('IPFS Version is', res.Version)
if (semver.satisfies(this.ipfs_version, '~0.4.0')) {
console.log('IPFS version is supported')
this.ipfs.id((err, res) => { this.ipfs.id((err, res) => {
if (err) { if (err) {
console.log('Error while getting OWN ID:', err) console.log('Error while getting OWN ID:', err)
if (this.isRunningFromGateway()) { this.limited = this.isRunningFromGateway() ? 1 : false
console.log('Running in limited mode')
this.limited = true
} else console.log('Limited mode not available')
this.ee.emit('error', err) this.ee.emit('error', err)
this.ee.emit('init', err, this.limited) this.ee.emit('init', err, this.limited)
if (done && done.apply) { if (done && done.apply) {
@ -769,27 +778,21 @@ BoardsAPI.prototype.init = function (done) {
if (err2) { if (err2) {
this.ee.emit('error', err2) this.ee.emit('error', err2)
console.log('Error while calculating version hash:', err2) console.log('Error while calculating version hash:', err2)
this.ee.emit('init', err2) this.ee.emit('init', err2, this.limited)
if (done && done.apply) done(err2) if (done && done.apply) done(err2)
} else { } else {
if (r && r.Hash) this.version_hash = r.Hash if (r && r.Hash) this.version_hash = r.Hash
if (r && r[0] && r[0].Hash) this.version_hash = r[0].Hash if (r && r[0] && r[0].Hash) this.version_hash = r[0].Hash
console.log('Version hash is', this.version_hash) console.log('Version hash is', this.version_hash)
this.ipfs.version((err, res) => { // DONE!
if (err) {
this.ee.emit('error', err)
this.ee.emit('init', err)
console.log('Error while getting ipfs version:', err)
if (done && done.apply) done(err)
} else {
this.ipfs_version = res.Version.split('-')[0]
console.log('IPFS Version is', res.Version)
if (semver.satisfies(this.ipfs_version, '~0.4.0')) {
console.log('IPFS version is supported')
this.ee.emit('init', undefined) this.ee.emit('init', undefined)
this.isInit = true this.isInit = true
delete this.init_error delete this.init_error
if (done && done.apply) done(null) if (done && done.apply) done(null)
}
})
}
})
} else { } else {
var e = { Message: 'IPFS Version not supported. This app supports go-ipfs 0.4.x' } var e = { Message: 'IPFS Version not supported. This app supports go-ipfs 0.4.x' }
if (done && done.apply) done(e) if (done && done.apply) done(e)
@ -799,10 +802,6 @@ BoardsAPI.prototype.init = function (done) {
} }
} }
}) })
}
})
}
})
} }
BoardsAPI.prototype.getEventEmitter = function () { BoardsAPI.prototype.getEventEmitter = function () {

View File

@ -40,21 +40,20 @@ module.exports = React.createClass({
console.log('Connection to go-ipfs has timed out (probably due to CORS)') console.log('Connection to go-ipfs has timed out (probably due to CORS)')
if (this.isMounted() && !this.state.connected && !this.state.limited) { if (this.isMounted() && !this.state.connected && !this.state.limited) {
this.setState({ long: true }) this.setState({ long: true })
this.init(this.props)
} }
}, 5000) }, 5000)
}, },
getContent () { getContent () {
if (this.state.limited) { if (this.state.limited) {
return <div> return <div>
<h1><Icon name="ban"/> You're running in limited mode</h1> <h1><Icon name="exclamation-triangle" className="light" /> You're running in limited mode</h1>
<h4 className="light">Sorry, but at the moment an external application is needed to fully take advantage of the app</h4> <h4 className="light">Sorry, but at the moment an external application is needed to fully take advantage of the app</h4>
<p>Only a few features are available in limited mode.</p> <p>Only a few features are available in limited mode.</p>
<h5>Why am I running in limited mode?</h5> <h5>Why am I running in limited mode?</h5>
</div> </div>
} else { } else {
return <div> return <div>
<h1><Icon name="ban"/> Connection to IPFS not available</h1> <h1><Icon name="ban" className="light" /> Connection to IPFS not available</h1>
<h4 className="light">Sorry, but at the moment an external application is needed to try the Prototype</h4> <h4 className="light">Sorry, but at the moment an external application is needed to try the Prototype</h4>
<p><b>Tip:</b> you can also run in limited mode by loading the app from an IPFS Gateway.</p> <p><b>Tip:</b> you can also run in limited mode by loading the app from an IPFS Gateway.</p>
</div> </div>

View File

@ -7,14 +7,6 @@ module.exports = function (boardsAPI) {
return { addr: 'localhost', port: 5001, api: false } return { addr: 'localhost', port: 5001, api: false }
}, },
getInitialState: function () { getInitialState: function () {
boardsAPI.use(boards => {
if (boards.isInit && this.isMounted()) this.setState({ api: true })
boards.getEventEmitter().on('init', (err, limited) => {
if ((!err || limited) && this.isMounted()) {
this.setState({ api: true, limited })
}
})
})
var s = window.localStorage.getItem('ipfs-boards-settings') var s = window.localStorage.getItem('ipfs-boards-settings')
var obj = this.getDefaults() var obj = this.getDefaults()
try { try {
@ -24,6 +16,20 @@ module.exports = function (boardsAPI) {
} }
return obj || this.getDefaults() return obj || this.getDefaults()
}, },
componentDidMount () {
boardsAPI.use(boards => {
if (boards.isInit || boards.limited) {
this.setState({ api: true, limited: boards.limited })
} else {
this.setState({ error: boards.init_error || 'neither limited mode nor the API is available.' })
}
boards.getEventEmitter().on('init', (err, limited) => {
if (this.isMounted()) {
this.setState({ error: err, api: (!err || limited), limited })
}
})
})
},
save: function () { save: function () {
if (isNaN(this.state.port) || parseInt(this.state.port, 10) > 65535 || parseInt(this.state.port, 10) < 1) { if (isNaN(this.state.port) || parseInt(this.state.port, 10) > 65535 || parseInt(this.state.port, 10) < 1) {
window.alert('Port number invalid') window.alert('Port number invalid')
@ -46,7 +52,13 @@ module.exports = function (boardsAPI) {
} }
}, },
isOK: function () { isOK: function () {
if (this.state.limited) { if (this.state.error) {
console.log('Error', this.state.error)
return <div className="itsok light">
<h5><Icon name="ban" /> Error</h5>
<p>{this.state.error}</p>
</div>
} else if (this.state.limited) {
return <div className="itsok light"> return <div className="itsok light">
<h5><Icon name="exclamation-triangle" /> Limited Mode</h5> <h5><Icon name="exclamation-triangle" /> Limited Mode</h5>
<p>Some features may not be available.</p> <p>Some features may not be available.</p>