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

234 lines
5.5 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import * as types from "constants/action_types";
import * as modalTypes from "constants/modal_types";
2017-04-07 07:15:22 +02:00
2017-06-22 22:11:52 +02:00
const currentPath = () => {
const hash = document.location.hash;
if (hash !== "") return hash.replace(/^#/, "");
2017-06-22 22:11:52 +02:00
else return "/discover";
};
const { remote } = require("electron");
const application = remote.app;
const win = remote.BrowserWindow.getFocusedWindow();
2017-06-06 06:21:55 +02:00
const reducers = {};
2017-04-07 07:15:22 +02:00
const defaultState = {
isLoaded: false,
isBackDisabled: true,
isForwardDisabled: true,
2017-06-22 22:11:52 +02:00
currentPath: currentPath(),
2017-07-19 17:09:40 +02:00
pathAfterAuth: "/discover",
2017-04-07 07:15:22 +02:00
platform: process.platform,
2017-06-06 23:19:12 +02:00
upgradeSkipped: sessionStorage.getItem("upgradeSkipped"),
daemonVersionMatched: null,
2017-04-22 15:17:01 +02:00
daemonReady: false,
hasSignature: false,
badgeNumber: 0,
history: { index: 0, stack: [] },
volume: 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) {
const { history } = state;
const { page } = action.data;
history.stack.push(page);
2017-05-02 05:57:50 +02:00
return Object.assign({}, state, {
daemonReady: true,
history,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
2017-05-02 05:57:50 +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-05-06 09:25:14 +02:00
reducers[types.CHANGE_PATH] = function(state, action) {
2017-04-07 07:15:22 +02:00
return Object.assign({}, state, {
currentPath: action.data.path,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
2017-04-07 07:15:22 +02:00
2017-07-19 17:09:40 +02:00
reducers[types.CHANGE_AFTER_AUTH_PATH] = function(state, action) {
return Object.assign({}, state, {
pathAfterAuth: action.data.path,
});
};
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, {
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-06-06 23:19:12 +02:00
sessionStorage.setItem("upgradeSkipped", true);
2017-04-07 07:15:22 +02:00
return Object.assign({}, state, {
upgradeSkipped: 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
reducers[types.OPEN_MODAL] = function(state, action) {
return Object.assign({}, state, {
modal: action.data.modal,
2017-06-06 23:19:12 +02:00
modalExtraContent: action.data.extraContent,
});
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-06-06 23:19:12 +02:00
modalExtraContent: undefined,
});
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
reducers[types.DOWNLOADING_COMPLETED] = function(state, action) {
const badgeNumber = state.badgeNumber;
// Don't update the badge number if the window is focused
if (win.isFocused()) return Object.assign({}, state);
return Object.assign({}, state, {
badgeNumber: badgeNumber + 1,
});
};
reducers[types.WINDOW_FOCUSED] = function(state, action) {
return Object.assign({}, state, {
badgeNumber: 0,
});
};
reducers[types.HISTORY_NAVIGATE] = (state, action) => {
let page = false;
let location = false;
// Get history from state
const { history } = state;
if (action.data.page) {
// Get page
page = action.data.page;
} else if (action.data.location) {
// Get new location
location = action.data.location;
}
// Add new location to stack
if (location) {
const lastItem = history.stack.length - 1;
// Check for duplicated
let is_duplicate = lastItem > -1
? history.stack[lastItem].location === location
: false;
if (!is_duplicate) {
// Create new page
page = {
index: history.stack.length,
location,
};
// Update index
history.index = history.stack.length;
// Add to stack
history.stack.push(page);
}
} else if (page) {
// Update index
history.index = page.index;
}
return Object.assign({}, state, {
history,
isBackDisabled: history.index === 0, // First page
isForwardDisabled: history.index === history.stack.length - 1, // Last page
});
};
reducers[types.VOLUME_CHANGED] = function(state, action) {
return Object.assign({}, state, {
volume: action.data.volume,
});
};
2017-04-07 07:15:22 +02:00
export default function reducer(state = defaultState, action) {
const handler = reducers[action.type];
if (handler) return handler(state, action);
return state;
}