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

342 lines
8.2 KiB
JavaScript
Raw Normal View History

2017-11-29 09:57:37 +01:00
// @flow
2017-04-07 07:15:22 +02:00
import * as ACTIONS from 'constants/action_types';
import { remote } from 'electron';
2017-11-29 09:57:37 +01:00
2019-02-22 07:59:50 +01:00
// @if TARGET='app'
const win = remote.BrowserWindow.getFocusedWindow();
2019-02-22 07:59:50 +01:00
// @endif
2017-06-06 06:21:55 +02:00
const reducers = {};
2017-11-30 07:53:23 +01:00
export type SnackBar = {
message: string,
linkText: string,
linkTarget: string,
isError: boolean,
};
export type AppState = {
2017-11-29 09:57:37 +01:00
isLoaded: boolean,
2017-11-30 07:53:23 +01:00
modal: ?string,
2017-11-29 09:57:37 +01:00
modalProps: mixed,
platform: string,
upgradeSkipped: boolean,
daemonVersionMatched: ?boolean,
2017-11-29 09:57:37 +01:00
daemonReady: boolean,
hasSignature: boolean,
badgeNumber: number,
volume: number,
autoUpdateDeclined: boolean,
modalsAllowed: boolean,
2017-11-30 07:53:23 +01:00
downloadProgress: ?number,
upgradeDownloading: ?boolean,
upgradeDownloadComplete: ?boolean,
checkUpgradeTimer: ?number,
isUpgradeAvailable: ?boolean,
isUpgradeSkipped: ?boolean,
2018-11-07 23:44:38 +01:00
hasClickedComment: boolean,
2019-05-30 15:20:28 +02:00
enhancedLayout: boolean,
2020-11-09 18:22:38 +01:00
splashAnimationEnabled: boolean,
searchOptionsExpanded: boolean,
2019-08-20 14:29:59 +02:00
isPasswordSaved: boolean,
welcomeVersion: number,
allowAnalytics: boolean,
hasNavigated: boolean,
interestedInYoutubeSync: boolean,
activeChannel: ?string,
incognito: boolean,
2017-11-30 07:53:23 +01:00
};
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,
daemonVersionMatched: null,
2017-04-22 15:17:01 +02:00
daemonReady: false,
hasSignature: false,
badgeNumber: 0,
2020-01-27 22:37:54 +01:00
volume: 1,
// @if TARGET='app'
upgradeSkipped: sessionStorage.getItem('upgradeSkipped') === 'true',
// @endif
muted: false,
autoUpdateDownloaded: false,
autoUpdateDeclined: false,
modalsAllowed: true,
2018-11-07 23:44:38 +01:00
hasClickedComment: false,
2017-11-30 07:53:23 +01:00
downloadProgress: undefined,
upgradeDownloading: undefined,
upgradeDownloadComplete: undefined,
checkUpgradeTimer: undefined,
isUpgradeAvailable: undefined,
isUpgradeSkipped: undefined,
2019-01-23 16:38:40 +01:00
enhancedLayout: false,
2020-11-09 18:22:38 +01:00
splashAnimationEnabled: true,
searchOptionsExpanded: false,
2019-07-17 22:49:06 +02:00
currentScroll: 0,
scrollHistory: [0],
2019-08-20 14:29:59 +02:00
isPasswordSaved: false,
welcomeVersion: 0.0,
allowAnalytics: false,
hasNavigated: false,
interestedInYoutubeSync: false,
activeChannel: undefined,
incognito: false,
2019-07-17 22:49:06 +02:00
};
2019-07-23 01:43:30 +02:00
// @@router comes from react-router
// This action is dispatched any time a user navigates forward or back
2020-01-27 22:37:54 +01:00
try {
defaultState.volume = Number(sessionStorage.getItem('volume') || 1);
2020-01-27 22:37:54 +01:00
} catch (e) {}
2019-07-17 22:49:06 +02:00
reducers['@@router/LOCATION_CHANGE'] = (state, action) => {
const { currentScroll } = state;
2019-07-23 10:05:51 +02:00
const scrollHistory = (state.scrollHistory && state.scrollHistory.slice()) || [];
2019-07-17 22:49:06 +02:00
const { action: name } = action.payload;
let newCurrentScroll = currentScroll;
if (name === 'PUSH') {
scrollHistory.push(window.scrollY);
newCurrentScroll = 0;
} else if (name === 'POP') {
newCurrentScroll = scrollHistory[scrollHistory.length - 1];
scrollHistory.pop();
}
return {
...state,
scrollHistory,
currentScroll: newCurrentScroll,
};
2017-06-06 06:21:55 +02:00
};
2017-04-07 07:15:22 +02:00
reducers[ACTIONS.DAEMON_READY] = (state) =>
Object.assign({}, state, {
2017-05-02 05:57:50 +02:00
daemonReady: true,
2017-06-06 23:19:12 +02:00
});
2017-05-02 05:57:50 +02:00
2019-08-20 14:29:59 +02:00
reducers[ACTIONS.PASSWORD_SAVED] = (state, action) =>
Object.assign({}, state, {
isPasswordSaved: action.data,
});
reducers[ACTIONS.DAEMON_VERSION_MATCH] = (state) =>
Object.assign({}, state, {
daemonVersionMatched: true,
});
reducers[ACTIONS.DAEMON_VERSION_MISMATCH] = (state) =>
Object.assign({}, state, {
daemonVersionMatched: false,
});
2017-05-02 05:57:50 +02:00
reducers[ACTIONS.UPGRADE_CANCELLED] = (state) =>
Object.assign({}, state, {
2017-04-07 07:15:22 +02:00
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-04-07 07:15:22 +02:00
reducers[ACTIONS.AUTO_UPDATE_DOWNLOADED] = (state) =>
Object.assign({}, state, {
autoUpdateDownloaded: true,
});
reducers[ACTIONS.AUTO_UPDATE_DECLINED] = (state) =>
2018-04-18 06:03:01 +02:00
Object.assign({}, state, {
autoUpdateDeclined: true,
});
reducers[ACTIONS.UPGRADE_DOWNLOAD_COMPLETED] = (state, action) =>
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-04-07 07:15:22 +02:00
reducers[ACTIONS.UPGRADE_DOWNLOAD_STARTED] = (state) =>
Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
upgradeDownloading: true,
});
2017-04-07 07:15:22 +02:00
reducers[ACTIONS.CHANGE_MODALS_ALLOWED] = (state, action) =>
Object.assign({}, state, {
modalsAllowed: action.data.modalsAllowed,
});
reducers[ACTIONS.SKIP_UPGRADE] = (state) => {
sessionStorage.setItem('upgradeSkipped', 'true');
2017-04-07 07:15:22 +02:00
return Object.assign({}, state, {
isUpgradeSkipped: true,
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[ACTIONS.MEDIA_PLAY] = (state) =>
2018-04-18 06:03:01 +02:00
Object.assign({}, state, {
modalsAllowed: false,
});
reducers[ACTIONS.MEDIA_PAUSE] = (state) =>
2018-04-18 06:03:01 +02:00
Object.assign({}, state, {
modalsAllowed: true,
});
reducers[ACTIONS.SET_PLAYING_URI] = (state, action) => {
if (action.data.uri === null) {
return Object.assign({}, state, {
modalsAllowed: true,
});
}
2018-03-26 23:32:43 +02:00
return state;
};
reducers[ACTIONS.UPDATE_VERSION] = (state, action) =>
Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
version: action.data.version,
});
2017-04-07 07:15:22 +02:00
reducers[ACTIONS.CHECK_UPGRADE_SUCCESS] = (state, action) =>
Object.assign({}, state, {
isUpgradeAvailable: action.data.upgradeAvailable,
remoteVersion: action.data.remoteVersion,
});
reducers[ACTIONS.CHECK_UPGRADE_SUBSCRIBE] = (state, action) =>
Object.assign({}, state, {
checkUpgradeTimer: action.data.checkUpgradeTimer,
});
reducers[ACTIONS.UPGRADE_DOWNLOAD_PROGRESSED] = (state, action) =>
Object.assign({}, state, {
2017-06-06 23:19:12 +02:00
downloadProgress: action.data.percent,
});
2017-04-07 07:15:22 +02:00
reducers[ACTIONS.DOWNLOADING_COMPLETED] = (state) => {
const { badgeNumber } = state;
// Don't update the badge number if the window is focused
2019-02-22 07:59:50 +01:00
// @if TARGET='app'
if (win && win.isFocused()) return Object.assign({}, state);
2019-02-22 07:59:50 +01:00
// @endif
return Object.assign({}, state, {
badgeNumber: badgeNumber + 1,
});
};
reducers[ACTIONS.WINDOW_FOCUSED] = (state) =>
Object.assign({}, state, {
badgeNumber: 0,
});
reducers[ACTIONS.VOLUME_CHANGED] = (state, action) =>
Object.assign({}, state, {
volume: action.data.volume,
});
reducers[ACTIONS.VOLUME_MUTED] = (state, action) =>
Object.assign({}, state, {
muted: action.data.muted,
});
reducers[ACTIONS.HISTORY_NAVIGATE] = (state) =>
2018-03-26 23:32:43 +02:00
Object.assign({}, state, {
modal: undefined,
modalProps: {},
});
reducers[ACTIONS.CLEAR_UPGRADE_TIMER] = (state) =>
Object.assign({}, state, {
checkUpgradeTimer: undefined,
});
reducers[ACTIONS.ADD_COMMENT] = (state) =>
2018-11-07 23:44:38 +01:00
Object.assign({}, state, {
hasClickedComment: true,
});
reducers[ACTIONS.SHOW_MODAL] = (state, action) =>
Object.assign({}, state, {
modal: action.data.id,
modalProps: action.data.modalProps,
});
reducers[ACTIONS.SET_WELCOME_VERSION] = (state, action) =>
Object.assign({}, state, {
welcomeVersion: action.data,
});
reducers[ACTIONS.SET_ALLOW_ANALYTICS] = (state, action) =>
Object.assign({}, state, {
allowAnalytics: action.data,
});
reducers[ACTIONS.SET_HAS_NAVIGATED] = (state, action) =>
Object.assign({}, state, {
hasNavigated: action.data,
});
reducers[ACTIONS.HIDE_MODAL] = (state) =>
Object.assign({}, state, {
modal: null,
modalProps: null,
});
reducers[ACTIONS.TOGGLE_SEARCH_EXPANDED] = (state) =>
Object.assign({}, state, {
searchOptionsExpanded: !state.searchOptionsExpanded,
});
reducers[ACTIONS.TOGGLE_YOUTUBE_SYNC_INTEREST] = (state, action) => {
return {
...state,
interestedInYoutubeSync: !state.interestedInYoutubeSync,
};
};
2020-11-09 18:22:38 +01:00
reducers[ACTIONS.TOGGLE_SPLASH_ANIMATION] = (state, action) => {
return {
...state,
splashAnimationEnabled: !state.splashAnimationEnabled,
};
};
reducers[ACTIONS.SET_ACTIVE_CHANNEL] = (state, action) => {
return {
...state,
activeChannel: action.data.claimId,
};
};
reducers[ACTIONS.SET_INCOGNITO] = (state, action) => {
return {
...state,
incognito: action.data.enabled,
};
};
reducers[ACTIONS.USER_STATE_POPULATE] = (state, action) => {
const { welcomeVersion, allowAnalytics } = action.data;
return {
...state,
...(welcomeVersion !== undefined ? { welcomeVersion } : {}),
...(allowAnalytics !== undefined ? { allowAnalytics } : {}),
};
};
reducers[ACTIONS.PURCHASE_URI_FAILED] = (state, action) => {
2020-05-21 17:38:28 +02:00
return {
...state,
modal: null,
modalProps: null,
};
};
2017-11-30 07:53:23 +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;
}