Memoize ancestorIds and descendantIds in detailed status view

This commit is contained in:
Claire 2021-08-02 17:46:18 +02:00 committed by marcin mikołajczak
parent 38ad49c1e6
commit b01b175fdc
1 changed files with 47 additions and 31 deletions

View File

@ -41,6 +41,7 @@ import StatusContainer from '../../containers/status_container';
import { openModal } from '../../actions/modal'; import { openModal } from '../../actions/modal';
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl'; import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
import ImmutablePureComponent from 'react-immutable-pure-component'; import ImmutablePureComponent from 'react-immutable-pure-component';
import { createSelector } from 'reselect';
import { HotKeys } from 'react-hotkeys'; import { HotKeys } from 'react-hotkeys';
import { attachFullscreenListener, detachFullscreenListener, isFullscreen } from '../ui/util/fullscreen'; import { attachFullscreenListener, detachFullscreenListener, isFullscreen } from '../ui/util/fullscreen';
import { textForScreenReader, defaultMediaVisibility } from '../../components/status'; import { textForScreenReader, defaultMediaVisibility } from '../../components/status';
@ -66,33 +67,36 @@ const messages = defineMessages({
const makeMapStateToProps = () => { const makeMapStateToProps = () => {
const getStatus = makeGetStatus(); const getStatus = makeGetStatus();
const mapStateToProps = (state, props) => { const getAncestorsIds = createSelector([
const status = getStatus(state, { (_, { id }) => id,
id: props.params.statusId, state => state.getIn(['contexts', 'inReplyTos']),
username: props.params.username, ], (statusId, inReplyTos) => {
});
let ancestorsIds = Immutable.List(); let ancestorsIds = Immutable.List();
let descendantsIds = Immutable.List();
if (status) {
ancestorsIds = ancestorsIds.withMutations(mutable => { ancestorsIds = ancestorsIds.withMutations(mutable => {
let id = state.getIn(['contexts', 'inReplyTos', status.get('id')]); let id = statusId;
while (id) { while (id) {
mutable.unshift(id); mutable.unshift(id);
id = state.getIn(['contexts', 'inReplyTos', id]); id = inReplyTos.get(id);
} }
}); });
return ancestorsIds;
});
const getDescendantsIds = createSelector([
(_, { id }) => id,
state => state.getIn(['contexts', 'replies']),
], (statusId, contextReplies) => {
let descendantsIds = Immutable.List();
descendantsIds = descendantsIds.withMutations(mutable => { descendantsIds = descendantsIds.withMutations(mutable => {
const ids = [status.get('id')]; const ids = [statusId];
while (ids.length > 0) { while (ids.length > 0) {
let id = ids.shift(); let id = ids.shift();
const replies = state.getIn(['contexts', 'replies', id]); const replies = contextReplies.get(id);
if (status.get('id') !== id) { if (statusId !== id) {
mutable.push(id); mutable.push(id);
} }
@ -103,6 +107,18 @@ const makeMapStateToProps = () => {
} }
} }
}); });
return descendantsIds;
});
const mapStateToProps = (state, props) => {
const status = getStatus(state, { id: props.params.statusId });
let ancestorsIds = Immutable.List();
let descendantsIds = Immutable.List();
if (status) {
ancestorsIds = getAncestorsIds(state, { id: state.getIn(['contexts', 'inReplyTos', status.get('id')]) });
descendantsIds = getDescendantsIds(state, { id: status.get('id') });
} }
const soapbox = getSoapboxConfig(state); const soapbox = getSoapboxConfig(state);