bigbuffet-rw/app/soapbox/components/sidebar_menu.js

354 lines
17 KiB
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
2021-03-26 20:34:30 -07:00
import { throttle } from 'lodash';
2020-03-27 13:59:38 -07:00
import { Link, NavLink } from 'react-router-dom';
import ImmutablePropTypes from 'react-immutable-proptypes';
import ImmutablePureComponent from 'react-immutable-pure-component';
import { injectIntl, defineMessages, FormattedMessage } from 'react-intl';
2020-03-27 13:59:38 -07:00
import classNames from 'classnames';
import Avatar from './avatar';
import IconButton from './icon_button';
import Icon from './icon';
import DisplayName from './display_name';
import { closeSidebar } from '../actions/sidebar';
import { isAdmin, getBaseURL } from '../utils/accounts';
import { makeGetAccount, makeGetOtherAccounts } from '../selectors';
2021-03-26 20:34:30 -07:00
import { logOut, switchAccount } from 'soapbox/actions/auth';
import ThemeToggle from '../features/ui/components/theme_toggle_container';
2021-03-26 20:34:30 -07:00
import { fetchOwnAccounts } from 'soapbox/actions/auth';
import { is as ImmutableIs } from 'immutable';
import { getSettings } from 'soapbox/actions/settings';
import { getSoapboxConfig } from 'soapbox/actions/soapbox';
import { getFeatures } from 'soapbox/utils/features';
2020-03-27 13:59:38 -07:00
const messages = defineMessages({
followers: { id: 'account.followers', defaultMessage: 'Followers' },
follows: { id: 'account.follows', defaultMessage: 'Follows' },
profile: { id: 'account.profile', defaultMessage: 'Profile' },
2021-11-02 10:03:58 -07:00
invites: { id: 'navigation_bar.invites', defaultMessage: 'Invites' },
2020-03-27 13:59:38 -07:00
preferences: { id: 'navigation_bar.preferences', defaultMessage: 'Preferences' },
follow_requests: { id: 'navigation_bar.follow_requests', defaultMessage: 'Follow requests' },
blocks: { id: 'navigation_bar.blocks', defaultMessage: 'Blocked users' },
domain_blocks: { id: 'navigation_bar.domain_blocks', defaultMessage: 'Hidden domains' },
mutes: { id: 'navigation_bar.mutes', defaultMessage: 'Muted users' },
filters: { id: 'navigation_bar.filters', defaultMessage: 'Muted words' },
2020-05-18 16:48:33 -07:00
admin_settings: { id: 'navigation_bar.admin_settings', defaultMessage: 'Admin settings' },
2020-08-23 13:21:19 -07:00
soapbox_config: { id: 'navigation_bar.soapbox_config', defaultMessage: 'Soapbox config' },
2020-09-27 09:18:25 -07:00
import_data: { id: 'navigation_bar.import_data', defaultMessage: 'Import data' },
account_aliases: { id: 'navigation_bar.account_aliases', defaultMessage: 'Account aliases' },
2020-06-05 12:58:59 -07:00
security: { id: 'navigation_bar.security', defaultMessage: 'Security' },
2020-03-27 13:59:38 -07:00
logout: { id: 'navigation_bar.logout', defaultMessage: 'Logout' },
2020-04-14 11:44:40 -07:00
lists: { id: 'column.lists', defaultMessage: 'Lists' },
2020-07-29 14:08:36 -07:00
bookmarks: { id: 'column.bookmarks', defaultMessage: 'Bookmarks' },
header: { id: 'tabs_bar.header', defaultMessage: 'Account Info' },
2020-03-27 13:59:38 -07:00
apps: { id: 'tabs_bar.apps', defaultMessage: 'Apps' },
news: { id: 'tabs_bar.news', defaultMessage: 'News' },
donate: { id: 'donate', defaultMessage: 'Donate' },
donate_crypto: { id: 'donate_crypto', defaultMessage: 'Donate cryptocurrency' },
info: { id: 'column.info', defaultMessage: 'Server information' },
developers: { id: 'navigation.developers', defaultMessage: 'Developers' },
2021-03-26 20:34:30 -07:00
add_account: { id: 'profile_dropdown.add_account', defaultMessage: 'Add an existing account' },
2020-04-14 11:44:40 -07:00
});
2020-03-27 13:59:38 -07:00
const makeMapStateToProps = () => {
2020-03-27 13:59:38 -07:00
const getAccount = makeGetAccount();
const getOtherAccounts = makeGetOtherAccounts();
2020-03-27 13:59:38 -07:00
const mapStateToProps = state => {
const me = state.get('me');
const account = state.getIn(['accounts', me]);
const instance = state.get('instance');
const features = getFeatures(instance);
const soapbox = getSoapboxConfig(state);
2021-03-26 20:34:30 -07:00
return {
account: getAccount(state, me),
sidebarOpen: state.get('sidebar').sidebarOpen,
donateUrl: state.getIn(['patron', 'instance', 'url']),
hasCrypto: typeof soapbox.getIn(['cryptoAddresses', 0, 'ticker']) === 'string',
2021-07-10 02:48:12 -07:00
otherAccounts: getOtherAccounts(state),
features,
2021-11-02 10:03:58 -07:00
instance,
settings: getSettings(state),
siteTitle: instance.get('title'),
baseURL: getBaseURL(account),
};
2020-03-27 13:59:38 -07:00
};
return mapStateToProps;
2020-03-27 13:59:38 -07:00
};
const mapDispatchToProps = (dispatch, { intl }) => ({
onClose() {
2020-03-27 13:59:38 -07:00
dispatch(closeSidebar());
},
2020-04-11 12:41:13 -07:00
onClickLogOut(e) {
dispatch(logOut(intl));
2020-04-11 12:41:13 -07:00
e.preventDefault();
},
2021-03-26 20:34:30 -07:00
fetchOwnAccounts() {
dispatch(fetchOwnAccounts());
},
switchAccount(account) {
dispatch(switchAccount(account.get('id')));
},
2020-03-27 13:59:38 -07:00
});
export default @injectIntl
@connect(makeMapStateToProps, mapDispatchToProps)
2020-03-27 13:59:38 -07:00
class SidebarMenu extends ImmutablePureComponent {
static propTypes = {
intl: PropTypes.object.isRequired,
account: ImmutablePropTypes.map,
otherAccounts: ImmutablePropTypes.list,
2020-03-27 13:59:38 -07:00
sidebarOpen: PropTypes.bool,
onClose: PropTypes.func.isRequired,
settings: PropTypes.object.isRequired,
features: PropTypes.object.isRequired,
2021-11-02 10:03:58 -07:00
instance: ImmutablePropTypes.map.isRequired,
baseURL: PropTypes.string,
2020-03-27 13:59:38 -07:00
};
2021-03-26 20:34:30 -07:00
state = {
switcher: false,
}
handleClose = () => {
this.setState({ switcher: false });
this.props.onClose();
}
2021-03-26 20:34:30 -07:00
handleSwitchAccount = account => {
return e => {
this.props.switchAccount(account);
e.preventDefault();
};
}
handleSwitcherClick = e => {
this.setState({ switcher: !this.state.switcher });
e.preventDefault();
}
fetchOwnAccounts = throttle(() => {
this.props.fetchOwnAccounts();
}, 2000);
componentDidMount() {
this.fetchOwnAccounts();
}
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-26 20:34:30 -07:00
}
renderAccount = account => {
return (
2021-03-29 22:46:21 -07:00
<a href='/' className='sidebar-account' onClick={this.handleSwitchAccount(account)} key={account.get('id')}>
2021-03-26 20:34:30 -07:00
<div className='account'>
<div className='account__wrapper'>
2021-03-29 22:46:21 -07:00
<div className='account__display-name'>
2021-03-26 20:34:30 -07:00
<div className='account__avatar-wrapper'><Avatar account={account} size={36} /></div>
<DisplayName account={account} />
</div>
</div>
</div>
</a>
);
}
render() {
2021-11-02 10:03:58 -07:00
const { sidebarOpen, intl, account, onClickLogOut, donateUrl, otherAccounts, hasCrypto, settings, features, instance, siteTitle, baseURL } = this.props;
2021-03-26 20:34:30 -07:00
const { switcher } = this.state;
if (!account) return null;
2020-03-27 13:59:38 -07:00
const acct = account.get('acct');
const classes = classNames('sidebar-menu__root', {
'sidebar-menu__root--visible': sidebarOpen,
});
return (
<div className={classes}>
<div className='sidebar-menu__wrapper' role='button' onClick={this.handleClose} />
2020-03-27 13:59:38 -07:00
<div className='sidebar-menu'>
<div className='sidebar-menu__content'>
<div className='sidebar-menu-profile'>
<IconButton title='close' onClick={this.handleClose} src={require('@tabler/icons/icons/x.svg')} className='sidebar-menu__close' />
2020-03-27 13:59:38 -07:00
<div className='sidebar-menu-profile__avatar'>
<Link to={`/@${acct}`} title={acct} onClick={this.handleClose}>
2020-03-27 13:59:38 -07:00
<Avatar account={account} />
</Link>
</div>
2021-03-26 20:34:30 -07:00
<a href='#' className='sidebar-menu-profile__name' onClick={this.handleSwitcherClick}>
2020-04-14 11:44:40 -07:00
<DisplayName account={account} />
<Icon src={switcher ? require('@tabler/icons/icons/caret-up.svg') : require('@tabler/icons/icons/caret-down.svg')} className='sidebar-menu-profile__caret' />
2021-03-26 20:34:30 -07:00
</a>
</div>
2020-03-27 13:59:38 -07:00
2021-03-26 20:34:30 -07:00
{switcher && <div className='sidebar-menu__section'>
{otherAccounts.map(account => this.renderAccount(account))}
2020-03-27 13:59:38 -07:00
<NavLink className='sidebar-menu-item' to='/auth/sign_in' onClick={this.handleClose}>
<Icon src={require('@tabler/icons/icons/plus.svg')} />
2021-03-26 20:34:30 -07:00
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.add_account)}</span>
</NavLink>
</div>}
2020-03-27 13:59:38 -07:00
2021-03-26 20:34:30 -07:00
<div className='sidebar-menu__section'>
<div className='sidebar-menu-item theme-toggle'>
<ThemeToggle showLabel />
</div>
</div>
<div className='sidebar-menu__section'>
{features.federating ? (
<NavLink to='/timeline/local' className='sidebar-menu-item' onClick={this.handleClose}>
<Icon src={require('@tabler/icons/icons/users.svg')} />
<span className='sidebar-menu-item__title'>{siteTitle}</span>
</NavLink>
) : (
<NavLink to='/timeline/local' className='sidebar-menu-item' onClick={this.handleClose}>
<Icon src={require('@tabler/icons/icons/world.svg')} />
<span className='sidebar-menu-item__title'><FormattedMessage id='tabs_bar.all' defaultMessage='All' /></span>
</NavLink>
)}
{features.federating && <NavLink to='/timeline/fediverse' className='sidebar-menu-item' onClick={this.handleClose}>
<Icon src={require('icons/fediverse.svg')} />
<span className='sidebar-menu-item__title'><FormattedMessage id='tabs_bar.fediverse' defaultMessage='Fediverse' /></span>
</NavLink>}
</div>
<div className='sidebar-menu__section'>
<NavLink className='sidebar-menu-item' to={`/@${acct}`} onClick={this.handleClose}>
2021-09-20 13:33:28 -07:00
<Icon src={require('@tabler/icons/icons/user.svg')} />
2020-03-27 13:59:38 -07:00
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.profile)}</span>
</NavLink>
2021-11-02 10:03:58 -07:00
{instance.get('invites_enabled') && <a className='sidebar-menu-item' href={`${baseURL}/invites`} onClick={this.handleClose}>
<Icon src={require('@tabler/icons/icons/mailbox.svg')} />
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.invites)}</span>
</a>}
{donateUrl && <a className='sidebar-menu-item' href={donateUrl} onClick={this.handleClose}>
2021-09-20 13:33:28 -07:00
<Icon src={require('@tabler/icons/icons/coin.svg')} />
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.donate)}</span>
</a>}
{hasCrypto && <NavLink className='sidebar-menu-item' to='/donate/crypto' onClick={this.handleClose}>
2021-09-20 13:33:28 -07:00
<Icon src={require('@tabler/icons/icons/currency-bitcoin.svg')} />
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.donate_crypto)}</span>
</NavLink>}
{features.lists && <NavLink className='sidebar-menu-item' to='/lists' onClick={this.handleClose}>
2021-09-20 13:33:28 -07:00
<Icon src={require('@tabler/icons/icons/list.svg')} />
2020-03-27 13:59:38 -07:00
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.lists)}</span>
</NavLink>}
{features.bookmarks && <NavLink className='sidebar-menu-item' to='/bookmarks' onClick={this.handleClose}>
2021-09-20 13:33:28 -07:00
<Icon src={require('@tabler/icons/icons/bookmarks.svg')} />
2020-07-29 14:08:36 -07:00
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.bookmarks)}</span>
</NavLink>}
2020-03-27 13:59:38 -07:00
</div>
<div className='sidebar-menu__section'>
<NavLink className='sidebar-menu-item' to='/follow_requests' onClick={this.handleClose}>
2021-09-20 13:33:28 -07:00
<Icon src={require('@tabler/icons/icons/user-plus.svg')} />
2020-03-27 13:59:38 -07:00
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.follow_requests)}</span>
</NavLink>
<NavLink className='sidebar-menu-item' to='/blocks' onClick={this.handleClose}>
2021-09-20 13:33:28 -07:00
<Icon src={require('@tabler/icons/icons/ban.svg')} />
2020-03-27 13:59:38 -07:00
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.blocks)}</span>
</NavLink>
{features.federating && <NavLink className='sidebar-menu-item' to='/domain_blocks' onClick={this.handleClose}>
2021-09-20 13:33:28 -07:00
<Icon src={require('@tabler/icons/icons/ban.svg')} />
2020-03-27 13:59:38 -07:00
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.domain_blocks)}</span>
</NavLink>}
<NavLink className='sidebar-menu-item' to='/mutes' onClick={this.handleClose}>
2021-09-20 13:33:28 -07:00
<Icon src={require('@tabler/icons/icons/circle-x.svg')} />
2020-03-27 13:59:38 -07:00
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.mutes)}</span>
</NavLink>
<NavLink className='sidebar-menu-item' to='/filters' onClick={this.handleClose}>
2021-09-20 13:33:28 -07:00
<Icon src={require('@tabler/icons/icons/filter.svg')} />
2020-03-27 13:59:38 -07:00
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.filters)}</span>
2020-08-04 13:21:42 -07:00
</NavLink>
{isAdmin(account) && <a className='sidebar-menu-item' href='/pleroma/admin' target='_blank' onClick={this.handleClose}>
2021-09-20 13:33:28 -07:00
<Icon src={require('@tabler/icons/icons/shield.svg')} />
2020-05-18 16:48:33 -07:00
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.admin_settings)}</span>
</a>}
{isAdmin(account) && <NavLink className='sidebar-menu-item' to='/soapbox/config' onClick={this.handleClose}>
2021-09-20 13:33:28 -07:00
<Icon src={require('@tabler/icons/icons/settings.svg')} />
2020-08-23 13:21:19 -07:00
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.soapbox_config)}</span>
</NavLink>}
{features.settingsStore ? (
<NavLink className='sidebar-menu-item' to='/settings/preferences' onClick={this.handleClose}>
<Icon src={require('@tabler/icons/icons/settings.svg')} />
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.preferences)}</span>
</NavLink>
) : (
<a className='sidebar-menu-item' href={`${baseURL}/settings/preferences`} onClick={this.handleClose}>
<Icon src={require('@tabler/icons/icons/settings.svg')} />
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.preferences)}</span>
</a>
)}
{features.importAPI ? (
<NavLink className='sidebar-menu-item' to='/settings/import' onClick={this.handleClose}>
<Icon src={require('@tabler/icons/icons/cloud-upload.svg')} />
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.import_data)}</span>
</NavLink>
) : (
<a className='sidebar-menu-item' href={`${baseURL}/settings/import`} onClick={this.handleClose}>
<Icon src={require('@tabler/icons/icons/cloud-upload.svg')} />
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.import_data)}</span>
</a>
)}
{(features.federating && features.accountAliasesAPI) && <NavLink className='sidebar-menu-item' to='/settings/aliases' onClick={this.handleClose}>
<Icon src={require('feather-icons/dist/icons/briefcase.svg')} />
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.account_aliases)}</span>
</NavLink>}
{features.securityAPI ? (
<NavLink className='sidebar-menu-item' to='/auth/edit' onClick={this.handleClose}>
<Icon src={require('@tabler/icons/icons/shield-lock.svg')} />
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.security)}</span>
</NavLink>
) : (
<a className='sidebar-menu-item' href={`${baseURL}/auth/edit`} onClick={this.handleClose}>
<Icon src={require('@tabler/icons/icons/shield-lock.svg')} />
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.security)}</span>
</a>
)}
2020-03-27 13:59:38 -07:00
</div>
<div className='sidebar-menu__section'>
<Link className='sidebar-menu-item' to='/info' onClick={this.handleClose}>
2021-09-20 13:33:28 -07:00
<Icon src={require('@tabler/icons/icons/info-circle.svg')} />
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.info)}</span>
</Link>
</div>
{(settings.get('isDeveloper')) && (
<Link className='sidebar-menu-item' to='/developers' onClick={this.handleClose}>
<Icon src={require('@tabler/icons/icons/code.svg')} />
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.developers)}</span>
</Link>
)}
2020-03-27 13:59:38 -07:00
<div className='sidebar-menu__section'>
2020-04-11 12:41:13 -07:00
<Link className='sidebar-menu-item' to='/auth/sign_out' onClick={onClickLogOut}>
2021-09-20 13:33:28 -07:00
<Icon src={require('@tabler/icons/icons/logout.svg')} />
2020-03-27 13:59:38 -07:00
<span className='sidebar-menu-item__title'>{intl.formatMessage(messages.logout)}</span>
2020-04-11 12:41:13 -07:00
</Link>
2020-03-27 13:59:38 -07:00
</div>
</div>
</div>
</div>
);
}
}