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

172 lines
4.3 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import { createSelector } from "reselect";
import lbryuri from "lbryuri";
2017-04-28 17:14:44 +02:00
2017-06-04 13:53:26 +02:00
const _selectState = state => state.claims || {};
2017-04-28 17:14:44 +02:00
2017-06-04 13:53:26 +02:00
export const selectClaimsById = createSelector(
2017-04-28 17:14:44 +02:00
_selectState,
2017-06-10 19:17:37 +02:00
state => state.byId || {}
2017-06-06 06:21:55 +02:00
);
2017-04-28 17:14:44 +02:00
2017-06-04 13:53:26 +02:00
export const selectClaimsByUri = createSelector(
_selectState,
selectClaimsById,
(state, byId) => {
const byUri = state.claimsByUri || {};
const claims = {};
Object.keys(byUri).forEach(uri => {
const claimId = byUri[uri];
2017-06-05 10:48:06 +02:00
// NOTE returning a null claim allows us to differentiate between an
// undefined (never fetched claim) and one which just doesn't exist. Not
// the cleanest solution but couldn't think of anything better right now
if (claimId === null) {
claims[uri] = null;
} else {
const claim = byId[claimId];
claims[uri] = claim;
}
2017-06-10 19:17:37 +02:00
});
2017-06-04 13:53:26 +02:00
return claims;
}
2017-06-10 19:17:37 +02:00
);
2017-06-04 13:53:26 +02:00
2017-05-15 05:50:59 +02:00
export const selectAllClaimsByChannel = createSelector(
2017-05-13 00:50:51 +02:00
_selectState,
2017-06-06 23:19:12 +02:00
state => state.claimsByChannel || {}
2017-06-06 06:21:55 +02:00
);
2017-05-13 00:50:51 +02:00
const selectClaimForUri = (state, props) => {
2017-06-06 23:19:12 +02:00
const uri = lbryuri.normalize(props.uri);
return selectClaimsByUri(state)[uri];
2017-06-06 06:21:55 +02:00
};
2017-05-15 05:50:59 +02:00
export const makeSelectClaimForUri = () => {
2017-06-06 23:19:12 +02:00
return createSelector(selectClaimForUri, claim => claim);
2017-06-06 06:21:55 +02:00
};
2017-05-13 00:50:51 +02:00
const selectClaimForUriIsMine = (state, props) => {
const uri = lbryuri.normalize(props.uri);
const claim = selectClaimsByUri(state)[uri];
2017-07-03 09:53:56 +02:00
const myClaims = selectMyClaimsRaw(state);
return myClaims.has(claim.claim_id);
};
export const makeSelectClaimForUriIsMine = () => {
return createSelector(selectClaimForUriIsMine, isMine => isMine);
};
export const selectClaimsInChannelForUri = (state, props) => {
2017-06-06 23:19:12 +02:00
return selectAllClaimsByChannel(state)[props.uri];
2017-06-06 06:21:55 +02:00
};
export const makeSelectClaimsInChannelForUri = () => {
2017-06-06 23:19:12 +02:00
return createSelector(selectClaimsInChannelForUri, claims => claims);
2017-06-06 06:21:55 +02:00
};
const selectMetadataForUri = (state, props) => {
2017-06-06 23:19:12 +02:00
const claim = selectClaimForUri(state, props);
const metadata =
claim && claim.value && claim.value.stream && claim.value.stream.metadata;
2017-06-06 23:19:12 +02:00
const value = metadata ? metadata : claim === undefined ? undefined : null;
return value;
2017-06-06 06:21:55 +02:00
};
export const makeSelectMetadataForUri = () => {
2017-06-06 23:19:12 +02:00
return createSelector(selectMetadataForUri, metadata => metadata);
2017-06-06 06:21:55 +02:00
};
const selectSourceForUri = (state, props) => {
2017-06-06 23:19:12 +02:00
const claim = selectClaimForUri(state, props);
const source =
claim && claim.value && claim.value.stream && claim.value.stream.source;
2017-06-06 23:19:12 +02:00
return source ? source : claim === undefined ? undefined : null;
2017-06-06 06:21:55 +02:00
};
export const makeSelectSourceForUri = () => {
2017-06-06 23:19:12 +02:00
return createSelector(selectSourceForUri, source => source);
2017-06-06 06:21:55 +02:00
};
2017-05-01 08:26:09 +02:00
2017-05-15 05:50:59 +02:00
export const makeSelectContentTypeForUri = () => {
return createSelector(
selectSourceForUri,
2017-06-06 23:19:12 +02:00
source => (source ? source.contentType : source)
);
2017-06-06 06:21:55 +02:00
};
2017-05-15 05:50:59 +02:00
export const selectIsFetchingClaimListMine = createSelector(
2017-05-01 08:26:09 +02:00
_selectState,
state => !!state.isFetchingClaimListMine
2017-06-06 06:21:55 +02:00
);
2017-05-01 08:26:09 +02:00
export const selectMyClaimsRaw = createSelector(
_selectState,
state => new Set(state.myClaims)
2017-06-06 06:21:55 +02:00
);
2017-05-01 08:26:09 +02:00
2017-07-08 10:03:12 +02:00
export const selectAbandoningIds = createSelector(_selectState, state =>
Object.keys(state.abandoningById || {})
);
export const selectPendingClaims = createSelector(_selectState, state =>
Object.values(state.pendingById || {})
);
export const selectMyClaims = createSelector(
selectMyClaimsRaw,
selectClaimsById,
2017-07-08 10:03:12 +02:00
selectAbandoningIds,
selectPendingClaims,
(myClaimIds, byId, abandoningIds, pendingClaims) => {
const claims = [];
myClaimIds.forEach(id => {
const claim = byId[id];
2017-07-08 10:03:12 +02:00
if (claim && abandoningIds.indexOf(id) == -1) claims.push(claim);
});
2017-07-08 10:03:12 +02:00
return [...claims, ...pendingClaims];
}
);
2017-07-08 10:03:12 +02:00
export const selectMyClaimsWithoutChannels = createSelector(
selectMyClaims,
myClaims => myClaims.filter(claim => !claim.name.match(/^@/))
);
2017-05-01 08:26:09 +02:00
export const selectMyClaimsOutpoints = createSelector(
selectMyClaims,
myClaims => {
2017-06-06 23:19:12 +02:00
const outpoints = [];
2017-05-01 08:26:09 +02:00
myClaims.forEach(claim => outpoints.push(`${claim.txid}:${claim.nout}`));
2017-06-06 23:19:12 +02:00
return outpoints;
2017-05-01 08:26:09 +02:00
}
2017-06-06 06:21:55 +02:00
);
2017-07-10 16:49:12 +02:00
export const selectFetchingMyChannels = createSelector(
_selectState,
state => !!state.fetchingMyChannels
);
export const selectMyChannelClaims = createSelector(
_selectState,
selectClaimsById,
(state, byId) => {
const ids = state.myChannelClaims || [];
const claims = [];
ids.forEach(id => claims.push(byId[id]));
return claims;
}
);