SidebarMenu: memoize otherAccounts for performance

actually-fix-tabs-bar
Alex Gleason 2021-07-01 18:59:48 -05:00
rodzic 1b92ce0d4a
commit 93d68ffe9b
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
1 zmienionych plików z 36 dodań i 20 usunięć

Wyświetl plik

@ -19,6 +19,7 @@ import ThemeToggle from '../features/ui/components/theme_toggle_container';
import { fetchOwnAccounts } from 'soapbox/actions/auth'; import { fetchOwnAccounts } from 'soapbox/actions/auth';
import { List as ImmutableList, is as ImmutableIs } from 'immutable'; import { List as ImmutableList, is as ImmutableIs } from 'immutable';
import { getSoapboxConfig } from 'soapbox/actions/soapbox'; import { getSoapboxConfig } from 'soapbox/actions/soapbox';
import { createSelector } from 'reselect';
const messages = defineMessages({ const messages = defineMessages({
followers: { id: 'account.followers', defaultMessage: 'Followers' }, followers: { id: 'account.followers', defaultMessage: 'Followers' },
@ -45,20 +46,32 @@ const messages = defineMessages({
add_account: { id: 'profile_dropdown.add_account', defaultMessage: 'Add an existing account' }, add_account: { id: 'profile_dropdown.add_account', defaultMessage: 'Add an existing account' },
}); });
const mapStateToProps = state => { const makeGetOtherAccounts = () => {
const me = state.get('me'); return createSelector(
const getAccount = makeGetAccount(); [(accounts, authUsers, me) => {
const soapbox = getSoapboxConfig(state); return authUsers
const otherAccounts =
state
.getIn(['auth', 'users'])
.keySeq() .keySeq()
.reduce((list, id) => { .reduce((list, id) => {
if (id === me) return list; if (id === me) return list;
const account = state.getIn(['accounts', id]); const account = accounts.get(id);
return account ? list.push(account) : list; return account ? list.push(account) : list;
}, ImmutableList()); }, ImmutableList());
}],
otherAccounts => otherAccounts,
);
};
const makeMapStateToProps = () => {
const getAccount = makeGetAccount();
const getOtherAccounts = makeGetOtherAccounts();
const mapStateToProps = state => {
const me = state.get('me');
const soapbox = getSoapboxConfig(state);
const accounts = state.get('accounts');
const authUsers = state.getIn(['auth', 'users']);
const otherAccounts = getOtherAccounts(accounts, authUsers, me);
return { return {
account: getAccount(state, me), account: getAccount(state, me),
@ -68,6 +81,9 @@ const mapStateToProps = state => {
isStaff: isStaff(state.getIn(['accounts', me])), isStaff: isStaff(state.getIn(['accounts', me])),
otherAccounts, otherAccounts,
}; };
};
return mapStateToProps;
}; };
const mapDispatchToProps = (dispatch, { intl }) => ({ const mapDispatchToProps = (dispatch, { intl }) => ({
@ -86,7 +102,7 @@ const mapDispatchToProps = (dispatch, { intl }) => ({
}, },
}); });
export default @connect(mapStateToProps, mapDispatchToProps) export default @connect(makeMapStateToProps, mapDispatchToProps)
@injectIntl @injectIntl
class SidebarMenu extends ImmutablePureComponent { class SidebarMenu extends ImmutablePureComponent {