ProfileDropdown: memoize otherAccounts for performance

merge-requests/568/head
Alex Gleason 3 years ago
parent 93d68ffe9b
commit e7d360baae
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7

@ -13,13 +13,12 @@ import Icon from './icon';
import DisplayName from './display_name'; import DisplayName from './display_name';
import { closeSidebar } from '../actions/sidebar'; import { closeSidebar } from '../actions/sidebar';
import { isStaff } from '../utils/accounts'; import { isStaff } from '../utils/accounts';
import { makeGetAccount } from '../selectors'; import { makeGetAccount, makeGetOtherAccounts } from '../selectors';
import { logOut, switchAccount } from 'soapbox/actions/auth'; import { logOut, switchAccount } from 'soapbox/actions/auth';
import ThemeToggle from '../features/ui/components/theme_toggle_container'; 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 { 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' },
@ -46,21 +45,6 @@ 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 makeGetOtherAccounts = () => {
return createSelector(
[(accounts, authUsers, me) => {
return authUsers
.keySeq()
.reduce((list, id) => {
if (id === me) return list;
const account = accounts.get(id);
return account ? list.push(account) : list;
}, ImmutableList());
}],
otherAccounts => otherAccounts,
);
};
const makeMapStateToProps = () => { const makeMapStateToProps = () => {
const getAccount = makeGetAccount(); const getAccount = makeGetAccount();
const getOtherAccounts = makeGetOtherAccounts(); const getOtherAccounts = makeGetOtherAccounts();

@ -8,33 +8,34 @@ import DropdownMenuContainer from '../../../containers/dropdown_menu_container';
import { isStaff } from 'soapbox/utils/accounts'; import { isStaff } from 'soapbox/utils/accounts';
import { defineMessages, injectIntl } from 'react-intl'; import { defineMessages, injectIntl } from 'react-intl';
import { logOut, switchAccount } from 'soapbox/actions/auth'; import { logOut, switchAccount } from 'soapbox/actions/auth';
import { List as ImmutableList, is as ImmutableIs } from 'immutable'; import { is as ImmutableIs } from 'immutable';
import Avatar from 'soapbox/components/avatar'; import Avatar from 'soapbox/components/avatar';
import DisplayName from 'soapbox/components/display_name'; import DisplayName from 'soapbox/components/display_name';
import { makeGetOtherAccounts } from 'soapbox/selectors';
const messages = defineMessages({ const messages = defineMessages({
add: { id: 'profile_dropdown.add_account', defaultMessage: 'Add an existing account' }, add: { id: 'profile_dropdown.add_account', defaultMessage: 'Add an existing account' },
logout: { id: 'profile_dropdown.logout', defaultMessage: 'Log out @{acct}' }, logout: { id: 'profile_dropdown.logout', defaultMessage: 'Log out @{acct}' },
}); });
const mapStateToProps = state => { const makeMapStateToProps = () => {
const me = state.get('me'); const getOtherAccounts = makeGetOtherAccounts();
const otherAccounts = const mapStateToProps = state => {
state const me = state.get('me');
.getIn(['auth', 'users'])
.keySeq() const accounts = state.get('accounts');
.reduce((list, id) => { const authUsers = state.getIn(['auth', 'users']);
if (id === me) return list; const otherAccounts = getOtherAccounts(accounts, authUsers, me);
const account = state.getIn(['accounts', id]);
return account ? list.push(account) : list; return {
}, ImmutableList()); account: state.getIn(['accounts', me]),
otherAccounts,
return { isStaff: isStaff(state.getIn(['accounts', me])),
account: state.getIn(['accounts', me]), };
otherAccounts,
isStaff: isStaff(state.getIn(['accounts', me])),
}; };
return mapStateToProps;
}; };
class ProfileDropdown extends React.PureComponent { class ProfileDropdown extends React.PureComponent {
@ -130,4 +131,4 @@ class ProfileDropdown extends React.PureComponent {
} }
export default injectIntl(connect(mapStateToProps)(ProfileDropdown)); export default injectIntl(connect(makeMapStateToProps)(ProfileDropdown));

@ -195,3 +195,18 @@ export const makeGetReport = () => {
}, },
); );
}; };
export const makeGetOtherAccounts = () => {
return createSelector(
[(accounts, authUsers, me) => {
return authUsers
.keySeq()
.reduce((list, id) => {
if (id === me) return list;
const account = accounts.get(id);
return account ? list.push(account) : list;
}, ImmutableList());
}],
otherAccounts => otherAccounts,
);
};

Loading…
Cancel
Save