lbry-desktop/ui/js/selectors/app.js
2017-08-29 14:40:30 -04:00

273 lines
6.5 KiB
JavaScript

import { createSelector } from "reselect";
import { parseQueryParams, toQueryString } from "util/query_params";
import * as settings from "constants/settings.js";
import lbry from "lbry";
import lbryuri from "lbryuri";
export const _selectState = state => state.app || {};
export const selectIsLoaded = createSelector(
_selectState,
state => state.isLoaded
);
export const selectCurrentPath = createSelector(
_selectState,
state => state.currentPath
);
export const selectCurrentPage = createSelector(selectCurrentPath, path => {
return path.replace(/^\//, "").split("?")[0];
});
export const selectCurrentParams = createSelector(selectCurrentPath, path => {
if (path === undefined) return {};
if (!path.match(/\?/)) return {};
return parseQueryParams(path.split("?")[1]);
});
export const selectPageTitle = createSelector(
selectCurrentPage,
selectCurrentParams,
(page, params) => {
switch (page) {
case "settings":
return __("Settings");
case "report":
return __("Report");
case "wallet":
return __("Wallet");
case "send":
return __("Send Credits");
case "receive":
return __("Receive Credits");
case "backup":
return __("Backup Your Wallet");
case "rewards":
return __("Rewards");
case "invite":
return __("Invites");
case "start":
return __("Start");
case "publish":
return __("Publish");
case "help":
return __("Help");
case "developer":
return __("Developer");
case "search":
return params.query
? __("Search results for %s", params.query)
: __("Search");
case "show": {
const parts = [lbryuri.normalize(params.uri)];
// If the params has any keys other than "uri"
if (Object.keys(params).length > 1) {
parts.push(toQueryString(Object.assign({}, params, { uri: null })));
}
return parts.join("?");
}
case "downloaded":
return __("Downloads & Purchases");
case "published":
return __("Publishes");
case "discover":
return __("Home");
case false:
case null:
case "":
return "";
default:
return page[0].toUpperCase() + (page.length > 0 ? page.substr(1) : "");
}
}
);
export const selectPlatform = createSelector(
_selectState,
state => state.platform
);
export const selectUpdateUrl = createSelector(selectPlatform, platform => {
switch (platform) {
case "darwin":
return "https://lbry.io/get/lbry.dmg";
case "linux":
return "https://lbry.io/get/lbry.deb";
case "win32":
return "https://lbry.io/get/lbry.exe";
default:
throw "Unknown platform";
}
});
export const selectVersion = createSelector(_selectState, state => {
return state.version;
});
export const selectUpgradeFilename = createSelector(
selectPlatform,
selectVersion,
(platform, version) => {
switch (platform) {
case "darwin":
return `LBRY_${version}.dmg`;
case "linux":
return `LBRY_${version}_amd64.deb`;
case "win32":
return `LBRY_${version}.exe`;
default:
throw "Unknown platform";
}
}
);
export const selectCurrentModal = createSelector(
_selectState,
state => state.modal
);
export const selectDownloadProgress = createSelector(
_selectState,
state => state.downloadProgress
);
export const selectDownloadComplete = createSelector(
_selectState,
state => state.upgradeDownloadCompleted
);
export const selectHeaderLinks = createSelector(selectCurrentPage, page => {
// This contains intentional fall throughs
switch (page) {
case "wallet":
case "history":
case "send":
case "receive":
case "invite":
case "rewards":
case "backup":
return {
wallet: __("Overview"),
rewards: __("Rewards"),
invite: __("Invites"),
history: __("History"),
send: __("Send"),
receive: __("Receive"),
};
case "downloaded":
case "published":
return {
downloaded: __("Downloaded"),
published: __("Published"),
};
case "settings":
case "help":
return {
settings: __("Settings"),
help: __("Help"),
};
default:
return null;
}
});
export const selectUpgradeSkipped = createSelector(
_selectState,
state => state.upgradeSkipped
);
export const selectUpgradeDownloadPath = createSelector(
_selectState,
state => state.downloadPath
);
export const selectUpgradeDownloadItem = createSelector(
_selectState,
state => state.downloadItem
);
export const selectModalExtraContent = createSelector(
_selectState,
state => state.modalExtraContent
);
export const selectDaemonReady = createSelector(
_selectState,
state => state.daemonReady
);
export const selectDaemonVersionMatched = createSelector(
_selectState,
state => state.daemonVersionMatched
);
export const selectSnackBar = createSelector(
_selectState,
state => state.snackBar || {}
);
export const selectSnackBarSnacks = createSelector(
selectSnackBar,
snackBar => snackBar.snacks || []
);
export const selectCreditsIntroAcknowledged = createSelector(
_selectState,
state => lbry.getClientSetting(settings.CREDIT_INTRO_ACKNOWLEDGED)
);
export const selectWelcomeModalAcknowledged = createSelector(
_selectState,
state => lbry.getClientSetting(settings.FIRST_RUN_ACKNOWLEDGED)
);
export const selectBadgeNumber = createSelector(
_selectState,
state => state.badgeNumber
);
export const selectCurrentLanguage = createSelector(
_selectState,
() => app.i18n.getLocale() || "en"
);
export const selectPathAfterAuth = createSelector(
_selectState,
state => state.pathAfterAuth
);
export const selectIsBackDisabled = createSelector(
_selectState,
state => state.isBackDisabled
);
export const selectIsForwardDisabled = createSelector(
_selectState,
state => state.isForwardDisabled
);
export const selectHistoryBack = createSelector(_selectState, state => {
const { history } = state;
const index = history.index - 1;
// Check if page exists
if (index > -1) {
// Get back history
return history.stack[index];
}
});
export const selectHistoryForward = createSelector(_selectState, state => {
const { history } = state;
const index = history.index + 1;
// Check if page exists
if (index <= history.stack.length) {
// Get forward history
return history.stack[index];
}
});
export const selectVolume = createSelector(_selectState, state => state.volume);