2021-03-23 17:06:55 -07:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
2021-03-23 19:01:50 -07:00
|
|
|
import { fetchOwnAccounts } from 'soapbox/actions/auth';
|
2021-03-25 11:47:01 -07:00
|
|
|
import { throttle } from 'lodash';
|
2021-03-23 17:06:55 -07:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import DropdownMenuContainer from '../../../containers/dropdown_menu_container';
|
|
|
|
import { isStaff } from 'soapbox/utils/accounts';
|
|
|
|
import { defineMessages, injectIntl } from 'react-intl';
|
|
|
|
import { logOut, switchAccount } from 'soapbox/actions/auth';
|
2021-07-01 17:31:27 -07:00
|
|
|
import { is as ImmutableIs } from 'immutable';
|
2021-03-25 11:23:11 -07:00
|
|
|
import Avatar from 'soapbox/components/avatar';
|
|
|
|
import DisplayName from 'soapbox/components/display_name';
|
2021-07-01 17:31:27 -07:00
|
|
|
import { makeGetOtherAccounts } from 'soapbox/selectors';
|
2021-03-23 17:06:55 -07:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
2021-03-25 10:21:48 -07:00
|
|
|
add: { id: 'profile_dropdown.add_account', defaultMessage: 'Add an existing account' },
|
2021-03-23 17:06:55 -07:00
|
|
|
logout: { id: 'profile_dropdown.logout', defaultMessage: 'Log out @{acct}' },
|
|
|
|
});
|
|
|
|
|
2021-07-01 17:31:27 -07:00
|
|
|
const makeMapStateToProps = () => {
|
|
|
|
const getOtherAccounts = makeGetOtherAccounts();
|
|
|
|
|
|
|
|
const mapStateToProps = state => {
|
|
|
|
const me = state.get('me');
|
|
|
|
|
|
|
|
return {
|
|
|
|
account: state.getIn(['accounts', me]),
|
2021-07-10 02:48:12 -07:00
|
|
|
otherAccounts: getOtherAccounts(state),
|
2021-07-01 17:31:27 -07:00
|
|
|
isStaff: isStaff(state.getIn(['accounts', me])),
|
|
|
|
};
|
2021-03-23 17:06:55 -07:00
|
|
|
};
|
2021-07-01 17:31:27 -07:00
|
|
|
|
|
|
|
return mapStateToProps;
|
2021-03-23 17:06:55 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class ProfileDropdown extends React.PureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
dispatch: PropTypes.func.isRequired,
|
|
|
|
size: PropTypes.number,
|
|
|
|
account: ImmutablePropTypes.map,
|
|
|
|
otherAccounts: ImmutablePropTypes.list,
|
|
|
|
isStaff: PropTypes.bool.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
isStaff: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
handleLogOut = e => {
|
2021-06-26 15:04:27 -07:00
|
|
|
this.props.dispatch(logOut(this.props.intl));
|
2021-03-23 17:06:55 -07:00
|
|
|
e.preventDefault();
|
|
|
|
};
|
|
|
|
|
|
|
|
handleSwitchAccount = account => {
|
|
|
|
return e => {
|
|
|
|
this.props.dispatch(switchAccount(account.get('id')));
|
|
|
|
e.preventDefault();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-29 21:22:54 -07:00
|
|
|
handleMiddleClick = account => {
|
|
|
|
return e => {
|
2021-03-29 22:45:23 -07:00
|
|
|
this.props.dispatch(switchAccount(account.get('id'), true));
|
2021-03-29 21:22:54 -07:00
|
|
|
window.open('/', '_blank', 'noopener,noreferrer');
|
|
|
|
e.preventDefault();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-25 11:47:01 -07:00
|
|
|
fetchOwnAccounts = throttle(() => {
|
2021-03-23 19:01:50 -07:00
|
|
|
this.props.dispatch(fetchOwnAccounts());
|
2021-03-25 11:47:01 -07:00
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.fetchOwnAccounts();
|
2021-03-23 19:01:50 -07:00
|
|
|
}
|
|
|
|
|
2021-04-10 16:09:05 -07:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const accountChanged = !ImmutableIs(prevProps.account, this.props.account);
|
|
|
|
const otherAccountsChanged = !ImmutableIs(prevProps.otherAccounts, this.props.otherAccounts);
|
|
|
|
|
|
|
|
if (accountChanged || otherAccountsChanged) {
|
|
|
|
this.fetchOwnAccounts();
|
|
|
|
}
|
2021-03-23 19:01:50 -07:00
|
|
|
}
|
|
|
|
|
2021-03-25 11:23:11 -07:00
|
|
|
renderAccount = account => {
|
|
|
|
return (
|
|
|
|
<div className='account'>
|
|
|
|
<div className='account__wrapper'>
|
2021-03-29 21:22:54 -07:00
|
|
|
<div className='account__display-name'>
|
2021-03-25 11:23:11 -07:00
|
|
|
<div className='account__avatar-wrapper'><Avatar account={account} size={36} /></div>
|
|
|
|
<DisplayName account={account} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-23 17:06:55 -07:00
|
|
|
render() {
|
|
|
|
const { intl, account, otherAccounts } = this.props;
|
|
|
|
const size = this.props.size || 16;
|
|
|
|
|
2021-08-03 10:10:42 -07:00
|
|
|
const menu = [];
|
2021-03-23 17:06:55 -07:00
|
|
|
|
2021-03-29 21:26:50 -07:00
|
|
|
menu.push({ text: this.renderAccount(account), to: `/@${account.get('acct')}` });
|
2021-03-27 10:16:45 -07:00
|
|
|
|
2021-03-23 17:06:55 -07:00
|
|
|
otherAccounts.forEach(account => {
|
2021-03-29 21:22:54 -07:00
|
|
|
menu.push({ text: this.renderAccount(account), action: this.handleSwitchAccount(account), href: '/', middleClick: this.handleMiddleClick(account) });
|
2021-03-23 17:06:55 -07:00
|
|
|
});
|
|
|
|
|
2021-03-27 10:16:45 -07:00
|
|
|
menu.push(null);
|
2021-03-23 17:06:55 -07:00
|
|
|
|
2021-03-25 11:47:01 -07:00
|
|
|
menu.push({ text: intl.formatMessage(messages.add), to: '/auth/sign_in' });
|
2021-03-23 17:06:55 -07:00
|
|
|
menu.push({ text: intl.formatMessage(messages.logout, { acct: account.get('acct') }), to: '/auth/sign_out', action: this.handleLogOut });
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='compose__action-bar' style={{ 'marginTop':'-6px' }}>
|
|
|
|
<div className='compose__action-bar-dropdown'>
|
|
|
|
<DropdownMenuContainer items={menu} icon='chevron-down' size={size} direction='right' />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-01 17:31:27 -07:00
|
|
|
export default injectIntl(connect(makeMapStateToProps)(ProfileDropdown));
|