2017-11-29 09:57:37 +01:00
|
|
|
// @flow
|
2017-04-07 07:15:22 +02:00
|
|
|
|
2017-11-29 09:57:37 +01:00
|
|
|
import * as types from "../../constants/action_types";
|
|
|
|
import * as modalTypes from "../../constants/modal_types";
|
|
|
|
|
|
|
|
// $FlowIssue: Flow cannot find election
|
2017-06-24 10:57:37 +02:00
|
|
|
const { remote } = require("electron");
|
2017-11-29 09:57:37 +01:00
|
|
|
|
2017-06-24 10:57:37 +02:00
|
|
|
const application = remote.app;
|
|
|
|
const win = remote.BrowserWindow.getFocusedWindow();
|
|
|
|
|
2017-06-06 06:21:55 +02:00
|
|
|
const reducers = {};
|
2017-11-29 09:57:37 +01:00
|
|
|
type appState = {
|
|
|
|
isLoaded: boolean,
|
|
|
|
modal: mixed,
|
|
|
|
modalProps: mixed,
|
|
|
|
platform: string,
|
2017-11-29 23:43:27 +01:00
|
|
|
upgradeSkipped: boolean,
|
|
|
|
daemonVersionMatched: ?boolean,
|
2017-11-29 09:57:37 +01:00
|
|
|
daemonReady: boolean,
|
|
|
|
hasSignature: boolean,
|
|
|
|
badgeNumber: number,
|
2017-11-29 23:43:27 +01:00
|
|
|
volume: number,
|
2017-11-29 09:57:37 +01:00
|
|
|
downloadProgress?: number,
|
|
|
|
upgradeDownloading?: boolean,
|
|
|
|
upgradeDownloadComplete?: boolean,
|
|
|
|
checkUpgradeTimer?: mixed,
|
|
|
|
isUpgradeAvailable?: boolean,
|
|
|
|
isUpgradeSkipped?: boolean,
|
|
|
|
snackBar?: mixed,
|
|
|
|
};
|
|
|
|
const defaultState: appState = {
|
2017-04-07 07:15:22 +02:00
|
|
|
isLoaded: false,
|
2017-09-08 05:15:05 +02:00
|
|
|
modal: null,
|
|
|
|
modalProps: {},
|
2017-04-07 07:15:22 +02:00
|
|
|
platform: process.platform,
|
2017-11-29 23:43:27 +01:00
|
|
|
upgradeSkipped: sessionStorage.getItem("upgradeSkipped") === "true",
|
2017-07-19 23:05:08 +02:00
|
|
|
daemonVersionMatched: null,
|
2017-04-22 15:17:01 +02:00
|
|
|
daemonReady: false,
|
2017-04-29 11:50:29 +02:00
|
|
|
hasSignature: false,
|
2017-06-24 10:57:37 +02:00
|
|
|
badgeNumber: 0,
|
2017-11-29 23:43:27 +01:00
|
|
|
volume: Number(sessionStorage.getItem("volume")) || 1,
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
|
2017-05-02 05:57:50 +02:00
|
|
|
reducers[types.DAEMON_READY] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
daemonReady: true,
|
2017-06-06 23:19:12 +02:00
|
|
|
});
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
2017-05-02 05:57:50 +02:00
|
|
|
|
2017-07-19 23:05:08 +02:00
|
|
|
reducers[types.DAEMON_VERSION_MATCH] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
daemonVersionMatched: true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
reducers[types.DAEMON_VERSION_MISMATCH] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
daemonVersionMatched: false,
|
|
|
|
modal: modalTypes.INCOMPATIBLE_DAEMON,
|
|
|
|
});
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
2017-05-02 05:57:50 +02:00
|
|
|
|
2017-04-07 07:15:22 +02:00
|
|
|
reducers[types.UPGRADE_CANCELLED] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
downloadProgress: null,
|
2017-05-25 22:07:32 +02:00
|
|
|
upgradeDownloadComplete: false,
|
2017-04-07 07:15:22 +02:00
|
|
|
modal: null,
|
2017-06-06 23:19:12 +02:00
|
|
|
});
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
|
|
|
|
reducers[types.UPGRADE_DOWNLOAD_COMPLETED] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
2017-06-01 08:51:16 +02:00
|
|
|
downloadPath: action.data.path,
|
2017-05-25 22:07:32 +02:00
|
|
|
upgradeDownloading: false,
|
2017-06-06 23:19:12 +02:00
|
|
|
upgradeDownloadCompleted: true,
|
|
|
|
});
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
|
|
|
|
reducers[types.UPGRADE_DOWNLOAD_STARTED] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
2017-06-06 23:19:12 +02:00
|
|
|
upgradeDownloading: true,
|
|
|
|
});
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
|
|
|
|
reducers[types.SKIP_UPGRADE] = function(state, action) {
|
2017-11-29 23:43:27 +01:00
|
|
|
sessionStorage.setItem("upgradeSkipped", "true");
|
2017-04-07 07:15:22 +02:00
|
|
|
|
|
|
|
return Object.assign({}, state, {
|
2017-11-15 02:50:21 +01:00
|
|
|
isUpgradeSkipped: true,
|
2017-06-06 23:19:12 +02:00
|
|
|
modal: null,
|
|
|
|
});
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
|
|
|
|
reducers[types.UPDATE_VERSION] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
2017-06-06 23:19:12 +02:00
|
|
|
version: action.data.version,
|
|
|
|
});
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
|
2017-11-16 22:39:10 +01:00
|
|
|
reducers[types.CHECK_UPGRADE_SUCCESS] = function(state, action) {
|
2017-11-15 02:50:21 +01:00
|
|
|
return Object.assign({}, state, {
|
|
|
|
isUpgradeAvailable: action.data.upgradeAvailable,
|
|
|
|
remoteVersion: action.data.remoteVersion,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-11-16 22:39:10 +01:00
|
|
|
reducers[types.CHECK_UPGRADE_SUBSCRIBE] = function(state, action) {
|
2017-11-15 02:50:21 +01:00
|
|
|
return Object.assign({}, state, {
|
|
|
|
checkUpgradeTimer: action.data.checkUpgradeTimer,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-04-07 07:15:22 +02:00
|
|
|
reducers[types.OPEN_MODAL] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
modal: action.data.modal,
|
2017-09-08 05:15:05 +02:00
|
|
|
modalProps: action.data.modalProps || {},
|
2017-06-06 23:19:12 +02:00
|
|
|
});
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
|
|
|
|
reducers[types.CLOSE_MODAL] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
modal: undefined,
|
2017-09-08 05:15:05 +02:00
|
|
|
modalProps: {},
|
2017-06-06 23:19:12 +02:00
|
|
|
});
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
|
|
|
|
reducers[types.UPGRADE_DOWNLOAD_PROGRESSED] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
2017-06-06 23:19:12 +02:00
|
|
|
downloadProgress: action.data.percent,
|
|
|
|
});
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
|
2017-05-23 09:21:21 +02:00
|
|
|
reducers[types.SHOW_SNACKBAR] = function(state, action) {
|
2017-06-06 23:19:12 +02:00
|
|
|
const { message, linkText, linkTarget, isError } = action.data;
|
|
|
|
const snackBar = Object.assign({}, state.snackBar);
|
|
|
|
const snacks = Object.assign([], snackBar.snacks);
|
2017-05-23 09:21:21 +02:00
|
|
|
snacks.push({
|
|
|
|
message,
|
|
|
|
linkText,
|
|
|
|
linkTarget,
|
|
|
|
isError,
|
2017-06-06 23:19:12 +02:00
|
|
|
});
|
2017-05-23 09:21:21 +02:00
|
|
|
const newSnackBar = Object.assign({}, snackBar, {
|
|
|
|
snacks,
|
2017-06-06 23:19:12 +02:00
|
|
|
});
|
2017-05-23 09:21:21 +02:00
|
|
|
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
snackBar: newSnackBar,
|
2017-06-06 23:19:12 +02:00
|
|
|
});
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
2017-05-23 09:21:21 +02:00
|
|
|
|
|
|
|
reducers[types.REMOVE_SNACKBAR_SNACK] = function(state, action) {
|
2017-06-06 23:19:12 +02:00
|
|
|
const snackBar = Object.assign({}, state.snackBar);
|
|
|
|
const snacks = Object.assign([], snackBar.snacks);
|
|
|
|
snacks.shift();
|
2017-05-23 09:21:21 +02:00
|
|
|
|
|
|
|
const newSnackBar = Object.assign({}, snackBar, {
|
|
|
|
snacks,
|
2017-06-06 23:19:12 +02:00
|
|
|
});
|
2017-05-23 09:21:21 +02:00
|
|
|
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
snackBar: newSnackBar,
|
2017-06-06 23:19:12 +02:00
|
|
|
});
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
2017-05-23 09:21:21 +02:00
|
|
|
|
2017-06-24 10:57:37 +02:00
|
|
|
reducers[types.DOWNLOADING_COMPLETED] = function(state, action) {
|
|
|
|
const badgeNumber = state.badgeNumber;
|
|
|
|
|
|
|
|
// Don't update the badge number if the window is focused
|
2017-10-13 22:33:37 +02:00
|
|
|
if (win && win.isFocused()) return Object.assign({}, state);
|
2017-06-24 10:57:37 +02:00
|
|
|
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
badgeNumber: badgeNumber + 1,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
reducers[types.WINDOW_FOCUSED] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
badgeNumber: 0,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-08-25 21:05:00 +02:00
|
|
|
reducers[types.VOLUME_CHANGED] = function(state, action) {
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
volume: action.data.volume,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-11-29 23:45:49 +01:00
|
|
|
export default function reducer(state: appState = defaultState, action: any) {
|
2017-04-07 07:15:22 +02:00
|
|
|
const handler = reducers[action.type];
|
|
|
|
if (handler) return handler(state, action);
|
|
|
|
return state;
|
|
|
|
}
|