Skip to content

Commit

Permalink
feat: ask when opening external protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Feb 2, 2022
1 parent 30ba843 commit 061e79d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
9 changes: 9 additions & 0 deletions assets/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,14 @@
"clearCustomIpfsBinarySuccess": {
"title": "Clear custom IPFS binary",
"message": "The custom IPFS binary was cleared. To start using the bundled IPFS version, IPFS needs to be restarted first."
},
"protocolHandlerDialog": {
"title": "Opening IPFS URIs",
"message": "How would you like IPFS Desktop to open IPFS URIs?",
"browserPublicGateway": "In my default browser using a public gateway",
"browserLocalGateway": "In my default browser using a local gateway",
"filesScreen": "In the Files screen",
"exploreScreen": "In the Explore screen",
"rememberThisChoice": "Remember this choice for all IPFS URIs"
}
}
36 changes: 21 additions & 15 deletions src/protocol-handlers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { app, shell } = require('electron')
const { app, shell, ipcMain } = require('electron')
const toUri = require('multiaddr-to-uri')
const i18n = require('i18next')
const createToggler = require('./utils/create-toggler')
const store = require('./common/store')
const { showPrompt } = require('./dialogs')

Expand All @@ -23,25 +24,25 @@ async function getAction () {
}

const { button, input } = await showPrompt({
title: 'Opening IPFS URIs',
message: 'How would you like IPFS Desktop to open IPFS URIs?',
title: i18n.t('protocolHandlerDialog.title'),
message: i18n.t('protocolHandlerDialog.message'),
inputs: [
{
type: 'radio',
name: 'action',
defaultValue: DEFAULT_ACTION,
labels: {
[ACTION_OPTIONS.BROWSER_PUBLIC_GATEWAY]: 'In my default browser using a public gateway',
[ACTION_OPTIONS.BROWSER_LOCAL_GATEWAY]: 'In my default browser using a local gateway',
[ACTION_OPTIONS.FILES_SCREEN]: 'In the Files screen',
[ACTION_OPTIONS.EXPLORE_SCREEN]: 'In the Explore screen'
[ACTION_OPTIONS.BROWSER_PUBLIC_GATEWAY]: i18n.t('protocolHandlerDialog.browserPublicGateway'),
[ACTION_OPTIONS.BROWSER_LOCAL_GATEWAY]: i18n.t('protocolHandlerDialog.browserLocalGateway'),
[ACTION_OPTIONS.FILES_SCREEN]: i18n.t('protocolHandlerDialog.filesScreen'),
[ACTION_OPTIONS.EXPLORE_SCREEN]: i18n.t('protocolHandlerDialog.exploreScreen')
}
},
{
type: 'checkbox',
name: 'remember',
defaultValue: 'checked',
label: 'Remember this choice for all IPFS URIs'
label: i18n.t('protocolHandlerDialog.rememberThisChoice')
}
],
buttons: [
Expand All @@ -63,6 +64,7 @@ async function getAction () {
if (input.remember === 'on') {
store.set(CONFIG_KEY, false)
store.set(CONFIG_KEY_ACTION, action)
ipcMain.emit('configUpdated')
}

return action
Expand Down Expand Up @@ -96,15 +98,16 @@ async function parseUrl (url, ctx) {
return false
}

const action = store.get(CONFIG_KEY_ACTION, DEFAULT_ACTION)
const action = await getAction()
let base = 'https://dweb.link'
let ipfsd

switch (action) {
case ACTION_OPTIONS.BROWSER_PUBLIC_GATEWAY:
openLink(protocol, part, base)
break
case ACTION_OPTIONS.BROWSER_LOCAL_GATEWAY:
const ipfsd = ctx.getIpfsd ? await ctx.getIpfsd(true) : null
ipfsd = ctx.getIpfsd ? await ctx.getIpfsd(true) : null

// Best effort. Defaults to public gateway if not available.
if (ipfsd && ipfsd.gatewayAddr) {
Expand All @@ -117,7 +120,12 @@ async function parseUrl (url, ctx) {
ctx.launchWebUI(`/${protocol}/${part}`, { focus: true })
break
case ACTION_OPTIONS.EXPLORE_SCREEN:
ctx.launchWebUI(`/explore/${protocol}/${part}`, { focus: true })
if (protocol === 'ipns') {
// IPNS is not supported on the explore page yet.
ctx.launchWebUI(`/${protocol}/${part}`, { focus: true })
} else {
ctx.launchWebUI(`/explore/${protocol}/${part}`, { focus: true })
}
break
default:
return false
Expand All @@ -144,12 +152,10 @@ module.exports = function (ctx) {
// tray option shows a 'tick'.
if (store.get(CONFIG_KEY, null) === null) {
store.set(CONFIG_KEY, true)
ipcMain.emit('configUpdated')
}

setTimeout(async () => {
const action = await getAction()
console.log(action)
}, 5000)
createToggler(CONFIG_KEY, () => true)

// Handle if the app started running now, and a link
// was sent to be handled.
Expand Down

0 comments on commit 061e79d

Please sign in to comment.