Skip to content

Commit

Permalink
fix: menu items requiring both api and ipfs context
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Oct 1, 2018
1 parent 7964f9b commit 1bcaa9d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
24 changes: 16 additions & 8 deletions add-on/src/lib/context-menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,31 @@ function createContextMenus (getState, runtime, ipfsPathValidator, { onAddFromCo
throw err
}

// enabled only when ipfsContext is shown when API is up
const apiAndIpfsContextItems = new Set([...apiMenuItems].filter(i => ipfsContextItems.has(i)))
// state to avoid async tab lookups
let ipfsContext = false

return {
async update (changedTabId) {
try {
const canUpload = getState().peerCount > 0
for (let item of apiMenuItems) {
await browser.contextMenus.update(item, { enabled: canUpload })
}
if (changedTabId) {
// recalculate tab-dependant menu items
const currentTab = await browser.tabs.query({ active: true, currentWindow: true }).then(tabs => tabs[0])
if (currentTab && currentTab.id === changedTabId) {
const ipfsContext = ipfsPathValidator.isIpfsPageActionsContext(currentTab.url)
for (let item of ipfsContextItems) {
browser.contextMenus.update(item, { enabled: ipfsContext })
}
ipfsContext = ipfsPathValidator.isIpfsPageActionsContext(currentTab.url)
}
}
const ifApi = getState().peerCount > 0
for (let item of apiMenuItems) {
await browser.contextMenus.update(item, { enabled: ifApi })
}
for (let item of ipfsContextItems) {
await browser.contextMenus.update(item, { enabled: ipfsContext })
}
for (let item of apiAndIpfsContextItems) {
await browser.contextMenus.update(item, { enabled: (ifApi && ipfsContext) })
}
} catch (err) {
console.log('[ipfs-companion] Error updating context menus', err)
}
Expand Down
12 changes: 11 additions & 1 deletion add-on/src/lib/copier.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,17 @@ function createCopier (getState, getIpfs, notify) {
notify('notify_copiedTitle', directCid)
} catch (error) {
console.error('Unable to resolve/copy direct CID:', error.message)
if (notify) notify('notify_addonIssueTitle', 'notify_inlineErrorMsg', error.message)
if (notify) {
const errMsg = error.toString()
if (errMsg.startsWith('Error: no link')) {
// Sharding support is limited:
// - https://github.com/ipfs/js-ipfs/issues/1279
// - https://github.com/ipfs/go-ipfs/issues/5270
notify('notify_addonIssueTitle', 'Unable to resolve CID within HAMT-sharded directory, sorry! Will be fixed soon.')
} else {
notify('notify_addonIssueTitle', 'notify_inlineErrorMsg', error.message)
}
}
}
},

Expand Down
12 changes: 12 additions & 0 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ module.exports = async function init () {
browser.webNavigation.onCommitted.addListener(onNavigationCommitted)
browser.tabs.onUpdated.addListener(onUpdatedTab)
browser.tabs.onActivated.addListener(onActivatedTab)
if (browser.windows) {
browser.windows.onFocusChanged.addListener(onWindowFocusChanged)
}
browser.runtime.onMessage.addListener(onRuntimeMessage)
browser.runtime.onConnect.addListener(onRuntimeConnect)

Expand Down Expand Up @@ -342,6 +345,15 @@ module.exports = async function init () {
// Page-specific Actions
// -------------------------------------------------------------------

async function onWindowFocusChanged (windowId) {
// Note: On some Linux window managers, WINDOW_ID_NONE will always be sent
// immediately preceding a switch from one browser window to another.
if (windowId !== browser.windows.WINDOW_ID_NONE) {
const currentTab = await browser.tabs.query({ active: true, windowId }).then(tabs => tabs[0])
await contextMenus.update(currentTab.id)
}
}

async function onActivatedTab (activeInfo) {
await contextMenus.update(activeInfo.tabId)
}
Expand Down

0 comments on commit 1bcaa9d

Please sign in to comment.