lbry-desktop/ui/js/selectors/file_info.js

111 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import lbry from "lbry";
import { createSelector } from "reselect";
2017-05-01 08:26:09 +02:00
import {
selectClaimsByUri,
selectClaimListMineIsPending,
2017-05-01 08:26:09 +02:00
selectMyClaimsOutpoints,
2017-06-06 23:19:12 +02:00
} from "selectors/claims";
2017-04-28 17:14:44 +02:00
2017-06-06 06:21:55 +02:00
export const _selectState = state => state.fileInfo || {};
2017-04-28 17:14:44 +02:00
export const selectAllFileInfos = createSelector(
_selectState,
2017-06-06 23:19:12 +02:00
state => state.fileInfos || {}
2017-06-06 06:21:55 +02:00
);
2017-04-28 17:14:44 +02:00
export const selectFileListIsPending = createSelector(
_selectState,
2017-06-06 23:19:12 +02:00
state => state.isFileListPending
2017-06-06 06:21:55 +02:00
);
export const selectFileListDownloadedOrPublishedIsPending = createSelector(
selectFileListIsPending,
selectClaimListMineIsPending,
2017-06-06 23:19:12 +02:00
(isFileListPending, isClaimListMinePending) =>
isFileListPending || isClaimListMinePending
2017-06-06 06:21:55 +02:00
);
2017-05-15 05:50:59 +02:00
export const selectFileInfoForUri = (state, props) => {
const claims = selectClaimsByUri(state),
2017-06-06 23:19:12 +02:00
claim = claims[props.uri],
fileInfos = selectAllFileInfos(state),
outpoint = claim ? `${claim.txid}:${claim.nout}` : undefined;
2017-06-06 23:19:12 +02:00
return outpoint && fileInfos ? fileInfos[outpoint] : undefined;
2017-06-06 06:21:55 +02:00
};
export const makeSelectFileInfoForUri = () => {
2017-06-06 23:19:12 +02:00
return createSelector(selectFileInfoForUri, fileInfo => fileInfo);
2017-06-06 06:21:55 +02:00
};
export const selectUrisDownloading = createSelector(
_selectState,
2017-06-06 23:19:12 +02:00
state => state.urisDownloading || {}
2017-06-06 06:21:55 +02:00
);
const selectDownloadingForUri = (state, props) => {
2017-06-06 23:19:12 +02:00
const byUri = selectUrisDownloading(state);
return byUri[props.uri];
2017-06-06 06:21:55 +02:00
};
export const makeSelectDownloadingForUri = () => {
return createSelector(
selectDownloadingForUri,
2017-06-06 23:19:12 +02:00
downloadingForUri => !!downloadingForUri
);
2017-06-06 06:21:55 +02:00
};
export const selectUrisLoading = createSelector(
_selectState,
2017-06-06 23:19:12 +02:00
state => state.urisLoading || {}
2017-06-06 06:21:55 +02:00
);
const selectLoadingForUri = (state, props) => {
2017-06-06 23:19:12 +02:00
const byUri = selectUrisLoading(state);
return byUri[props.uri];
2017-06-06 06:21:55 +02:00
};
export const makeSelectLoadingForUri = () => {
2017-06-06 23:19:12 +02:00
return createSelector(selectLoadingForUri, loading => !!loading);
2017-06-06 06:21:55 +02:00
};
export const selectFileInfosDownloaded = createSelector(
selectAllFileInfos,
selectMyClaimsOutpoints,
(fileInfos, myClaimOutpoints) => {
2017-06-06 23:19:12 +02:00
const fileInfoList = [];
Object.values(fileInfos).forEach(fileInfo => {
2017-06-06 23:19:12 +02:00
if (
fileInfo &&
myClaimOutpoints.indexOf(fileInfo.outpoint) === -1 &&
(fileInfo.completed || fileInfo.written_bytes)
) {
fileInfoList.push(fileInfo);
}
2017-06-06 23:19:12 +02:00
});
return fileInfoList;
}
2017-06-06 06:21:55 +02:00
);
2017-05-01 08:26:09 +02:00
export const selectFileInfosPendingPublish = createSelector(
_selectState,
2017-06-06 23:19:12 +02:00
state => {
return lbry.getPendingPublishes();
}
2017-06-06 06:21:55 +02:00
);
export const selectFileInfosPublished = createSelector(
selectAllFileInfos,
selectFileInfosPendingPublish,
2017-05-01 08:26:09 +02:00
selectMyClaimsOutpoints,
(allFileInfos, pendingFileInfos, outpoints) => {
2017-06-06 23:19:12 +02:00
const fileInfos = [];
2017-05-01 08:26:09 +02:00
outpoints.forEach(outpoint => {
if (allFileInfos[outpoint]) {
2017-06-06 23:19:12 +02:00
fileInfos.push(allFileInfos[outpoint]);
}
2017-06-06 23:19:12 +02:00
});
return [...fileInfos, ...pendingFileInfos];
2017-05-01 08:26:09 +02:00
}
2017-06-06 06:21:55 +02:00
);