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

86 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

2020-12-08 22:50:10 +01:00
import { selectClaimsByUri, selectIsFetchingClaimListMine, selectMyClaims } from 'lbry-redux';
2019-03-05 05:46:57 +01:00
import { createSelector } from 'reselect';
export const selectState = state => state.fileInfo || {};
2020-12-08 22:50:10 +01:00
export const selectFileInfosByOutpoint = createSelector(selectState, state => state.byOutpoint || {});
2019-03-05 05:46:57 +01:00
2020-12-08 22:50:10 +01:00
export const selectIsFetchingFileList = createSelector(selectState, state => state.isFetchingFileList);
2019-03-05 05:46:57 +01:00
export const selectIsFetchingFileListDownloadedOrPublished = createSelector(
selectIsFetchingFileList,
selectIsFetchingClaimListMine,
(isFetchingFileList, isFetchingClaimListMine) => isFetchingFileList || isFetchingClaimListMine
);
export const makeSelectFileInfoForUri = uri =>
2020-12-08 22:50:10 +01:00
createSelector(selectClaimsByUri, selectFileInfosByOutpoint, (claims, byOutpoint) => {
const claim = claims[uri];
const outpoint = claim ? `${claim.txid}:${claim.nout}` : undefined;
return outpoint ? byOutpoint[outpoint] : undefined;
});
export const selectDownloadingByOutpoint = createSelector(selectState, state => state.downloadingByOutpoint || {});
2019-03-05 05:46:57 +01:00
export const makeSelectDownloadingForUri = uri =>
2020-12-08 22:50:10 +01:00
createSelector(selectDownloadingByOutpoint, makeSelectFileInfoForUri(uri), (byOutpoint, fileInfo) => {
if (!fileInfo) return false;
return byOutpoint[fileInfo.outpoint];
});
2019-03-05 05:46:57 +01:00
2020-12-08 22:50:10 +01:00
export const selectUrisLoading = createSelector(selectState, state => state.urisLoading || {});
export const makeSelectLoadingForUri = uri => createSelector(selectUrisLoading, byUri => byUri && byUri[uri]);
2019-03-05 05:46:57 +01:00
export const selectFileInfosDownloaded = createSelector(
selectFileInfosByOutpoint,
selectMyClaims,
(byOutpoint, myClaims) =>
Object.values(byOutpoint).filter(fileInfo => {
const myClaimIds = myClaims.map(claim => claim.claim_id);
2019-05-07 23:38:29 +02:00
return fileInfo && myClaimIds.indexOf(fileInfo.claim_id) === -1 && (fileInfo.completed || fileInfo.written_bytes);
2019-03-05 05:46:57 +01:00
})
);
// export const selectFileInfoForUri = (state, props) => {
// const claims = selectClaimsByUri(state),
// claim = claims[props.uri],
// fileInfos = selectAllFileInfos(state),
// outpoint = claim ? `${claim.txid}:${claim.nout}` : undefined;
// return outpoint && fileInfos ? fileInfos[outpoint] : undefined;
// };
export const selectDownloadingFileInfos = createSelector(
selectDownloadingByOutpoint,
selectFileInfosByOutpoint,
(downloadingByOutpoint, fileInfosByOutpoint) => {
const outpoints = Object.keys(downloadingByOutpoint);
const fileInfos = [];
outpoints.forEach(outpoint => {
const fileInfo = fileInfosByOutpoint[outpoint];
if (fileInfo) fileInfos.push(fileInfo);
});
return fileInfos;
}
);
2020-12-08 22:50:10 +01:00
export const selectTotalDownloadProgress = createSelector(selectDownloadingFileInfos, fileInfos => {
const progress = [];
2019-03-05 05:46:57 +01:00
2020-12-08 22:50:10 +01:00
fileInfos.forEach(fileInfo => {
progress.push((fileInfo.written_bytes / fileInfo.total_bytes) * 100);
});
2019-03-05 05:46:57 +01:00
2020-12-08 22:50:10 +01:00
const totalProgress = progress.reduce((a, b) => a + b, 0);
2019-03-05 05:46:57 +01:00
2020-12-08 22:50:10 +01:00
if (fileInfos.length > 0) return totalProgress / fileInfos.length / 100.0;
return -1;
});
2019-03-05 05:46:57 +01:00
2020-12-08 22:50:10 +01:00
export const selectFileInfoErrors = createSelector(selectState, state => state.errors || {});