Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(un): decouple webui [META] [THOUGHT] #1413

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion assets/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"viewOnGitHub": "View on GitHub",
"status": "Status",
"files": "Files",
"settings": "Settings",
"quit": "Quit",
"versions": "Versions",
"screenshotTaken": "Screenshot taken",
Expand Down Expand Up @@ -116,5 +115,14 @@
"itemsFailedNotification": {
"title": "Failed to add items",
"message": "Could not add your items to your node."
},
"settings": {
"settings": "Settings",
"preferences": "Preferences",
"launchOnStartup": "Launch at Login",
"ipfsCommandLineTools": "Command Line Tools",
"takeScreenshotShortcut": "Take Screenshot Shortcut",
"downloadHashShortcut": "Download Hash Shortcut",
"npmOnIpfs": "NPM on IPFS Experiment"
}
}
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@
"lint": "standard",
"test": "cross-env NODE_ENV=test mocha test/unit/**/*.spec.js",
"test:e2e": "xvfb-maybe cross-env NODE_ENV=test mocha test/e2e/**/*.e2e.js --exit",
"postinstall": "run-s install-app-deps build:webui",
"postinstall": "run-s install-app-deps",
"install-app-deps": "electron-builder install-app-deps",
"clean:webui": "shx rm -rf assets/webui/",
"build": "run-s clean:webui build:*",
"build:webui": "run-s build:webui:*",
"build:webui:download": "npx ipfs-or-gateway -c Qmexhq2sBHnXQbvyP2GfUdbnY7HCagH2Mw5vUNSBn2nxip -p assets/webui/ -t 360000 --verbose",
"build:webui:minimize": "shx rm -rf assets/webui/static/js/*.map && shx rm -rf assets/webui/static/css/*.map",
"build:binaries": "electron-builder --publish onTag"
"build": "electron-builder --publish onTag"
},
"pre-commit": [
"lint"
Expand Down
4 changes: 3 additions & 1 deletion src/auto-launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,7 @@ module.exports = async function (ctx) {
}

activate(store.get(CONFIG_KEY, false))
createToggler(ctx, CONFIG_KEY, activate)
createToggler(CONFIG_KEY, activate)
}

module.exports.CONFIG_KEY = CONFIG_KEY
19 changes: 6 additions & 13 deletions src/create-toggler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,21 @@ const { ipcMain } = require('electron')
const store = require('./common/store')
const logger = require('./common/logger')

module.exports = function ({ webui }, settingsOption, activate) {
ipcMain.on('config.toggle', async (_, opt) => {
if (opt !== settingsOption) {
return
}

module.exports = function (settingsOption, activate) {
ipcMain.on(`toggle_${settingsOption}`, async () => {
const oldValue = store.get(settingsOption, null)
const newValue = !oldValue
let success = false

// TODO: refactor: tell the user if didn't work or not available.
// Receive prompt() to ask user if they're sure they want to enable for some.

if (await activate(newValue, oldValue)) {
store.set(settingsOption, newValue)
success = true

const action = newValue ? 'enabled' : 'disabled'
logger.info(`[${settingsOption}] ${action}`)
}

webui.webContents.send('config.changed', {
config: store.store,
changed: settingsOption,
success
})
ipcMain.emit('configUpdated')
})
}
1 change: 1 addition & 0 deletions src/download-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,4 @@ module.exports = function (ctx) {

module.exports.downloadHash = downloadHash
module.exports.SHORTCUT = SHORTCUT
module.exports.CONFIG_KEY = CONFIG_KEY
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async function run () {
await setupAppMenu(ctx)

await setupAutoUpdater(ctx) // ctx.checkForUpdates
await setupWebUI(ctx) // ctx.webui, launchWebUI
await setupWebUI(ctx) // launchWebUI
await setupTray(ctx) // ctx.tray
await setupDaemon(ctx) // ctx.getIpfsd, startIpfs, stopIpfs, restartIpfs

Expand Down
4 changes: 3 additions & 1 deletion src/ipfs-on-path/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { recoverableErrorDialog } = require('../dialogs')
const CONFIG_KEY = 'ipfsOnPath'

module.exports = async function (ctx) {
createToggler(ctx, CONFIG_KEY, async (value, oldValue) => {
createToggler(CONFIG_KEY, async (value, oldValue) => {
if (value === oldValue || (oldValue === null && !value)) return
if (value === true) return run('install')
return run('uninstall')
Expand All @@ -20,6 +20,8 @@ module.exports = async function (ctx) {
firstTime()
}

module.exports.CONFIG_KEY = CONFIG_KEY

async function firstTime () {
// Check if we've done this before.
if (store.get(CONFIG_KEY, null) !== null) {
Expand Down
4 changes: 3 additions & 1 deletion src/npm-on-ipfs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const CONFIG_KEY = 'experiments.npmOnIpfs'
module.exports = function (ctx) {
let interval = null

createToggler(ctx, CONFIG_KEY, async (value, oldValue) => {
createToggler(CONFIG_KEY, async (value, oldValue) => {
if (value === oldValue || oldValue === null) return true

// If the user is telling to (un)install even though they have (un)installed
Expand Down Expand Up @@ -43,6 +43,8 @@ module.exports = function (ctx) {
}
}

module.exports.CONFIG_KEY = CONFIG_KEY

function isPkgInstalled () {
return !!which.sync('ipfs-npm', { nothrow: true })
}
Expand Down
6 changes: 1 addition & 5 deletions src/second-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ module.exports = async function (ctx) {
return
}

if (await filesHandler(argv, ctx)) {
return
}

ctx.launchWebUI()
await filesHandler(argv, ctx)
})
}
2 changes: 1 addition & 1 deletion src/setup-global-shortcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function (ctx, { settingsOption, accelerator, action }) {
}

activate(store.get(settingsOption, false))
createToggler(ctx, settingsOption, activate)
createToggler(settingsOption, activate)

if (!IS_MAC) {
return
Expand Down
8 changes: 5 additions & 3 deletions src/take-screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ function handleScreenshot (ctx) {
}

function takeScreenshot (ctx) {
const { webui } = ctx
logger.info('[screenshot] taking screenshot')
webui.webContents.send('screenshot')
// TODO: fix
// const { webui } = ctx
// logger.info('[screenshot] taking screenshot')
// webui.webContents.send('screenshot')
}

module.exports = function (ctx) {
Expand All @@ -115,3 +116,4 @@ module.exports = function (ctx) {

module.exports.takeScreenshot = takeScreenshot
module.exports.SHORTCUT = SHORTCUT
module.exports.CONFIG_KEY = CONFIG_KEY
48 changes: 42 additions & 6 deletions src/tray.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
const { Menu, Tray, shell, app, ipcMain } = require('electron')
const i18n = require('i18next')
const path = require('path')
const { SHORTCUT: SCREENSHOT_SHORTCUT, takeScreenshot } = require('./take-screenshot')
const { SHORTCUT: HASH_SHORTCUT, downloadHash } = require('./download-hash')
const addToIpfs = require('./add-to-ipfs')
const { STATUS } = require('./daemon')
const logger = require('./common/logger')
const store = require('./common/store')
const { IS_MAC, IS_WIN, VERSION, GO_IPFS_VERSION } = require('./common/consts')
const moveRepositoryLocation = require('./move-repository-location')

const { SHORTCUT: SCREENSHOT_SHORTCUT, CONFIG_KEY: SCREENSHOT_KEY, takeScreenshot } = require('./take-screenshot')
const { SHORTCUT: HASH_SHORTCUT, CONFIG_KEY: HASH_KEY, downloadHash } = require('./download-hash')
const { CONFIG_KEY: AUTO_LAUNCH_KEY } = require('./auto-launch')
const { CONFIG_KEY: IPFS_PATH_KEY } = require('./ipfs-on-path')
const { CONFIG_KEY: NPM_IPFS_KEY } = require('./npm-on-ipfs')

const CONFIG_KEYS = [
AUTO_LAUNCH_KEY,
IPFS_PATH_KEY,
NPM_IPFS_KEY,
SCREENSHOT_KEY,
HASH_KEY
]

function buildCheckbox (key, label) {
return {
id: key,
label: i18n.t(label),
click: () => { ipcMain.emit(`toggle_${key}`) },
type: 'checkbox',
checked: false
}
}

// Notes on this: we are only supporting accelerators on macOS for now because
// they natively work as soon as the menu opens. They don't work like that on Windows
// or other OSes and must be registered globally. They still collide with global
Expand Down Expand Up @@ -57,10 +79,6 @@ function buildMenu (ctx) {
label: i18n.t('files'),
click: () => { ctx.launchWebUI('/files') }
},
{
label: i18n.t('settings'),
click: () => { ctx.launchWebUI('/settings') }
},
{ type: 'separator' },
{
id: 'takeScreenshot',
Expand All @@ -77,6 +95,16 @@ function buildMenu (ctx) {
enabled: false
},
{ type: 'separator' },
{
label: IS_MAC ? i18n.t('settings.preferences') : i18n.t('settings.settings'),
submenu: [
buildCheckbox(AUTO_LAUNCH_KEY, 'settings.launchOnStartup'),
buildCheckbox(IPFS_PATH_KEY, 'settings.ipfsCommandLineTools'),
buildCheckbox(SCREENSHOT_KEY, 'settings.takeScreenshotShortcut'),
buildCheckbox(HASH_KEY, 'settings.downloadHashShortcut'),
buildCheckbox(NPM_IPFS_KEY, 'settings.npmOnIpfs')
]
},
{
label: i18n.t('advanced'),
submenu: [
Expand Down Expand Up @@ -175,6 +203,7 @@ module.exports = function (ctx) {
menu.on('menu-will-close', () => { ipcMain.emit('menubar-will-close') })

updateStatus(status)
updateConfig()
}

const updateStatus = data => {
Expand Down Expand Up @@ -207,8 +236,15 @@ module.exports = function (ctx) {
}
}

const updateConfig = () => {
for (const key of CONFIG_KEYS) {
menu.getMenuItemById(key).checked = store.get(key, false)
}
}

ipcMain.on('ipfsd', (status) => { updateStatus(status) })
ipcMain.on('languageUpdated', () => { setupMenu(status) })
ipcMain.on('configUpdated', (config) => { updateConfig(config) })
setupMenu()

ctx.tray = tray
Expand Down
16 changes: 16 additions & 0 deletions src/webui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const logger = require('./common/logger')
const { shell } = require('electron')

module.exports = async function (ctx) {
ctx.launchWebUI = (url) => {
if (!url) {
logger.info('[web ui] launching web ui')
} else {
logger.info(`[web ui] navigate to ${url}`)
}

// TODO: correct api port
// TODO: use hash directly so we can make sure we open the correct url
shell.openExternal(`http://localhost:5001/webui/${url}`)
}
}
11 changes: 0 additions & 11 deletions src/webui/connection-status.js

This file was deleted.

Loading