Fix memo: stake indicator

This commit is contained in:
infinite-persistence 2021-11-10 23:30:58 +08:00
parent 6492fe1c66
commit 827a08ac26
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0
5 changed files with 43 additions and 49 deletions

View file

@ -1,15 +1,15 @@
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { import {
makeSelectClaimForUri, selectClaimForUri,
makeSelectStakedLevelForChannelUri, selectStakedLevelForChannelUri,
makeSelectTotalStakedAmountForChannelUri, selectTotalStakedAmountForChannelUri,
} from 'redux/selectors/claims'; } from 'redux/selectors/claims';
import ChannelStakedIndicator from './view'; import ChannelStakedIndicator from './view';
const select = (state, props) => ({ const select = (state, props) => ({
channelClaim: makeSelectClaimForUri(props.uri)(state), channelClaim: selectClaimForUri(state, props.uri),
amount: makeSelectTotalStakedAmountForChannelUri(props.uri)(state), amount: selectTotalStakedAmountForChannelUri(state, props.uri),
level: makeSelectStakedLevelForChannelUri(props.uri)(state), level: selectStakedLevelForChannelUri(state, props.uri),
}); });
export default connect(select)(ChannelStakedIndicator); export default connect(select)(ChannelStakedIndicator);

View file

@ -1,6 +1,6 @@
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { import {
makeSelectStakedLevelForChannelUri, selectStakedLevelForChannelUri,
makeSelectClaimForUri, makeSelectClaimForUri,
makeSelectThumbnailForUri, makeSelectThumbnailForUri,
selectHasChannels, selectHasChannels,
@ -33,7 +33,7 @@ const select = (state, props) => {
activeChannelClaim, activeChannelClaim,
hasChannels: selectHasChannels(state), hasChannels: selectHasChannels(state),
playingUri: selectPlayingUri(state), playingUri: selectPlayingUri(state),
stakedLevel: makeSelectStakedLevelForChannelUri(props.authorUri)(state), stakedLevel: selectStakedLevelForChannelUri(state, props.authorUri),
linkedCommentAncestors: selectLinkedCommentAncestors(state), linkedCommentAncestors: selectLinkedCommentAncestors(state),
totalReplyPages: makeSelectTotalReplyPagesForParentId(props.commentId)(state), totalReplyPages: makeSelectTotalReplyPagesForParentId(props.commentId)(state),
}; };

View file

@ -1,10 +1,10 @@
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { makeSelectStakedLevelForChannelUri, selectClaimForUri } from 'redux/selectors/claims'; import { selectStakedLevelForChannelUri, selectClaimForUri } from 'redux/selectors/claims';
import LivestreamComment from './view'; import LivestreamComment from './view';
const select = (state, props) => ({ const select = (state, props) => ({
claim: selectClaimForUri(state, props.uri), claim: selectClaimForUri(state, props.uri),
stakedLevel: makeSelectStakedLevelForChannelUri(props.authorUri)(state), stakedLevel: selectStakedLevelForChannelUri(state, props.authorUri),
}); });
export default connect(select)(LivestreamComment); export default connect(select)(LivestreamComment);

View file

@ -1,5 +1,5 @@
import { createSelector } from 'reselect'; import { createSelector } from 'reselect';
import { selectClaimWithId, selectMyChannelClaims, makeSelectStakedLevelForChannelUri } from 'redux/selectors/claims'; import { selectClaimWithId, selectMyChannelClaims, selectStakedLevelForChannelUri } from 'redux/selectors/claims';
export const selectState = (state) => state.app || {}; export const selectState = (state) => state.app || {};
@ -97,18 +97,14 @@ export const selectActiveChannelClaim = createSelector(
} }
); );
export const selectActiveChannelStakedLevel = createSelector( export const selectActiveChannelStakedLevel = (state) => {
(state) => state, const activeChannelClaim = selectActiveChannelClaim(state);
selectActiveChannelClaim, if (!activeChannelClaim) {
(state, activeChannelClaim) => { return 0;
if (!activeChannelClaim) {
return 0;
}
const uri = activeChannelClaim.permanent_url;
const stakedLevel = makeSelectStakedLevelForChannelUri(uri)(state);
return stakedLevel;
} }
);
const uri = activeChannelClaim.permanent_url;
return selectStakedLevelForChannelUri(state, uri);
};
export const selectIncognito = (state) => selectState(state).incognito; export const selectIncognito = (state) => selectState(state).incognito;

View file

@ -704,34 +704,32 @@ export const makeSelectClaimIsStreamPlaceholder = (uri: string) =>
return Boolean(claim.value_type === 'stream' && !claim.value.source); return Boolean(claim.value_type === 'stream' && !claim.value.source);
}); });
export const makeSelectTotalStakedAmountForChannelUri = (uri: string) => export const selectTotalStakedAmountForChannelUri = createCachedSelector(selectClaimForUri, (claim) => {
createSelector(makeSelectClaimForUri(uri), (claim) => { if (!claim || !claim.amount || !claim.meta || !claim.meta.support_amount) {
if (!claim || !claim.amount || !claim.meta || !claim.meta.support_amount) { return 0;
return 0; }
}
return parseFloat(claim.amount) + parseFloat(claim.meta.support_amount) || 0; return parseFloat(claim.amount) + parseFloat(claim.meta.support_amount) || 0;
}); })((state, uri) => String(uri));
export const makeSelectStakedLevelForChannelUri = (uri: string) => export const selectStakedLevelForChannelUri = createCachedSelector(selectTotalStakedAmountForChannelUri, (amount) => {
createSelector(makeSelectTotalStakedAmountForChannelUri(uri), (amount) => { let level = 1;
let level = 1; switch (true) {
switch (true) { case amount >= CLAIM.LEVEL_2_STAKED_AMOUNT && amount < CLAIM.LEVEL_3_STAKED_AMOUNT:
case amount >= CLAIM.LEVEL_2_STAKED_AMOUNT && amount < CLAIM.LEVEL_3_STAKED_AMOUNT: level = 2;
level = 2; break;
break; case amount >= CLAIM.LEVEL_3_STAKED_AMOUNT && amount < CLAIM.LEVEL_4_STAKED_AMOUNT:
case amount >= CLAIM.LEVEL_3_STAKED_AMOUNT && amount < CLAIM.LEVEL_4_STAKED_AMOUNT: level = 3;
level = 3; break;
break; case amount >= CLAIM.LEVEL_4_STAKED_AMOUNT && amount < CLAIM.LEVEL_5_STAKED_AMOUNT:
case amount >= CLAIM.LEVEL_4_STAKED_AMOUNT && amount < CLAIM.LEVEL_5_STAKED_AMOUNT: level = 4;
level = 4; break;
break; case amount >= CLAIM.LEVEL_5_STAKED_AMOUNT:
case amount >= CLAIM.LEVEL_5_STAKED_AMOUNT: level = 5;
level = 5; break;
break; }
} return level;
return level; })((state, uri) => String(uri));
});
export const selectUpdatingCollection = (state: State) => selectState(state).updatingCollection; export const selectUpdatingCollection = (state: State) => selectState(state).updatingCollection;
export const selectUpdateCollectionError = (state: State) => selectState(state).updateCollectionError; export const selectUpdateCollectionError = (state: State) => selectState(state).updateCollectionError;