From 827a08ac26a3e30c828b163a151e3749d199df65 Mon Sep 17 00:00:00 2001 From: infinite-persistence Date: Wed, 10 Nov 2021 23:30:58 +0800 Subject: [PATCH] Fix memo: stake indicator --- ui/component/channelStakedIndicator/index.js | 12 ++--- ui/component/comment/index.js | 4 +- ui/component/livestreamComment/index.js | 4 +- ui/redux/selectors/app.js | 22 ++++----- ui/redux/selectors/claims.js | 50 ++++++++++---------- 5 files changed, 43 insertions(+), 49 deletions(-) diff --git a/ui/component/channelStakedIndicator/index.js b/ui/component/channelStakedIndicator/index.js index d217012a4..c2a7a4f61 100644 --- a/ui/component/channelStakedIndicator/index.js +++ b/ui/component/channelStakedIndicator/index.js @@ -1,15 +1,15 @@ import { connect } from 'react-redux'; import { - makeSelectClaimForUri, - makeSelectStakedLevelForChannelUri, - makeSelectTotalStakedAmountForChannelUri, + selectClaimForUri, + selectStakedLevelForChannelUri, + selectTotalStakedAmountForChannelUri, } from 'redux/selectors/claims'; import ChannelStakedIndicator from './view'; const select = (state, props) => ({ - channelClaim: makeSelectClaimForUri(props.uri)(state), - amount: makeSelectTotalStakedAmountForChannelUri(props.uri)(state), - level: makeSelectStakedLevelForChannelUri(props.uri)(state), + channelClaim: selectClaimForUri(state, props.uri), + amount: selectTotalStakedAmountForChannelUri(state, props.uri), + level: selectStakedLevelForChannelUri(state, props.uri), }); export default connect(select)(ChannelStakedIndicator); diff --git a/ui/component/comment/index.js b/ui/component/comment/index.js index 99de22a84..27a847ee1 100644 --- a/ui/component/comment/index.js +++ b/ui/component/comment/index.js @@ -1,6 +1,6 @@ import { connect } from 'react-redux'; import { - makeSelectStakedLevelForChannelUri, + selectStakedLevelForChannelUri, makeSelectClaimForUri, makeSelectThumbnailForUri, selectHasChannels, @@ -33,7 +33,7 @@ const select = (state, props) => { activeChannelClaim, hasChannels: selectHasChannels(state), playingUri: selectPlayingUri(state), - stakedLevel: makeSelectStakedLevelForChannelUri(props.authorUri)(state), + stakedLevel: selectStakedLevelForChannelUri(state, props.authorUri), linkedCommentAncestors: selectLinkedCommentAncestors(state), totalReplyPages: makeSelectTotalReplyPagesForParentId(props.commentId)(state), }; diff --git a/ui/component/livestreamComment/index.js b/ui/component/livestreamComment/index.js index b18a96a36..a75d34717 100644 --- a/ui/component/livestreamComment/index.js +++ b/ui/component/livestreamComment/index.js @@ -1,10 +1,10 @@ import { connect } from 'react-redux'; -import { makeSelectStakedLevelForChannelUri, selectClaimForUri } from 'redux/selectors/claims'; +import { selectStakedLevelForChannelUri, selectClaimForUri } from 'redux/selectors/claims'; import LivestreamComment from './view'; const select = (state, props) => ({ claim: selectClaimForUri(state, props.uri), - stakedLevel: makeSelectStakedLevelForChannelUri(props.authorUri)(state), + stakedLevel: selectStakedLevelForChannelUri(state, props.authorUri), }); export default connect(select)(LivestreamComment); diff --git a/ui/redux/selectors/app.js b/ui/redux/selectors/app.js index 37f6786ba..97403d51d 100644 --- a/ui/redux/selectors/app.js +++ b/ui/redux/selectors/app.js @@ -1,5 +1,5 @@ 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 || {}; @@ -97,18 +97,14 @@ export const selectActiveChannelClaim = createSelector( } ); -export const selectActiveChannelStakedLevel = createSelector( - (state) => state, - selectActiveChannelClaim, - (state, activeChannelClaim) => { - if (!activeChannelClaim) { - return 0; - } - - const uri = activeChannelClaim.permanent_url; - const stakedLevel = makeSelectStakedLevelForChannelUri(uri)(state); - return stakedLevel; +export const selectActiveChannelStakedLevel = (state) => { + const activeChannelClaim = selectActiveChannelClaim(state); + if (!activeChannelClaim) { + return 0; } -); + + const uri = activeChannelClaim.permanent_url; + return selectStakedLevelForChannelUri(state, uri); +}; export const selectIncognito = (state) => selectState(state).incognito; diff --git a/ui/redux/selectors/claims.js b/ui/redux/selectors/claims.js index 79b86b959..07cccd54e 100644 --- a/ui/redux/selectors/claims.js +++ b/ui/redux/selectors/claims.js @@ -704,34 +704,32 @@ export const makeSelectClaimIsStreamPlaceholder = (uri: string) => return Boolean(claim.value_type === 'stream' && !claim.value.source); }); -export const makeSelectTotalStakedAmountForChannelUri = (uri: string) => - createSelector(makeSelectClaimForUri(uri), (claim) => { - if (!claim || !claim.amount || !claim.meta || !claim.meta.support_amount) { - return 0; - } +export const selectTotalStakedAmountForChannelUri = createCachedSelector(selectClaimForUri, (claim) => { + if (!claim || !claim.amount || !claim.meta || !claim.meta.support_amount) { + 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) => - createSelector(makeSelectTotalStakedAmountForChannelUri(uri), (amount) => { - let level = 1; - switch (true) { - case amount >= CLAIM.LEVEL_2_STAKED_AMOUNT && amount < CLAIM.LEVEL_3_STAKED_AMOUNT: - level = 2; - break; - case amount >= CLAIM.LEVEL_3_STAKED_AMOUNT && amount < CLAIM.LEVEL_4_STAKED_AMOUNT: - level = 3; - break; - case amount >= CLAIM.LEVEL_4_STAKED_AMOUNT && amount < CLAIM.LEVEL_5_STAKED_AMOUNT: - level = 4; - break; - case amount >= CLAIM.LEVEL_5_STAKED_AMOUNT: - level = 5; - break; - } - return level; - }); +export const selectStakedLevelForChannelUri = createCachedSelector(selectTotalStakedAmountForChannelUri, (amount) => { + let level = 1; + switch (true) { + case amount >= CLAIM.LEVEL_2_STAKED_AMOUNT && amount < CLAIM.LEVEL_3_STAKED_AMOUNT: + level = 2; + break; + case amount >= CLAIM.LEVEL_3_STAKED_AMOUNT && amount < CLAIM.LEVEL_4_STAKED_AMOUNT: + level = 3; + break; + case amount >= CLAIM.LEVEL_4_STAKED_AMOUNT && amount < CLAIM.LEVEL_5_STAKED_AMOUNT: + level = 4; + break; + case amount >= CLAIM.LEVEL_5_STAKED_AMOUNT: + level = 5; + break; + } + return level; +})((state, uri) => String(uri)); export const selectUpdatingCollection = (state: State) => selectState(state).updatingCollection; export const selectUpdateCollectionError = (state: State) => selectState(state).updateCollectionError;