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';
|
2020-04-10 17:32:16 -07:00
|
|
|
import { Link, NavLink, withRouter } from 'react-router-dom';
|
2020-03-27 13:59:38 -07:00
|
|
|
import { FormattedMessage, injectIntl } from 'react-intl';
|
|
|
|
import { throttle } from 'lodash';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import NotificationsCounterIcon from './notifications_counter_icon';
|
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';
|
2020-05-28 15:52:07 -07:00
|
|
|
import ActionBar from 'soapbox/features/compose/components/action_bar';
|
2020-03-27 13:59:38 -07:00
|
|
|
import { openModal } from '../../../actions/modal';
|
|
|
|
import { openSidebar } from '../../../actions/sidebar';
|
2020-04-28 20:00:50 -07:00
|
|
|
import Icon from '../../../components/icon';
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
@withRouter
|
|
|
|
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,
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
state = {
|
|
|
|
collapsed: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
router: PropTypes.object,
|
|
|
|
}
|
|
|
|
|
|
|
|
lastScrollTop = 0;
|
|
|
|
|
2020-04-14 14:47:35 -07:00
|
|
|
componentDidMount() {
|
2020-03-27 13:59:38 -07:00
|
|
|
this.window = window;
|
|
|
|
this.documentElement = document.scrollingElement || document.documentElement;
|
|
|
|
|
|
|
|
this.attachScrollListener();
|
|
|
|
// Handle initial scroll posiiton
|
|
|
|
this.handleScroll();
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:47:35 -07:00
|
|
|
componentWillUnmount() {
|
2020-03-27 13:59:38 -07:00
|
|
|
this.detachScrollListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
setRef = ref => {
|
|
|
|
this.node = ref;
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:47:35 -07:00
|
|
|
attachScrollListener() {
|
2020-03-27 13:59:38 -07:00
|
|
|
this.window.addEventListener('scroll', this.handleScroll);
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:47:35 -07:00
|
|
|
detachScrollListener() {
|
2020-03-27 13:59:38 -07:00
|
|
|
this.window.removeEventListener('scroll', this.handleScroll);
|
|
|
|
}
|
|
|
|
|
2020-04-10 17:32:16 -07:00
|
|
|
getNavLinks() {
|
|
|
|
const { intl: { formatMessage }, logo, account } = this.props;
|
2020-04-01 16:10:48 -07:00
|
|
|
let links = [];
|
|
|
|
if (logo) {
|
|
|
|
links.push(
|
2020-04-28 22:36:25 -07:00
|
|
|
<Link key='logo' className='tabs-bar__link--logo' to='/' data-preview-title-id='column.home'>
|
|
|
|
<img alt='Logo' src={logo} />
|
2020-04-01 16:10:48 -07:00
|
|
|
<FormattedMessage id='tabs_bar.home' defaultMessage='Home' />
|
2020-04-14 11:44:40 -07:00
|
|
|
</Link>);
|
2020-04-01 16:10:48 -07:00
|
|
|
}
|
|
|
|
links.push(
|
2020-04-10 18:16:34 -07:00
|
|
|
<NavLink key='home' className='tabs-bar__link' exact to='/' data-preview-title-id='column.home'>
|
2020-04-28 20:00:50 -07:00
|
|
|
<Icon id='home' />
|
2020-04-01 16:10:48 -07:00
|
|
|
<FormattedMessage id='tabs_bar.home' defaultMessage='Home' />
|
2020-04-14 11:44:40 -07:00
|
|
|
</NavLink>);
|
2020-04-10 17:32:16 -07:00
|
|
|
if (account) {
|
|
|
|
links.push(
|
|
|
|
<NavLink key='notifications' className='tabs-bar__link' to='/notifications' data-preview-title-id='column.notifications'>
|
2020-04-28 20:00:50 -07:00
|
|
|
<Icon id='bell' />
|
2020-04-10 17:32:16 -07:00
|
|
|
<NotificationsCounterIcon />
|
|
|
|
<FormattedMessage id='tabs_bar.notifications' defaultMessage='Notifications' />
|
2020-04-14 11:44:40 -07:00
|
|
|
</NavLink>);
|
2020-04-10 17:32:16 -07:00
|
|
|
}
|
|
|
|
links.push(
|
|
|
|
<NavLink key='search' className='tabs-bar__link tabs-bar__link--search' to='/search' data-preview-title-id='tabs_bar.search'>
|
2020-04-28 20:00:50 -07:00
|
|
|
<Icon id='search' />
|
2020-04-01 16:10:48 -07:00
|
|
|
<FormattedMessage id='tabs_bar.search' defaultMessage='Search' />
|
2020-04-10 17:32:16 -07:00
|
|
|
</NavLink>
|
2020-04-01 16:10:48 -07:00
|
|
|
);
|
|
|
|
return links.map((link) =>
|
|
|
|
React.cloneElement(link, {
|
|
|
|
'aria-label': formatMessage({
|
2020-04-14 11:44:40 -07:00
|
|
|
id: link.props['data-preview-title-id'],
|
|
|
|
}),
|
|
|
|
}));
|
2020-04-01 16:10:48 -07:00
|
|
|
}
|
|
|
|
|
2020-03-27 13:59:38 -07:00
|
|
|
handleScroll = throttle(() => {
|
|
|
|
if (this.window) {
|
|
|
|
const { pageYOffset, innerWidth } = this.window;
|
|
|
|
if (innerWidth > 895) return;
|
|
|
|
const { scrollTop } = this.documentElement;
|
|
|
|
|
|
|
|
let st = pageYOffset || scrollTop;
|
|
|
|
if (st > this.lastScrollTop){
|
|
|
|
let offset = st - this.lastScrollTop;
|
2020-04-14 11:44:40 -07:00
|
|
|
if (offset > 50) this.setState({ collapsed: true });
|
2020-03-27 13:59:38 -07:00
|
|
|
} else {
|
|
|
|
let offset = this.lastScrollTop - st;
|
2020-04-14 11:44:40 -07:00
|
|
|
if (offset > 50) this.setState({ collapsed: false });
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this.lastScrollTop = st <= 0 ? 0 : st;
|
|
|
|
}
|
|
|
|
}, 150, {
|
|
|
|
trailing: true,
|
|
|
|
});
|
|
|
|
|
2020-04-14 14:47:35 -07:00
|
|
|
render() {
|
2020-04-14 13:45:38 -07:00
|
|
|
const { account, onOpenCompose, onOpenSidebar } = this.props;
|
2020-03-27 13:59:38 -07:00
|
|
|
const { collapsed } = this.state;
|
|
|
|
|
|
|
|
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'>
|
2020-04-10 17:32:16 -07:00
|
|
|
{this.getNavLinks()}
|
2020-03-27 13:59:38 -07:00
|
|
|
</div>
|
|
|
|
<div className='tabs-bar__split tabs-bar__split--right'>
|
|
|
|
<div className='tabs-bar__search-container'>
|
|
|
|
<SearchContainer openInRoute />
|
|
|
|
</div>
|
|
|
|
{ account &&
|
|
|
|
<div className='flex'>
|
|
|
|
<div className='tabs-bar__profile'>
|
|
|
|
<Avatar account={account} />
|
2020-04-14 11:44:40 -07:00
|
|
|
<button className='tabs-bar__sidebar-btn' onClick={onOpenSidebar} />
|
2020-03-27 13:59:38 -07:00
|
|
|
<ActionBar account={account} size={34} />
|
|
|
|
</div>
|
2020-05-21 15:49:00 -07:00
|
|
|
<button className='tabs-bar__button-compose button' onClick={onOpenCompose} aria-label='Post'>
|
|
|
|
<span>Post</span>
|
2020-03-27 13:59:38 -07:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
{
|
|
|
|
!account &&
|
|
|
|
<div className='flex'>
|
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>
|
|
|
|
<Link className='tabs-bar__button button button-alternative-2' to='/auth/sign_up'>
|
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>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</nav>
|
|
|
|
);
|
|
|
|
}
|
2020-04-14 11:44:40 -07:00
|
|
|
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = state => {
|
2020-04-01 19:20:47 -07:00
|
|
|
const me = state.get('me');
|
2020-03-27 13:59:38 -07:00
|
|
|
return {
|
|
|
|
account: state.getIn(['accounts', me]),
|
2020-04-01 15:15:29 -07:00
|
|
|
logo: state.getIn(['soapbox', 'logo']),
|
2020-03-27 13:59:38 -07:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
|
|
|
onOpenCompose() {
|
|
|
|
dispatch(openModal('COMPOSE'));
|
|
|
|
},
|
|
|
|
onOpenSidebar() {
|
|
|
|
dispatch(openSidebar());
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export default injectIntl(
|
|
|
|
connect(mapStateToProps, mapDispatchToProps, null, { forwardRef: true }
|
2020-04-14 11:44:40 -07:00
|
|
|
)(TabsBar));
|