2017-06-06 23:19:12 +02:00
|
|
|
import * as types from "constants/action_types";
|
|
|
|
import lbry from "lbry";
|
2017-04-07 07:15:22 +02:00
|
|
|
import {
|
|
|
|
selectUpdateUrl,
|
2017-06-01 08:51:16 +02:00
|
|
|
selectUpgradeDownloadPath,
|
2017-04-07 07:15:22 +02:00
|
|
|
selectUpgradeDownloadItem,
|
2017-07-29 01:31:10 +02:00
|
|
|
selectUpgradeFilename,
|
2017-04-30 18:01:43 +02:00
|
|
|
selectPageTitle,
|
2017-05-23 10:46:52 +02:00
|
|
|
selectCurrentPage,
|
|
|
|
selectCurrentParams,
|
2017-08-16 23:24:04 +02:00
|
|
|
selectHistoryBack,
|
|
|
|
selectHistoryForward,
|
2017-06-06 23:19:12 +02:00
|
|
|
} from "selectors/app";
|
2017-06-08 02:56:52 +02:00
|
|
|
import { doSearch } from "actions/search";
|
2017-08-24 18:11:39 +02:00
|
|
|
import { doFetchDaemonSettings, doSetTheme } from "actions/settings";
|
2017-06-08 02:56:52 +02:00
|
|
|
import { doAuthenticate } from "actions/user";
|
|
|
|
import { doFileList } from "actions/file_info";
|
2017-07-19 07:35:32 +02:00
|
|
|
import { toQueryString } from "util/query_params";
|
2017-07-23 10:49:56 +02:00
|
|
|
import { parseQueryParams } from "util/query_params";
|
2017-04-07 07:15:22 +02:00
|
|
|
|
2017-06-06 23:19:12 +02:00
|
|
|
const { remote, ipcRenderer, shell } = require("electron");
|
|
|
|
const path = require("path");
|
|
|
|
const { download } = remote.require("electron-dl");
|
|
|
|
const fs = remote.require("fs");
|
2017-07-19 23:05:08 +02:00
|
|
|
const { lbrySettings: config } = require("../../../app/package.json");
|
2017-04-07 07:15:22 +02:00
|
|
|
|
2017-07-17 08:06:04 +02:00
|
|
|
export function doNavigate(path, params = {}, options = {}) {
|
2017-04-30 18:01:43 +02:00
|
|
|
return function(dispatch, getState) {
|
2017-06-06 23:19:12 +02:00
|
|
|
let url = path;
|
2017-07-19 07:35:32 +02:00
|
|
|
if (params) url = `${url}?${toQueryString(params)}`;
|
2017-05-06 18:31:47 +02:00
|
|
|
|
2017-06-06 23:19:12 +02:00
|
|
|
dispatch(doChangePath(url));
|
2017-05-05 19:05:33 +02:00
|
|
|
|
2017-06-06 23:19:12 +02:00
|
|
|
const state = getState();
|
|
|
|
const pageTitle = selectPageTitle(state);
|
2017-08-16 13:58:18 +02:00
|
|
|
const historyState = history.state;
|
2017-08-16 23:24:04 +02:00
|
|
|
|
2017-08-16 13:58:18 +02:00
|
|
|
dispatch(
|
|
|
|
doHistoryPush({ params, page: historyState.page + 1 }, pageTitle, url)
|
|
|
|
);
|
2017-06-06 23:19:12 +02:00
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
}
|
|
|
|
|
2017-07-19 17:09:40 +02:00
|
|
|
export function doAuthNavigate(pathAfterAuth = null, params = {}) {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
if (pathAfterAuth) {
|
|
|
|
dispatch({
|
|
|
|
type: types.CHANGE_AFTER_AUTH_PATH,
|
|
|
|
data: {
|
2017-07-22 00:06:39 +02:00
|
|
|
path: `${pathAfterAuth}?${toQueryString(params)}`,
|
2017-07-19 17:09:40 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
dispatch(doNavigate("/auth"));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:35:03 +02:00
|
|
|
export function doChangePath(path, options = {}) {
|
2017-05-06 18:31:47 +02:00
|
|
|
return function(dispatch, getState) {
|
|
|
|
dispatch({
|
|
|
|
type: types.CHANGE_PATH,
|
|
|
|
data: {
|
|
|
|
path,
|
2017-06-06 23:19:12 +02:00
|
|
|
},
|
|
|
|
});
|
2017-05-22 17:58:17 +02:00
|
|
|
|
2017-06-06 23:19:12 +02:00
|
|
|
const state = getState();
|
|
|
|
const pageTitle = selectPageTitle(state);
|
2017-07-19 10:35:03 +02:00
|
|
|
const scrollY = options.scrollY;
|
|
|
|
|
2017-06-06 23:19:12 +02:00
|
|
|
window.document.title = pageTitle;
|
2017-07-19 10:35:03 +02:00
|
|
|
|
|
|
|
if (scrollY) window.scrollTo(0, scrollY);
|
|
|
|
else window.scrollTo(0, 0);
|
2017-05-23 10:46:52 +02:00
|
|
|
|
2017-06-06 23:19:12 +02:00
|
|
|
const currentPage = selectCurrentPage(state);
|
|
|
|
if (currentPage === "search") {
|
|
|
|
const params = selectCurrentParams(state);
|
|
|
|
dispatch(doSearch(params.query));
|
2017-05-23 10:46:52 +02:00
|
|
|
}
|
2017-06-06 23:19:12 +02:00
|
|
|
};
|
2017-05-06 18:31:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function doHistoryBack() {
|
|
|
|
return function(dispatch, getState) {
|
2017-08-16 23:24:04 +02:00
|
|
|
// Get back history from stack
|
|
|
|
const back = selectHistoryBack(getState());
|
|
|
|
|
|
|
|
if (back) {
|
|
|
|
// Set location
|
|
|
|
dispatch(doChangePath(back.location));
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: types.HISTORY_NAVIGATE,
|
|
|
|
data: { page: back },
|
|
|
|
});
|
2017-08-16 01:29:55 +02:00
|
|
|
}
|
2017-06-06 23:19:12 +02:00
|
|
|
};
|
2017-05-06 09:25:14 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 06:10:42 +02:00
|
|
|
export function doHistoryForward() {
|
|
|
|
return function(dispatch, getState) {
|
2017-08-16 23:24:04 +02:00
|
|
|
// Get forward history from stack
|
|
|
|
const forward = selectHistoryForward(getState());
|
|
|
|
|
|
|
|
if (forward) {
|
|
|
|
// Set location
|
|
|
|
dispatch(doChangePath(forward.location));
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: types.HISTORY_NAVIGATE,
|
|
|
|
data: { page: forward },
|
|
|
|
});
|
|
|
|
}
|
2017-08-08 06:10:42 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:35:03 +02:00
|
|
|
export function doHistoryPush(currentState, title, relativeUrl) {
|
2017-05-17 23:52:45 +02:00
|
|
|
return function(dispatch, getState) {
|
2017-06-06 23:19:12 +02:00
|
|
|
title += " - LBRY";
|
2017-07-19 10:35:03 +02:00
|
|
|
history.pushState(currentState, title, `#${relativeUrl}`);
|
2017-08-16 23:24:04 +02:00
|
|
|
dispatch({
|
|
|
|
type: types.HISTORY_NAVIGATE,
|
|
|
|
data: {
|
|
|
|
location: relativeUrl,
|
|
|
|
},
|
|
|
|
});
|
2017-07-19 10:35:03 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doRecordScroll(scroll) {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
const state = getState();
|
|
|
|
const historyState = history.state;
|
|
|
|
|
|
|
|
if (!historyState) return;
|
|
|
|
|
|
|
|
historyState.scrollY = scroll;
|
|
|
|
history.replaceState(
|
|
|
|
historyState,
|
|
|
|
document.title,
|
|
|
|
`#${state.app.currentPath}`
|
|
|
|
);
|
2017-06-06 23:19:12 +02:00
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function doOpenModal(modal) {
|
|
|
|
return {
|
|
|
|
type: types.OPEN_MODAL,
|
|
|
|
data: {
|
2017-06-06 23:19:12 +02:00
|
|
|
modal,
|
|
|
|
},
|
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function doCloseModal() {
|
|
|
|
return {
|
|
|
|
type: types.CLOSE_MODAL,
|
2017-06-06 23:19:12 +02:00
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function doUpdateDownloadProgress(percent) {
|
|
|
|
return {
|
|
|
|
type: types.UPGRADE_DOWNLOAD_PROGRESSED,
|
|
|
|
data: {
|
2017-06-06 23:19:12 +02:00
|
|
|
percent: percent,
|
|
|
|
},
|
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function doSkipUpgrade() {
|
|
|
|
return {
|
2017-06-06 23:19:12 +02:00
|
|
|
type: types.SKIP_UPGRADE,
|
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function doStartUpgrade() {
|
|
|
|
return function(dispatch, getState) {
|
2017-06-06 23:19:12 +02:00
|
|
|
const state = getState();
|
|
|
|
const upgradeDownloadPath = selectUpgradeDownloadPath(state);
|
2017-04-07 07:15:22 +02:00
|
|
|
|
2017-06-06 23:19:12 +02:00
|
|
|
ipcRenderer.send("upgrade", upgradeDownloadPath);
|
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function doDownloadUpgrade() {
|
|
|
|
return function(dispatch, getState) {
|
2017-06-06 23:19:12 +02:00
|
|
|
const state = getState();
|
2017-04-07 07:15:22 +02:00
|
|
|
// Make a new directory within temp directory so the filename is guaranteed to be available
|
2017-07-19 23:05:08 +02:00
|
|
|
const dir = fs.mkdtempSync(
|
|
|
|
remote.app.getPath("temp") + require("path").sep
|
2017-07-29 01:31:10 +02:00
|
|
|
),
|
|
|
|
upgradeFilename = selectUpgradeFilename(state);
|
2017-04-07 07:15:22 +02:00
|
|
|
|
|
|
|
let options = {
|
2017-06-06 23:19:12 +02:00
|
|
|
onProgress: p => dispatch(doUpdateDownloadProgress(Math.round(p * 100))),
|
2017-04-07 07:15:22 +02:00
|
|
|
directory: dir,
|
|
|
|
};
|
2017-06-06 23:19:12 +02:00
|
|
|
download(
|
|
|
|
remote.getCurrentWindow(),
|
|
|
|
selectUpdateUrl(state),
|
|
|
|
options
|
|
|
|
).then(downloadItem => {
|
|
|
|
/**
|
2017-04-07 07:15:22 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-06-06 23:19:12 +02:00
|
|
|
dispatch({
|
|
|
|
type: types.UPGRADE_DOWNLOAD_COMPLETED,
|
|
|
|
data: {
|
|
|
|
downloadItem,
|
|
|
|
path: path.join(dir, upgradeFilename),
|
|
|
|
},
|
2017-04-07 07:15:22 +02:00
|
|
|
});
|
2017-06-06 23:19:12 +02:00
|
|
|
});
|
2017-04-07 07:15:22 +02:00
|
|
|
|
|
|
|
dispatch({
|
2017-06-06 23:19:12 +02:00
|
|
|
type: types.UPGRADE_DOWNLOAD_STARTED,
|
|
|
|
});
|
2017-04-07 07:15:22 +02:00
|
|
|
dispatch({
|
|
|
|
type: types.OPEN_MODAL,
|
|
|
|
data: {
|
2017-06-06 23:19:12 +02:00
|
|
|
modal: "downloading",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function doCancelUpgrade() {
|
|
|
|
return function(dispatch, getState) {
|
2017-06-06 23:19:12 +02:00
|
|
|
const state = getState();
|
|
|
|
const upgradeDownloadItem = selectUpgradeDownloadItem(state);
|
2017-04-07 07:15:22 +02:00
|
|
|
|
|
|
|
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) {
|
2017-06-06 23:19:12 +02:00
|
|
|
console.error(err);
|
2017-04-07 07:15:22 +02:00
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 23:19:12 +02:00
|
|
|
dispatch({ type: types.UPGRADE_CANCELLED });
|
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function doCheckUpgradeAvailable() {
|
|
|
|
return function(dispatch, getState) {
|
2017-06-06 23:19:12 +02:00
|
|
|
const state = getState();
|
2017-04-07 07:15:22 +02:00
|
|
|
|
2017-06-06 23:19:12 +02:00
|
|
|
lbry.getAppVersionInfo().then(({ remoteVersion, upgradeAvailable }) => {
|
2017-05-04 05:44:08 +02:00
|
|
|
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-06-06 23:19:12 +02:00
|
|
|
},
|
|
|
|
});
|
2017-04-07 07:15:22 +02:00
|
|
|
dispatch({
|
|
|
|
type: types.OPEN_MODAL,
|
|
|
|
data: {
|
2017-06-06 23:19:12 +02:00
|
|
|
modal: "upgrade",
|
|
|
|
},
|
|
|
|
});
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
2017-04-07 07:15:22 +02:00
|
|
|
});
|
2017-06-06 23:19:12 +02:00
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
}
|
|
|
|
|
2017-07-19 23:05:08 +02:00
|
|
|
export function doCheckDaemonVersion() {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
lbry.version().then(({ lbrynet_version }) => {
|
|
|
|
dispatch({
|
|
|
|
type: config.lbrynetDaemonVersion == lbrynet_version
|
|
|
|
? types.DAEMON_VERSION_MATCH
|
|
|
|
: types.DAEMON_VERSION_MISMATCH,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-04-07 07:15:22 +02:00
|
|
|
export function doAlertError(errorList) {
|
|
|
|
return function(dispatch, getState) {
|
2017-06-06 23:19:12 +02:00
|
|
|
const state = getState();
|
2017-04-07 07:15:22 +02:00
|
|
|
dispatch({
|
|
|
|
type: types.OPEN_MODAL,
|
|
|
|
data: {
|
2017-06-06 23:19:12 +02:00
|
|
|
modal: "error",
|
|
|
|
extraContent: errorList,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
}
|
|
|
|
|
2017-04-22 15:17:01 +02:00
|
|
|
export function doDaemonReady() {
|
2017-06-01 18:20:12 +02:00
|
|
|
return function(dispatch, getState) {
|
2017-07-23 10:49:56 +02:00
|
|
|
const path = window.location.hash || "#/discover";
|
|
|
|
const params = parseQueryParams(path.split("?")[1] || "");
|
2017-08-16 23:24:04 +02:00
|
|
|
|
|
|
|
// Get first page
|
|
|
|
const page = {
|
|
|
|
index: 0,
|
|
|
|
location: path.replace(/^#/, ""),
|
|
|
|
};
|
|
|
|
|
2017-08-16 01:29:55 +02:00
|
|
|
history.replaceState(
|
2017-08-16 13:58:18 +02:00
|
|
|
{ params, is_first_page: true, page: 1 },
|
2017-08-16 01:29:55 +02:00
|
|
|
document.title,
|
|
|
|
`${path}`
|
|
|
|
);
|
2017-06-08 02:56:52 +02:00
|
|
|
dispatch(doAuthenticate());
|
2017-06-01 18:20:12 +02:00
|
|
|
dispatch({
|
2017-06-08 02:56:52 +02:00
|
|
|
type: types.DAEMON_READY,
|
2017-08-16 23:24:04 +02:00
|
|
|
data: { page },
|
2017-06-08 02:56:52 +02:00
|
|
|
});
|
2017-08-24 18:11:39 +02:00
|
|
|
dispatch(doSetTheme());
|
2017-06-08 02:56:52 +02:00
|
|
|
dispatch(doFetchDaemonSettings());
|
|
|
|
dispatch(doFileList());
|
|
|
|
};
|
2017-04-22 15:17:01 +02:00
|
|
|
}
|
2017-05-23 09:21:21 +02:00
|
|
|
|
|
|
|
export function doShowSnackBar(data) {
|
|
|
|
return {
|
|
|
|
type: types.SHOW_SNACKBAR,
|
|
|
|
data,
|
2017-06-06 23:19:12 +02:00
|
|
|
};
|
2017-05-23 09:21:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function doRemoveSnackBarSnack() {
|
|
|
|
return {
|
|
|
|
type: types.REMOVE_SNACKBAR_SNACK,
|
2017-06-06 23:19:12 +02:00
|
|
|
};
|
2017-05-23 09:21:21 +02:00
|
|
|
}
|
2017-06-16 07:43:43 +02:00
|
|
|
|
|
|
|
export function doClearCache() {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
window.cacheStore.purge();
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
};
|
|
|
|
}
|
2017-07-19 23:05:08 +02:00
|
|
|
|
2017-07-29 21:22:17 +02:00
|
|
|
export function doQuit() {
|
2017-07-19 23:05:08 +02:00
|
|
|
return function(dispatch, getState) {
|
|
|
|
remote.app.quit();
|
|
|
|
};
|
|
|
|
}
|