2017-12-21 18:32:51 +01:00
|
|
|
import { createSelector } from 'reselect';
|
|
|
|
import { parseQueryParams, toQueryString } from 'util/query_params';
|
|
|
|
import Lbryuri from 'lbryuri';
|
2017-08-30 14:48:32 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
export const selectState = state => state.navigation || {};
|
2017-08-30 14:48:32 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
export const selectCurrentPath = createSelector(selectState, state => state.currentPath);
|
2017-08-30 14:48:32 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
export const computePageFromPath = path => path.replace(/^\//, '').split('?')[0];
|
2017-09-17 20:26:55 +02:00
|
|
|
|
2017-12-13 22:36:30 +01:00
|
|
|
export const selectCurrentPage = createSelector(selectCurrentPath, path =>
|
|
|
|
computePageFromPath(path)
|
|
|
|
);
|
2017-08-30 14:48:32 +02:00
|
|
|
|
|
|
|
export const selectCurrentParams = createSelector(selectCurrentPath, path => {
|
|
|
|
if (path === undefined) return {};
|
|
|
|
if (!path.match(/\?/)) return {};
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
return parseQueryParams(path.split('?')[1]);
|
2017-08-30 14:48:32 +02:00
|
|
|
});
|
|
|
|
|
2017-12-13 22:36:30 +01:00
|
|
|
export const makeSelectCurrentParam = param =>
|
2017-12-21 18:32:51 +01:00
|
|
|
createSelector(selectCurrentParams, params => (params ? params[param] : undefined));
|
2017-09-07 23:18:33 +02:00
|
|
|
|
2017-08-30 14:48:32 +02:00
|
|
|
export const selectHeaderLinks = createSelector(selectCurrentPage, page => {
|
|
|
|
// This contains intentional fall throughs
|
|
|
|
switch (page) {
|
2017-12-21 18:32:51 +01:00
|
|
|
case 'wallet':
|
|
|
|
case 'history':
|
|
|
|
case 'send':
|
|
|
|
case 'getcredits':
|
|
|
|
case 'invite':
|
|
|
|
case 'rewards':
|
|
|
|
case 'backup':
|
2017-08-30 14:48:32 +02:00
|
|
|
return {
|
2017-12-21 18:32:51 +01:00
|
|
|
wallet: __('Overview'),
|
|
|
|
getcredits: __('Get Credits'),
|
|
|
|
send: __('Send / Receive'),
|
|
|
|
rewards: __('Rewards'),
|
|
|
|
invite: __('Invites'),
|
|
|
|
history: __('History'),
|
2017-08-30 14:48:32 +02:00
|
|
|
};
|
2017-12-21 18:32:51 +01:00
|
|
|
case 'downloaded':
|
|
|
|
case 'published':
|
2017-08-30 14:48:32 +02:00
|
|
|
return {
|
2017-12-21 18:32:51 +01:00
|
|
|
downloaded: __('Downloaded'),
|
|
|
|
published: __('Published'),
|
2017-08-30 14:48:32 +02:00
|
|
|
};
|
2017-12-21 18:32:51 +01:00
|
|
|
case 'settings':
|
|
|
|
case 'help':
|
2017-08-30 14:48:32 +02:00
|
|
|
return {
|
2017-12-21 18:32:51 +01:00
|
|
|
settings: __('Settings'),
|
|
|
|
help: __('Help'),
|
2017-08-30 14:48:32 +02:00
|
|
|
};
|
2017-12-21 18:32:51 +01:00
|
|
|
case 'discover':
|
|
|
|
case 'subscriptions':
|
2017-12-08 21:14:35 +01:00
|
|
|
return {
|
2017-12-21 18:32:51 +01:00
|
|
|
discover: __('Discover'),
|
|
|
|
subscriptions: __('Subscriptions'),
|
2017-12-08 21:14:35 +01:00
|
|
|
};
|
2017-08-30 14:48:32 +02:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export const selectPageTitle = createSelector(
|
|
|
|
selectCurrentPage,
|
|
|
|
selectCurrentParams,
|
|
|
|
(page, params) => {
|
|
|
|
switch (page) {
|
2017-12-21 18:32:51 +01:00
|
|
|
case 'show': {
|
|
|
|
const parts = [Lbryuri.normalize(params.uri)];
|
2017-08-30 14:48:32 +02:00
|
|
|
// If the params has any keys other than "uri"
|
|
|
|
if (Object.keys(params).length > 1) {
|
|
|
|
parts.push(toQueryString(Object.assign({}, params, { uri: null })));
|
|
|
|
}
|
2017-12-21 18:32:51 +01:00
|
|
|
return parts.join('?');
|
2017-08-30 14:48:32 +02:00
|
|
|
}
|
2017-12-21 18:32:51 +01:00
|
|
|
case 'discover':
|
2018-01-04 06:05:20 +01:00
|
|
|
return __('Discover');
|
2017-08-30 20:39:20 +02:00
|
|
|
case false:
|
|
|
|
case null:
|
2017-12-21 18:32:51 +01:00
|
|
|
case '':
|
|
|
|
return '';
|
2017-08-30 20:39:20 +02:00
|
|
|
default:
|
2018-01-04 06:05:20 +01:00
|
|
|
return '';
|
2017-08-30 14:48:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
export const selectPathAfterAuth = createSelector(selectState, state => state.pathAfterAuth);
|
2017-08-30 14:48:32 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
export const selectIsBackDisabled = createSelector(selectState, state => state.index === 0);
|
2017-08-30 14:48:32 +02:00
|
|
|
|
|
|
|
export const selectIsForwardDisabled = createSelector(
|
2017-12-21 18:32:51 +01:00
|
|
|
selectState,
|
2017-08-30 14:48:32 +02:00
|
|
|
state => state.index === state.stack.length - 1
|
|
|
|
);
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
export const selectHistoryIndex = createSelector(selectState, state => state.index);
|
2017-08-30 14:48:32 +02:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
export const selectHistoryStack = createSelector(selectState, state => state.stack);
|
2017-11-17 22:27:35 +01:00
|
|
|
|
|
|
|
// returns current page attributes (scrollY, path)
|
|
|
|
export const selectActiveHistoryEntry = createSelector(
|
2017-12-21 18:32:51 +01:00
|
|
|
selectState,
|
2017-11-17 22:27:35 +01:00
|
|
|
state => state.stack[state.index]
|
|
|
|
);
|