Skip to content

Commit

Permalink
UX: Display peer connection number as a button badge
Browse files Browse the repository at this point in the history
- implements #51
- when API does not respond, display OFF state
- if redirect is enabled and in OFF state, background is red
  • Loading branch information
lidel committed Feb 4, 2016
1 parent c1dbb39 commit 2fc5480
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 46 deletions.
36 changes: 0 additions & 36 deletions lib/automatic-mode.js

This file was deleted.

9 changes: 9 additions & 0 deletions lib/gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ function buttonPanelHide () {
}

exports.toggleButton = button
exports.toggleButtonOn = ON_STATE
exports.toggleButtonOff = OFF_STATE

function setButtonState (trueFalse) {
let newState = trueFalse ? ON_STATE : OFF_STATE
Expand All @@ -79,6 +81,13 @@ function setButtonState (trueFalse) {
})
}

exports.setButtonBadge = (peerCount) => {
button.badge = peerCount !== null ? peerCount : OFF_STATE.badge
if (gw.redirectEnabled) {
button.badgeColor = (peerCount < 1) ? 'red' : ON_STATE.badgeColor
}
}

/*
* COMMON (PANEL & CONTEXT MENU)
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const parent = require('sdk/remote/parent')

exports.main = function (options, callbacks) { // eslint-disable-line no-unused-vars
require('./redirects.js')
require('./peer-watch.js')
protocols.register()
require('./automatic-mode.js')
// console.log('Addon loaded.')
parent.remoteRequire('./child-main.js', module)
// console.log('Addon loaded.')
}

exports.onUnload = function (reason) { // eslint-disable-line no-unused-vars
Expand Down
37 changes: 37 additions & 0 deletions lib/peer-watch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

const { setInterval, clearInterval } = require('sdk/timers')
const notifications = require('sdk/notifications')
const prefs = require('sdk/simple-prefs').prefs
const api = require('./api.js')
const gui = require('./gui.js')
const gw = require('./gateways.js')
const _ = require('sdk/l10n').get

const interval = 3000

function ipfsHealthCheck () {
api.getSwarmPeers((peers) => {
const peerCount = (peers && peers.Strings) ? peers.Strings.length : null
gui.setButtonBadge(peerCount)

if (prefs.automatic) {
let isUp = peerCount > 0
if (isUp !== gw.redirectEnabled) {
notifications.notify({
title: isUp ? _('automatic_up_notification_title') : _('automatic_down_notification_title'),
text: isUp ? _('automatic_up_notification_text') : _('automatic_down_notification_text')
})
gw.redirectEnabled = isUp
}
}
})
}

const checkRunner = setInterval(ipfsHealthCheck, interval)
require('sdk/system/unload').when(() => {
clearInterval(checkRunner)
})

exports.interval = interval
exports.triggerManualCheck = ipfsHealthCheck
2 changes: 1 addition & 1 deletion test/test-automatic-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { setTimeout } = require('sdk/timers')
const { prefs } = require('sdk/simple-prefs')
const tabs = require('sdk/tabs')
const gw = require('../lib/gateways.js')
const autoMode = require('../lib/automatic-mode.js')
const autoMode = require('../lib/peer-watch.js')

const ipfsPath = 'ipfs/QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D/'

Expand Down
16 changes: 9 additions & 7 deletions test/test-gui.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
var gui = require('../lib/gui.js')
var gw = require('../lib/gateways.js')
const gui = require('../lib/gui.js')
const gw = require('../lib/gateways.js')

var button = gui.toggleButton
var panel = gui.toggleButtonPanel
const button = gui.toggleButton
const buttonOn = gui.toggleButtonOn
const buttonOff = gui.toggleButtonOff
const panel = gui.toggleButtonPanel

// BUTTON BADGE
exports['test default toggleButton attributes'] = function (assert) {
assert.equal(button.id, 'ipfs-gateway-status', 'button id')
assert.equal(button.badge, gw.redirectEnabled ? 'ON' : 'OFF', 'default badge follows prefs.useCustomGateway')
assert.equal(button.icon['16'], gw.redirectEnabled ? buttonOn.icon['16'] : buttonOff.icon['16'], 'default badge follows prefs.useCustomGateway')
}
exports['test disabled toggleButton'] = function (assert) {
gw.redirectEnabled = false
assert.equal(button.badge, 'OFF', 'badge for prefs.useCustomGateway=false')
assert.equal(button.icon['16'], buttonOff.icon['16'], 'badge for prefs.useCustomGateway=false')
}
exports['test enabled toggleButton'] = function (assert) {
gw.redirectEnabled = true
assert.equal(button.badge, 'ON', 'badge for prefs.useCustomGateway=false')
assert.equal(button.icon['16'], buttonOn.icon['16'], 'badge for prefs.useCustomGateway=false')
}

// PANEL
Expand Down

0 comments on commit 2fc5480

Please sign in to comment.