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

183 lines
6.4 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 {
selectClaimsByUri,
selectClaimIsNsfwForUri,
selectClaimIsMineForUri,
makeSelectContentTypeForUri,
selectClaimForUri,
} from 'redux/selectors/claims';
import { makeSelectMediaTypeForUri, makeSelectFileNameForUri } from 'redux/selectors/file_info';
import { selectBalance } from 'redux/selectors/wallet';
import { selectCostInfoForUri } from 'lbryinc';
2019-08-02 08:28:14 +02:00
import { selectShowMatureContent } from 'redux/selectors/settings';
import * as RENDER_MODES from 'constants/file_render_modes';
import path from 'path';
import { FORCE_CONTENT_TYPE_PLAYER, FORCE_CONTENT_TYPE_COMIC } from 'constants/claim';
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
2022-05-18 08:31:49 +02:00
type State = { claims: any, content: ContentState, user: UserState };
2019-03-05 05:46:57 +01:00
export const selectState = (state: State) => state.content || {};
2019-03-05 05:46:57 +01:00
export const selectPlayingUri = (state: State) => selectState(state).playingUri;
export const selectPrimaryUri = (state: State) => selectState(state).primaryUri;
export const selectListLoop = (state: State) => selectState(state).loopList;
export const selectListShuffle = (state: State) => selectState(state).shuffleList;
export const makeSelectIsPlaying = (uri: string) =>
createSelector(selectPrimaryUri, (primaryUri) => primaryUri === uri);
2019-08-02 08:28:14 +02:00
export const selectIsUriCurrentlyPlaying = createSelector(
(state, uri) => uri,
selectPlayingUri,
(uri, playingUri) => Boolean(playingUri.uri === uri)
);
2022-02-08 13:15:22 +01:00
export const makeSelectIsPlayerFloating = (location: UrlLocation) =>
createSelector(selectPrimaryUri, selectPlayingUri, (primaryUri, playingUri) => {
if (!playingUri.uri) return false;
const { pathname, search } = location;
const hasSecondarySource = Boolean(playingUri.source);
const isComment = playingUri.source === 'comment';
const isInlineSecondaryPlayer =
hasSecondarySource && playingUri.uri !== primaryUri && pathname === playingUri.pathname;
if (isComment && isInlineSecondaryPlayer && search && search !== '?view=discussion') return true;
if (
isInlineSecondaryPlayer ||
(hasSecondarySource && !isComment ? playingUri.primaryUri === primaryUri : playingUri.uri === primaryUri)
) {
return false;
}
return true;
2020-04-14 01:48:11 +02:00
});
export const selectContentPositionForUri = (state: State, uri: string) => {
const claim = selectClaimForUri(state, uri);
if (claim) {
const outpoint = `${claim.txid}:${claim.nout}`;
const id = claim.claim_id;
const positions = selectState(state).positions;
return positions[id] ? positions[id][outpoint] : null;
}
return null;
};
2019-03-05 05:46:57 +01:00
export const selectHistory = (state: State) => selectState(state).history || [];
2019-04-01 01:04:01 +02:00
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(selectHistory, selectClaimsByUri, (history, claimsByUri) => {
const left = page * HISTORY_ITEMS_PER_PAGE;
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(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-07-11 20:06:25 +02:00
export const selectRecentHistory = createSelector(selectHistory, (history) => {
return history.slice(0, RECENT_HISTORY_AMOUNT);
});
2019-04-01 01:04:01 +02:00
export const selectWatchHistoryUris = createSelector(selectHistory, (history) => {
const uris = [];
for (let entry of history) {
if (entry.uri.indexOf('@') !== -1) {
uris.push(entry.uri);
}
}
return uris;
});
export const selectShouldObscurePreviewForUri = (state: State, uri: string) => {
const showMatureContent = selectShowMatureContent(state);
const isClaimMature = selectClaimIsNsfwForUri(state, uri);
return isClaimMature && !showMatureContent;
};
// should probably be in lbry-redux, yarn link was fighting me
export const makeSelectFileExtensionForUri = (uri: string) =>
createSelector(makeSelectFileNameForUri(uri), (fileName) => {
return fileName && path.extname(fileName).substring(1);
});
export const makeSelectFileRenderModeForUri = (uri: string) =>
createSelector(
makeSelectContentTypeForUri(uri),
makeSelectMediaTypeForUri(uri),
makeSelectFileExtensionForUri(uri),
(contentType, mediaType, extension) => {
2022-03-15 17:18:08 +01:00
if (mediaType === 'video' || FORCE_CONTENT_TYPE_PLAYER.includes(contentType) || mediaType === 'livestream') {
return RENDER_MODES.VIDEO;
}
2020-04-03 16:18:07 +02:00
if (mediaType === 'audio') {
return RENDER_MODES.AUDIO;
}
if (mediaType === 'image') {
return RENDER_MODES.IMAGE;
}
if (['md', 'markdown'].includes(extension) || ['text/md', 'text/markdown'].includes(contentType)) {
return RENDER_MODES.MARKDOWN;
}
if (contentType === 'application/pdf') {
return RENDER_MODES.PDF;
}
if (['text/htm', 'text/html'].includes(contentType)) {
return RENDER_MODES.HTML;
}
if (['text', 'document', 'script'].includes(mediaType)) {
return RENDER_MODES.DOCUMENT;
}
if (extension === 'docx') {
return RENDER_MODES.DOCX;
}
// when writing this my local copy of Lbry.getMediaType had '3D-file', but I was receiving model...'
if (['3D-file', 'model'].includes(mediaType)) {
return RENDER_MODES.CAD;
}
// Force content type for fallback support of older claims
if (mediaType === 'comic-book' || FORCE_CONTENT_TYPE_COMIC.includes(contentType)) {
return RENDER_MODES.COMIC;
}
if (
[
'application/zip',
'application/x-gzip',
'application/x-gtar',
'application/x-tgz',
'application/vnd.rar',
'application/x-7z-compressed',
].includes(contentType)
) {
return RENDER_MODES.DOWNLOAD;
}
if (mediaType === 'application') {
return RENDER_MODES.APPLICATION;
}
return RENDER_MODES.UNSUPPORTED;
}
);
2020-01-06 21:57:49 +01:00
export const selectInsufficientCreditsForUri = (state: State, uri: string) => {
const isMine = selectClaimIsMineForUri(state, uri);
const costInfo = selectCostInfoForUri(state, uri);
const balance = selectBalance(state);
return !isMine && costInfo && costInfo.cost > 0 && costInfo.cost > balance;
};