bigbuffet-rw/app/soapbox/features/ui/components/tabs_bar.js

191 lines
7.4 KiB
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
import React from 'react';
import PropTypes from 'prop-types';
2020-04-14 14:37:17 -07:00
import ImmutablePropTypes from 'react-immutable-proptypes';
import { Link, NavLink, withRouter } from 'react-router-dom';
2020-06-06 12:57:05 -07:00
import { FormattedMessage, injectIntl, defineMessages } from 'react-intl';
2020-03-27 13:59:38 -07:00
import { connect } from 'react-redux';
import classNames from 'classnames';
import IconWithCounter from 'soapbox/components/icon_with_counter';
2020-05-28 15:52:07 -07:00
import SearchContainer from 'soapbox/features/compose/containers/search_container';
2020-03-27 13:59:38 -07:00
import Avatar from '../../../components/avatar';
import Icon from 'soapbox/components/icon';
import ProfileDropdown from './profile_dropdown';
2020-03-27 13:59:38 -07:00
import { openModal } from '../../../actions/modal';
import { openSidebar } from '../../../actions/sidebar';
import ThemeToggle from '../../ui/components/theme_toggle_container';
import { getSoapboxConfig } from 'soapbox/actions/soapbox';
import { isStaff } from 'soapbox/utils/accounts';
import { getFeatures } from 'soapbox/utils/features';
2020-03-27 13:59:38 -07:00
2020-06-06 12:57:05 -07:00
const messages = defineMessages({
post: { id: 'tabs_bar.post', defaultMessage: 'Post' },
});
2020-03-27 13:59:38 -07:00
class TabsBar extends React.PureComponent {
static propTypes = {
intl: PropTypes.object.isRequired,
history: PropTypes.object.isRequired,
onOpenCompose: PropTypes.func,
onOpenSidebar: PropTypes.func.isRequired,
2020-04-14 14:37:17 -07:00
logo: PropTypes.string,
account: ImmutablePropTypes.map,
features: PropTypes.object.isRequired,
dashboardCount: PropTypes.number,
notificationCount: PropTypes.number,
chatsCount: PropTypes.number,
2020-03-27 13:59:38 -07:00
}
state = {
collapsed: false,
}
static contextTypes = {
router: PropTypes.object,
}
setRef = ref => {
this.node = ref;
}
isHomeActive = (match, location) => {
const { pathname } = location;
return pathname === '/' || pathname.startsWith('/timeline/');
}
onProfilePage = () => {
try {
const { pathname } = this.context.router.route.location;
return pathname.startsWith('/@') && !pathname.includes('/posts/');
} catch {
return false;
}
}
render() {
const { intl, account, logo, onOpenCompose, onOpenSidebar, features, dashboardCount, notificationCount, chatsCount } = this.props;
2020-07-11 13:22:39 -07:00
const { collapsed } = this.state;
const profilePage = this.onProfilePage();
2020-03-27 13:59:38 -07:00
const classes = classNames('tabs-bar', {
'tabs-bar--collapsed': collapsed,
2020-04-14 11:44:40 -07:00
});
2020-03-27 13:59:38 -07:00
return (
<nav className={classes} ref={this.setRef}>
<div className='tabs-bar__container'>
<div className='tabs-bar__split tabs-bar__split--left'>
{logo ? (
2021-09-12 16:51:39 -07:00
<Link key='logo' className='tabs-bar__link--logo' to='/' data-preview-title-id='column.home'>
<img alt='Logo' src={logo} />
<span><FormattedMessage id='tabs_bar.home' defaultMessage='Home' /></span>
</Link>
) : (
<Link key='logo' className='tabs-bar__link--logo' to='/' data-preview-title-id='column.home'>
2021-10-05 15:58:25 -07:00
<Icon alt='Logo' src={require('icons/home-square.svg')} />
<span><FormattedMessage id='tabs_bar.home' defaultMessage='Home' /></span>
</Link>
2021-09-12 16:51:39 -07:00
)}
2021-09-12 17:08:41 -07:00
2020-03-27 13:59:38 -07:00
<div className='tabs-bar__search-container'>
<SearchContainer openInRoute autosuggest />
2020-03-27 13:59:38 -07:00
</div>
2021-09-12 17:08:41 -07:00
</div>
<div className='tabs-bar__split tabs-bar__split--right'>
{account ? (
2020-07-20 13:34:54 -07:00
<>
{profilePage && (
<>
<NavLink key='notifications' className='tabs-bar__link' to='/notifications' data-preview-title-id='column.notifications'>
<IconWithCounter
src={require('@tabler/icons/icons/bell.svg')}
className={classNames('primary-navigation__icon', {
'svg-icon--active': location.pathname === '/notifications',
'svg-icon--unread': notificationCount > 0,
})}
count={notificationCount}
/>
<span><FormattedMessage id='tabs_bar.notifications' defaultMessage='Notifications' /></span>
</NavLink>
{features.chats && (
<NavLink key='chats' className='tabs-bar__link' to='/chats' data-preview-title-id='column.chats'>
<IconWithCounter
src={require('@tabler/icons/icons/messages.svg')}
className={classNames('primary-navigation__icon', { 'svg-icon--active': location.pathname === '/chats' })}
count={chatsCount}
/>
<span><FormattedMessage id='tabs_bar.chats' defaultMessage='Chats' /></span>
</NavLink>
)}
{isStaff(account) && (
<NavLink key='dashboard' className='tabs-bar__link' to='/admin' data-preview-title-id='tabs_bar.dashboard'>
<IconWithCounter
src={location.pathname.startsWith('/admin') ? require('icons/dashboard-filled.svg') : require('@tabler/icons/icons/dashboard.svg')}
className='primary-navigation__icon'
count={dashboardCount}
/>
<span><FormattedMessage id='tabs_bar.dashboard' defaultMessage='Dashboard' /></span>
</NavLink>
)}
</>
)}
<ThemeToggle />
2020-03-27 13:59:38 -07:00
<div className='tabs-bar__profile'>
<Avatar account={account} />
2020-04-14 11:44:40 -07:00
<button className='tabs-bar__sidebar-btn' onClick={onOpenSidebar} />
<ProfileDropdown account={account} size={34} />
2020-03-27 13:59:38 -07:00
</div>
2020-06-06 12:57:05 -07:00
<button className='tabs-bar__button-compose button' onClick={onOpenCompose} aria-label={intl.formatMessage(messages.post)}>
<span>{intl.formatMessage(messages.post)}</span>
2020-03-27 13:59:38 -07:00
</button>
2020-07-20 13:34:54 -07:00
</>
) : (
<div className='tabs-bar__unauthenticated'>
2020-04-10 17:32:16 -07:00
<Link className='tabs-bar__button button' to='/auth/sign_in'>
2020-03-27 13:59:38 -07:00
<FormattedMessage id='account.login' defaultMessage='Log In' />
2020-04-10 17:32:16 -07:00
</Link>
2020-06-07 14:55:28 -07:00
<Link className='tabs-bar__button button button-alternative-2' to='/'>
2020-03-27 13:59:38 -07:00
<FormattedMessage id='account.register' defaultMessage='Sign up' />
2020-04-10 17:32:16 -07:00
</Link>
2020-03-27 13:59:38 -07:00
</div>
)}
2020-03-27 13:59:38 -07:00
</div>
</div>
</nav>
);
}
2020-04-14 11:44:40 -07:00
2020-03-27 13:59:38 -07:00
}
const mapStateToProps = state => {
const me = state.get('me');
const reportsCount = state.getIn(['admin', 'openReports']).count();
const approvalCount = state.getIn(['admin', 'awaitingApproval']).count();
const instance = state.get('instance');
2021-08-20 13:46:17 -07:00
2020-03-27 13:59:38 -07:00
return {
account: state.getIn(['accounts', me]),
logo: getSoapboxConfig(state).get('logo'),
features: getFeatures(instance),
notificationCount: state.getIn(['notifications', 'unread']),
chatsCount: state.get('chats').reduce((acc, curr) => acc + Math.min(curr.get('unread', 0), 1), 0),
dashboardCount: reportsCount + approvalCount,
2020-03-27 13:59:38 -07:00
};
};
const mapDispatchToProps = (dispatch) => ({
onOpenCompose() {
dispatch(openModal('COMPOSE'));
},
onOpenSidebar() {
dispatch(openSidebar());
},
});
export default withRouter(injectIntl(
connect(mapStateToProps, mapDispatchToProps, null, { forwardRef: true },
)(TabsBar)));