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

214 lines
5.2 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import { createSelector } from "reselect";
import lbryuri from "lbryuri";
2017-09-20 16:57:43 +02:00
import { makeSelectCurrentParam } from "./navigation";
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
2017-09-08 05:15:05 +02:00
export const makeSelectClaimForUri = uri => {
return createSelector(
selectClaimsByUri,
claims => claims && claims[lbryuri.normalize(uri)]
);
};
2017-09-08 05:15:05 +02:00
export const makeSelectClaimIsMine = rawUri => {
const uri = lbryuri.normalize(rawUri);
return createSelector(
selectClaimsByUri,
2017-11-01 21:23:30 +01:00
selectMyActiveClaims,
2017-09-08 05:15:05 +02:00
(claims, myClaims) =>
claims &&
claims[uri] &&
claims[uri].claim_id &&
myClaims.has(claims[uri].claim_id)
);
};
2017-07-17 08:06:04 +02:00
export const selectAllFetchingChannelClaims = createSelector(
_selectState,
state => state.fetchingChannelClaims || {}
);
2017-09-08 05:15:05 +02:00
export const makeSelectFetchingChannelClaims = uri => {
2017-09-17 20:26:55 +02:00
return createSelector(
2017-09-08 05:15:05 +02:00
selectAllFetchingChannelClaims,
fetching => fetching && fetching[uri]
);
2017-07-17 08:06:04 +02:00
};
2017-09-20 16:57:43 +02:00
export const makeSelectClaimsInChannelForCurrentPage = uri => {
const pageSelector = makeSelectCurrentParam("page");
2017-09-08 05:15:05 +02:00
return createSelector(
selectClaimsById,
selectAllClaimsByChannel,
2017-09-20 16:57:43 +02:00
pageSelector,
(byId, allClaims, page) => {
2017-09-08 05:15:05 +02:00
const byChannel = allClaims[uri] || {};
2017-09-20 19:12:53 +02:00
const claimIds = byChannel[page || 1];
2017-09-08 05:15:05 +02:00
if (!claimIds) return claimIds;
2017-09-08 05:15:05 +02:00
return claimIds.map(claimId => byId[claimId]);
}
);
2017-06-06 06:21:55 +02:00
};
2017-09-08 05:15:05 +02:00
export const makeSelectMetadataForUri = uri => {
return createSelector(makeSelectClaimForUri(uri), claim => {
const metadata =
claim && claim.value && claim.value.stream && claim.value.stream.metadata;
2017-09-08 05:15:05 +02:00
const value = metadata ? metadata : claim === undefined ? undefined : null;
return value;
});
2017-06-06 06:21:55 +02:00
};
2017-09-17 22:33:52 +02:00
export const makeSelectTitleForUri = uri => {
return createSelector(
makeSelectMetadataForUri(uri),
metadata => metadata && metadata.title
);
};
2017-09-08 05:15:05 +02:00
export const makeSelectContentTypeForUri = uri => {
return createSelector(makeSelectClaimForUri(uri), claim => {
const source =
claim && claim.value && claim.value.stream && claim.value.stream.source;
return source ? source.contentType : undefined;
});
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
2017-11-01 21:23:30 +01:00
export const selectMyClaimsRaw = createSelector(
_selectState,
2017-10-31 15:02:08 +01:00
state => 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 || {})
);
2017-11-01 21:23:30 +01:00
export const selectMyActiveClaims = createSelector(
selectMyClaimsRaw,
2017-10-31 15:02:08 +01:00
selectAbandoningIds,
(claims, abandoningIds) =>
new Set(
2017-11-06 15:50:43 +01:00
claims &&
claims
.map(claim => claim.claim_id)
.filter(claimId => Object.keys(abandoningIds).indexOf(claimId) === -1)
2017-10-31 15:02:08 +01:00
)
);
2017-07-08 10:03:12 +02:00
export const selectPendingClaims = createSelector(_selectState, state =>
Object.values(state.pendingById || {})
);
export const selectMyClaims = createSelector(
2017-11-01 21:23:30 +01:00
selectMyActiveClaims,
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-11-01 21:23:30 +01:00
export const selectAllMyClaimsByOutpoint = createSelector(
selectMyClaimsRaw,
claims =>
new Set(
claims && claims.length
? claims.map(claim => `${claim.txid}:${claim.nout}`)
: null
)
2017-10-24 15:10:27 +02:00
);
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 = [];
2017-09-22 03:12:18 +02:00
ids.forEach(id => {
if (byId[id]) {
//I'm not sure why this check is necessary, but it ought to be a quick fix for https://github.com/lbryio/lbry-app/issues/544
claims.push(byId[id]);
}
});
2017-07-10 16:49:12 +02:00
return claims;
}
);