lbry-desktop/src/ui/redux/selectors/content.js

88 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-03-05 05:46:57 +01:00
// @flow
import { createSelector } from 'reselect';
import {
makeSelectClaimForUri,
selectClaimsByUri,
2019-03-28 17:53:13 +01:00
makeSelectClaimsInChannelForCurrentPageState,
2019-03-05 05:46:57 +01:00
} from 'lbry-redux';
2019-04-01 01:04:01 +02:00
const RECENT_HISTORY_AMOUNT = 10;
const HISTORY_ITEMS_PER_PAGE = 50;
2019-03-05 05:46:57 +01:00
export const selectState = (state: any) => state.content || {};
export const selectPlayingUri = createSelector(
selectState,
state => state.playingUri
);
export const selectRewardContentClaimIds = createSelector(
selectState,
state => state.rewardedContentClaimIds
);
export const makeSelectContentPositionForUri = (uri: string) =>
createSelector(
selectState,
makeSelectClaimForUri(uri),
(state, claim) => {
if (!claim) {
return null;
}
const outpoint = `${claim.txid}:${claim.nout}`;
const id = claim.claim_id;
return state.positions[id] ? state.positions[id][outpoint] : null;
}
);
2019-04-01 01:04:01 +02:00
export const selectHistory = createSelector(
2019-03-05 05:46:57 +01:00
selectState,
2019-04-01 01:04:01 +02:00
state => state.history || []
);
export const selectHistoryPageCount = createSelector(
selectHistory,
history => Math.ceil(history.length / HISTORY_ITEMS_PER_PAGE)
2019-03-05 05:46:57 +01:00
);
export const makeSelectHistoryForPage = (page: number) =>
createSelector(
2019-04-01 01:04:01 +02:00
selectHistory,
2019-03-05 05:46:57 +01:00
selectClaimsByUri,
2019-04-01 01:04:01 +02:00
(history, claimsByUri) => {
2019-03-05 05:46:57 +01:00
const left = page * HISTORY_ITEMS_PER_PAGE;
2019-04-01 01:04:01 +02:00
const historyItemsForPage = history.slice(left, left + HISTORY_ITEMS_PER_PAGE);
return historyItemsForPage;
2019-03-05 05:46:57 +01:00
}
);
export const makeSelectHistoryForUri = (uri: string) =>
createSelector(
2019-04-01 01:04:01 +02:00
selectHistory,
history => history.find(i => i.uri === uri)
2019-03-05 05:46:57 +01:00
);
2019-04-01 01:04:01 +02:00
export const selectRecentHistory = createSelector(
selectHistory,
history => {
return history.slice(0, RECENT_HISTORY_AMOUNT);
}
);
2019-03-05 05:46:57 +01:00
export const makeSelectCategoryListUris = (uris: ?Array<string>, channel: string) =>
createSelector(
2019-03-28 17:53:13 +01:00
makeSelectClaimsInChannelForCurrentPageState(channel),
2019-03-05 05:46:57 +01:00
channelClaims => {
if (uris) return uris;
if (channelClaims) {
const CATEGORY_LIST_SIZE = 10;
return channelClaims
.slice(0, CATEGORY_LIST_SIZE)
.map(({ name, claim_id: claimId }) => `${name}#${claimId}`);
}
return null;
}
);