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

139 lines
3.6 KiB
JavaScript
Raw Normal View History

2019-03-05 05:46:57 +01:00
// @flow
import { createSelector } from 'reselect';
2019-08-02 08:28:14 +02:00
import {
makeSelectClaimForUri,
selectClaimsByUri,
makeSelectClaimsInChannelForCurrentPageState,
makeSelectClaimIsNsfw,
2019-09-09 19:31:00 +02:00
makeSelectRecommendedContentForUri,
makeSelectMediaTypeForUri,
2019-08-02 08:28:14 +02:00
} from 'lbry-redux';
import { selectShowMatureContent } from 'redux/selectors/settings';
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
);
2019-08-02 08:28:14 +02:00
export const makeSelectIsPlaying = (uri: string) =>
createSelector(
selectPlayingUri,
playingUri => playingUri === uri
);
2019-03-05 05:46:57 +01:00
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-07-11 20:06:25 +02:00
export const makeSelectHasVisitedUri = (uri: string) =>
createSelector(
makeSelectHistoryForUri(uri),
history => Boolean(history)
);
2019-09-09 19:31:00 +02:00
export const makeSelectNextUnplayedRecommended = (uri: string) =>
createSelector(
makeSelectRecommendedContentForUri(uri),
selectHistory,
(possibleNext, history) => {
if (possibleNext) {
for (let i = 0; i < possibleNext.length; i++) {
if (!history.find(item => item.uri === possibleNext[i])) {
return possibleNext[i];
}
}
}
}
);
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;
2019-05-07 23:38:29 +02:00
return channelClaims.slice(0, CATEGORY_LIST_SIZE).map(({ name, claim_id: claimId }) => `${name}#${claimId}`);
2019-03-05 05:46:57 +01:00
}
return null;
}
);
2019-08-02 08:28:14 +02:00
export const makeSelectShouldObscurePreview = (uri: string) =>
createSelector(
selectShowMatureContent,
makeSelectClaimIsNsfw(uri),
(showMatureContent, isClaimMature) => {
return isClaimMature && !showMatureContent;
}
);
export const makeSelectCanAutoplay = (uri: string) =>
createSelector(
makeSelectMediaTypeForUri(uri),
mediaType => {
const canAutoPlay = ['audio', 'video', 'image', 'text', 'document'].includes(mediaType);
return canAutoPlay;
}
);
2020-01-06 21:57:49 +01:00
export const makeSelectIsText = (uri: string) =>
createSelector(
makeSelectMediaTypeForUri(uri),
mediaType => {
const isText = ['text', 'document'].includes(mediaType);
return isText;
}
);