lbry-desktop/ui/js/actions/app.js

241 lines
5.4 KiB
JavaScript
Raw Normal View History

2017-04-07 07:15:22 +02:00
import * as types from 'constants/action_types'
import lbry from 'lbry'
import {
selectUpdateUrl,
selectUpgradeDownloadDir,
selectUpgradeDownloadItem,
selectUpgradeFilename,
selectPageTitle,
selectCurrentPage,
selectCurrentParams,
2017-04-07 07:15:22 +02:00
} from 'selectors/app'
import {
doSearch,
} from 'actions/search'
2017-04-07 07:15:22 +02:00
const {remote, ipcRenderer, shell} = require('electron');
const path = require('path');
const app = require('electron').remote.app;
const {download} = remote.require('electron-dl');
const fs = remote.require('fs');
2017-05-06 18:31:47 +02:00
const queryStringFromParams = (params) => {
return Object
.keys(params)
.map(key => `${key}=${params[key]}`)
.join('&')
}
2017-05-06 19:57:34 +02:00
export function doNavigate(path, params = {}) {
return function(dispatch, getState) {
2017-05-06 18:31:47 +02:00
let url = path
if (params)
url = `${url}?${queryStringFromParams(params)}`
2017-05-06 19:57:34 +02:00
dispatch(doChangePath(url))
2017-05-05 19:05:33 +02:00
2017-05-06 19:57:34 +02:00
const state = getState()
const pageTitle = selectPageTitle(state)
dispatch(doHistoryPush(params, pageTitle, url))
2017-04-07 07:15:22 +02:00
}
}
2017-05-06 09:25:14 +02:00
export function doChangePath(path) {
2017-05-06 18:31:47 +02:00
return function(dispatch, getState) {
dispatch({
type: types.CHANGE_PATH,
data: {
path,
}
})
2017-05-22 17:58:17 +02:00
const state = getState()
const pageTitle = selectPageTitle(state)
window.document.title = pageTitle
const currentPage = selectCurrentPage(state)
if (currentPage === 'search') {
const params = selectCurrentParams(state)
dispatch(doSearch(params.query))
}
2017-05-06 18:31:47 +02:00
}
}
export function doHistoryBack() {
return function(dispatch, getState) {
history.back()
2017-05-06 09:25:14 +02:00
}
}
export function doHistoryPush(params, title, relativeUrl) {
return function(dispatch, getState) {
let pathParts = window.location.pathname.split('/')
pathParts[pathParts.length - 1] = relativeUrl.replace(/^\//, '')
const url = pathParts.join('/')
title += " - LBRY"
history.pushState(params, title, url)
}
2017-04-07 07:15:22 +02:00
}
export function doOpenModal(modal) {
return {
type: types.OPEN_MODAL,
data: {
modal
}
}
}
export function doCloseModal() {
return {
type: types.CLOSE_MODAL,
}
}
export function doUpdateDownloadProgress(percent) {
return {
type: types.UPGRADE_DOWNLOAD_PROGRESSED,
data: {
percent: percent
}
}
}
export function doSkipUpgrade() {
return {
type: types.SKIP_UPGRADE
}
}
export function doStartUpgrade() {
return function(dispatch, getState) {
const state = getState()
const upgradeDownloadPath = selectUpgradeDownloadDir(state)
ipcRenderer.send('upgrade', upgradeDownloadPath)
}
}
export function doDownloadUpgrade() {
return function(dispatch, getState) {
const state = getState()
// Make a new directory within temp directory so the filename is guaranteed to be available
const dir = fs.mkdtempSync(app.getPath('temp') + require('path').sep);
const upgradeFilename = selectUpgradeFilename(state)
let options = {
onProgress: (p) => dispatch(doUpdateDownloadProgress(Math.round(p * 100))),
directory: dir,
};
download(remote.getCurrentWindow(), selectUpdateUrl(state), options)
.then(downloadItem => {
/**
* TODO: get the download path directly from the download object. It should just be
* downloadItem.getSavePath(), but the copy on the main process is being garbage collected
* too soon.
*/
const _upgradeDownloadItem = downloadItem;
const _upgradeDownloadPath = path.join(dir, upgradeFilename);
dispatch({
type: types.UPGRADE_DOWNLOAD_COMPLETED,
data: {
dir,
downloadItem
}
})
});
dispatch({
type: types.UPGRADE_DOWNLOAD_STARTED
})
dispatch({
type: types.OPEN_MODAL,
data: {
modal: 'downloading'
}
})
}
}
export function doCancelUpgrade() {
return function(dispatch, getState) {
const state = getState()
const upgradeDownloadItem = selectUpgradeDownloadItem(state)
if (upgradeDownloadItem) {
/*
* Right now the remote reference to the download item gets garbage collected as soon as the
* the download is over (maybe even earlier), so trying to cancel a finished download may
* throw an error.
*/
try {
upgradeDownloadItem.cancel();
} catch (err) {
console.error(err)
// Do nothing
}
}
dispatch({ type: types.UPGRADE_CANCELLED })
}
}
export function doCheckUpgradeAvailable() {
return function(dispatch, getState) {
const state = getState()
2017-05-04 05:44:08 +02:00
lbry.getVersionInfo().then(({remoteVersion, upgradeAvailable}) => {
if (upgradeAvailable) {
2017-04-07 07:15:22 +02:00
dispatch({
type: types.UPDATE_VERSION,
data: {
2017-05-05 19:05:33 +02:00
version: remoteVersion,
2017-04-07 07:15:22 +02:00
}
})
dispatch({
type: types.OPEN_MODAL,
data: {
modal: 'upgrade'
}
})
2017-05-04 05:44:08 +02:00
}
2017-04-07 07:15:22 +02:00
});
}
}
export function doAlertError(errorList) {
return function(dispatch, getState) {
const state = getState()
2017-05-24 23:53:03 +02:00
console.log('do alert error')
console.log(errorList)
2017-04-07 07:15:22 +02:00
dispatch({
type: types.OPEN_MODAL,
data: {
modal: 'error',
2017-05-24 23:53:03 +02:00
extraContent: errorList
2017-04-07 07:15:22 +02:00
}
})
}
}
2017-04-22 15:17:01 +02:00
export function doDaemonReady() {
return {
type: types.DAEMON_READY
}
}
2017-05-23 09:21:21 +02:00
export function doShowSnackBar(data) {
return {
type: types.SHOW_SNACKBAR,
data,
}
}
export function doRemoveSnackBarSnack() {
return {
type: types.REMOVE_SNACKBAR_SNACK,
}
}