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

Ship the new WebUI #590

Merged
merged 8 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ yarn-error.log
crowdin.yml
.*~
add-on/dist
add-on/webui/
add-on/ui-kit/
coverage
.nyc_output
add-on/ui-kit/**/*
add-on/manifest.json
27 changes: 26 additions & 1 deletion add-on/src/lib/ipfs-client/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const browser = require('webextension-polyfill')
const external = require('./external')
const embedded = require('./embedded')

Expand All @@ -14,7 +15,9 @@ async function initIpfsClient (opts) {
client = external
}

return client.init(opts)
const instance = await client.init(opts)
_reloadIpfsClientDependents() // async (API is present)
return instance
}

async function destroyIpfsClient () {
Expand All @@ -23,6 +26,28 @@ async function destroyIpfsClient () {
await client.destroy()
} finally {
client = null
await _reloadIpfsClientDependents() // sync (API stopped working)
}
}
}

function _isWebuiTab (url) {
const bundled = !url.startsWith('http') && url.includes('/webui/index.html#/')
const ipns = url.includes('/webui.ipfs.io/#/')
Copy link
Member Author

@lidel lidel Oct 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@olizilla FYI this is part of #590. It will detect webui loaded from:

  • https://webui.ipfs.io
  • http://127.0.0.1:8080/ipns/webui.ipfs.io

And reload it when companion API state changes (on/off/port change etc)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#/ will only be added by the app after the initial bundle has loaded (unless the user has bookmarked it explicitly)...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that was on purpose to remove false-positive matches on random websites and avoid double-reload when quickly switching between backends.

return bundled || ipns
}

async function _reloadIpfsClientDependents () {
if (browser.tabs && browser.tabs.query) {
const tabs = await browser.tabs.query({})
if (tabs) {
tabs.forEach((tab) => {
// detect bundled webui in any of open tabs
if (_isWebuiTab(tab.url)) {
browser.tabs.reload(tab.id)
console.log('[ipfs-companion] reloading bundled webui')
}
})
}
}
}
Expand Down
1 change: 1 addition & 0 deletions add-on/src/popup/browser-action/context-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = function contextActions ({
})}
${navItem({
text: browser.i18n.getMessage(contextMenuCopyRawCid),
disabled: !activePinControls,
onClick: () => onCopy(contextMenuCopyRawCid)
})}
${!isPinned ? (
Expand Down
6 changes: 4 additions & 2 deletions add-on/src/popup/browser-action/nav-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

const html = require('choo/html')

function navItem ({ icon, text, bold, disabled, onClick }) {
function navItem ({ icon, text, disabled, addClass, onClick }) {
let className = 'black button-reset db w-100 bg-white b--none outline-0--focus pv2 ph3 f5 tl'
if (bold) className += ' b'
if (disabled) {
className += ' o-40'
} else {
className += ' pointer bg-near-white--hover'
}
if (addClass) {
className += ` ${addClass}`
}

return html`
<button class="${className}" onclick=${disabled ? null : onClick} ${disabled ? 'disabled' : ''}>
Expand Down
2 changes: 1 addition & 1 deletion add-on/src/popup/browser-action/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = function operations ({
<div class="fade-in pv1">
${navItem({
text: browser.i18n.getMessage('panel_quickUpload'),
bold: true,
addClass: 'b',
disabled: !activeQuickUpload,
onClick: onQuickUpload
})}
Expand Down
5 changes: 2 additions & 3 deletions add-on/src/popup/browser-action/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ module.exports = (state, emitter) => {

emitter.on('openWebUi', async () => {
try {
const options = await browser.storage.local.get('ipfsApiUrl')
const apiUrl = options['ipfsApiUrl']
await browser.tabs.create({ url: apiUrl + '/webui/' })
// Open bundled version of WebUI
await browser.tabs.create({ url: '/webui/index.html' })
window.close()
} catch (error) {
console.error(`Unable Open Web UI due to ${error}`)
Expand Down
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"clean:build": "shx rm -rf build/*",
"clean:dist": "shx rm -rf add-on/dist",
"clean:ui-kit": "shx rm -rf add-on/ui-kit",
"clean:webui": "shx rm -rf add-on/webui",
"build": "run-s clean build:*",
"build:copy": "run-s build:copy:*",
"build:copy:src": "shx mkdir -p add-on/dist && shx cp -R add-on/src/* add-on/dist",
Expand All @@ -22,6 +23,12 @@
"build:copy:ui-kit:ipfs-css:fonts": "shx mkdir -p add-on/ui-kit/fonts && shx cp node_modules/ipfs-css/fonts/* add-on/ui-kit/fonts",
"build:copy:ui-kit:ipfs-css:icons": "shx mkdir -p add-on/ui-kit/icons && shx cp node_modules/ipfs-css/icons/* add-on/ui-kit/icons",
"build:copy:ui-kit:tachyons": "shx mkdir -p add-on/ui-kit && shx cp node_modules/tachyons/css/tachyons.css add-on/ui-kit",
"build:webui": "cross-env CID=QmQYAwCEy1wAxXwkQBzeA1L7RXjmm7Sk3T6mQAHvwTFxpj npm run build:webui:with-cid",
"build:webui:with-cid": "cross-env-shell \"shx test -d add-on/webui/ || (npm run build:webui:dir && (npm run build:webui:fetch-ipfs || npm run build:webui:fetch-http) && npm run build:webui:minimize)\"",
"build:webui:dir": "shx mkdir -p add-on/webui",
"build:webui:fetch-ipfs": "cross-env-shell \"ipfs get $CID -o add-on/webui/\"",
"build:webui:fetch-http": "cross-env-shell \"node scripts/fetch-webui-from-gateway.js $CID add-on/webui/\"",
"build:webui:minimize": "shx rm -rf add-on/webui/static/js/*.map && shx rm -rf add-on/webui/static/css/*.map",
"build:js": "run-p build:js:*",
"build:js:webpack": "webpack -p",
"build:minimize-dist": "shx rm -rf add-on/dist/lib add-on/dist/contentScripts/ add-on/dist/bundles/ipfsProxyContentScriptPayload.bundle.js",
Expand All @@ -38,10 +45,10 @@
"test": "run-s test:*",
"test:functional": " nyc --reporter=lcov --reporter=text mocha --timeout 15000 --require ignore-styles --reporter mocha-jenkins-reporter \"test/functional/**/*.test.js\"",
"lint": "run-s lint:*",
"lint:standard": "standard -v \"*.js\" \"add-on/src/**/*.js\" \"test/**/*.js\"",
"lint:standard": "standard -v \"*.js\" \"add-on/src/**/*.js\" \"test/**/*.js\" \"scripts/**/*.js\"",
"lint:web-ext": "web-ext lint",
"fix:lint": "run-s fix:lint:*",
"fix:lint:standard": "standard -v --fix \"*.js\" \"add-on/src/**/*.js\" \"test/**/*.js\"",
"fix:lint:standard": "standard -v --fix \"*.js\" \"add-on/src/**/*.js\" \"test/**/*.js\" \"scripts/**/*.js\"",
"precommit": "run-s lint:standard",
"prepush": "run-s clean build lint test",
"firefox": "web-ext run --url about:debugging",
Expand All @@ -68,6 +75,7 @@
"babel-plugin-syntax-async-generators": "6.13.0",
"chai": "4.1.2",
"cross-env": "5.2.0",
"download-cli": "1.1.1",
"fakefile": "0.0.9",
"fs-promise": "2.0.3",
"get-firefox": "2.2.0",
Expand All @@ -80,11 +88,13 @@
"npm-run-all": "4.1.3",
"nyc": "13.0.1",
"raw-loader": "0.5.1",
"request-progress": "3.0.0",
"shx": "0.3.2",
"simple-progress-webpack-plugin": "1.1.2",
"sinon": "6.1.5",
"sinon-chrome": "2.3.2",
"standard": "12.0.1",
"tar": "4.4.6",
"terser": "3.8.2",
"transform-loader": "0.2.4",
"web-ext": "2.9.1",
Expand Down
27 changes: 27 additions & 0 deletions scripts/fetch-webui-from-gateway.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* This is a fallback script used when ipfs cli fails or is not available
* More details: https://github.com/ipfs-shipyard/ipfs-webui/issues/843
*/
const tar = require('tar')
const request = require('request')
const progress = require('request-progress')

const cid = process.argv[2]
const destination = process.argv[3]

// pick random preloader
const no = Math.round(Math.random()) // 0 or 1
const url = 'https://node' + no + '.preload.ipfs.io/api/v0/get?arg=' + cid + '&archive=true&compress=true'

console.log('Fallback to HTTP GET from: ' + url)

progress(request(url), { lengthHeader: 'x-content-length', delay: 10000 })
.on('progress', (state) => console.log(`progress: ${Math.round(state.percent)} %, transferred: ${state.size.transferred}`, state))
.on('response', (response) => console.log('Status Code', response.statusCode))
.on('error', (error) => console.log('Download Error', error))
.on('close', () => console.log('Done! webui extracted to: ' + destination))
.pipe(
tar.extract({
strip: 1,
C: destination
})
)
1 change: 1 addition & 0 deletions web-ext-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
sourceDir: 'add-on/',
ignoreFiles: [
'src/',
'*.map',
'manifest.*.json'
],
// Command options:
Expand Down
Loading