Skip to content

Commit bc1f03d

Browse files
committed
feat: Show discovered peer count
1 parent 36ff4e4 commit bc1f03d

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

assets/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"ipfsIsStarting": "IPFS is Starting",
55
"ipfsIsNotRunning": "IPFS is Not Running",
66
"ipfsHasErrored": "IPFS has Errored",
7+
"peerCount": "Peers",
78
"runningWithGC": "Running (GC in progress)",
89
"runningWhileCheckingForUpdate": "Running (Checking for Updates)",
910
"start": "Start",

src/tray.js

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function buildCheckbox (key, label) {
4646
// they natively work as soon as the menu opens. They don't work like that on Windows
4747
// or other OSes and must be registered globally. They still collide with global
4848
// accelerator. Please see ../utils/setup-global-shortcut.js for more info.
49-
function buildMenu (ctx) {
49+
function buildMenu (ctx, peerCount) {
5050
return Menu.buildFromTemplate([
5151
...[
5252
['ipfsIsStarting', 'yellow'],
@@ -63,6 +63,11 @@ function buildMenu (ctx) {
6363
enabled: false,
6464
icon: path.resolve(path.join(__dirname, `../assets/icons/status/${color}.png`))
6565
})),
66+
{
67+
id: 'peerCount',
68+
label: peerCount.toString() + ' ' + i18n.t('peerCount'),
69+
enabled: false
70+
},
6671
{
6772
id: 'restartIpfs',
6873
label: i18n.t('restart'),
@@ -253,7 +258,8 @@ module.exports = function (ctx) {
253258
const state = {
254259
status: null,
255260
gcRunning: false,
256-
isUpdating: false
261+
isUpdating: false,
262+
peerCount: 0
257263
}
258264

259265
// macOS tray drop files
@@ -271,15 +277,30 @@ module.exports = function (ctx) {
271277
})
272278
}
273279

280+
const pollPeers = () => {
281+
// If the daemon is running, send a request to retrieve the number
282+
// of connected peers. Emit 'peersPolled' event upon retrieval.
283+
if (state.status === STATUS.STARTING_FINISHED && ctx.getIpfsd) {
284+
ctx.getIpfsd().then((daemon) => {
285+
daemon.api.swarm.peers().then((value) => {
286+
if (value.length) {
287+
ipcMain.emit('peersPolled', value.length)
288+
}
289+
})
290+
})
291+
} else {
292+
ipcMain.emit('peersPolled', 0)
293+
}
294+
}
295+
274296
const setupMenu = () => {
275-
menu = buildMenu(ctx)
297+
menu = buildMenu(ctx, state.peerCount)
276298

277299
tray.setContextMenu(menu)
278-
tray.setToolTip('IPFS Desktop')
300+
tray.setToolTip(state.peerCount.toString() + ' ' + i18n.t('peerCount'))
279301

280302
menu.on('menu-will-show', () => { ipcMain.emit('menubar-will-open') })
281303
menu.on('menu-will-close', () => { ipcMain.emit('menubar-will-close') })
282-
283304
updateMenu()
284305
}
285306

@@ -292,6 +313,7 @@ module.exports = function (ctx) {
292313
menu.getMenuItemById('ipfsIsStopping').visible = status === STATUS.STOPPING_STARTED && !gcRunning && !isUpdating
293314
menu.getMenuItemById('ipfsIsNotRunning').visible = status === STATUS.STOPPING_FINISHED && !gcRunning && !isUpdating
294315
menu.getMenuItemById('ipfsHasErrored').visible = errored && !gcRunning && !isUpdating
316+
menu.getMenuItemById('peerCount').visible = status === STATUS.STARTING_FINISHED
295317
menu.getMenuItemById('runningWithGC').visible = gcRunning
296318
menu.getMenuItemById('runningWhileCheckingForUpdate').visible = isUpdating
297319

@@ -365,10 +387,25 @@ module.exports = function (ctx) {
365387
updateMenu()
366388
})
367389

390+
ipcMain.on('peersPolled', peerCount => {
391+
// When a new peer count is retrieved, rebuild the menu and update
392+
// the tray tooltip with the new number if necessary.
393+
if (peerCount !== state.peerCount) {
394+
state.peerCount = peerCount
395+
menu = buildMenu(ctx, state.peerCount)
396+
menu.on('menu-will-show', () => { ipcMain.emit('menubar-will-open') })
397+
menu.on('menu-will-close', () => { ipcMain.emit('menubar-will-close') })
398+
tray.setContextMenu(menu)
399+
tray.setToolTip(state.peerCount.toString() + ' ' + i18n.t('peerCount'))
400+
updateMenu()
401+
}
402+
})
403+
368404
ipcMain.on('configUpdated', () => { updateMenu() })
369405
ipcMain.on('languageUpdated', () => { setupMenu() })
370406

371407
setupMenu()
408+
setInterval(pollPeers, 60000)
372409

373410
ctx.tray = tray
374411
logger.info('[tray] started')

0 commit comments

Comments
 (0)