2021-09-12 16:16:53 -07:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2021-10-15 15:11:16 -07:00
|
|
|
import { connect } from 'react-redux';
|
2021-10-15 16:37:32 -07:00
|
|
|
import { injectIntl, defineMessages } from 'react-intl';
|
2021-10-08 17:09:34 -07:00
|
|
|
import { throttle } from 'lodash';
|
2022-01-10 14:01:24 -08:00
|
|
|
import classNames from 'classnames';
|
2021-09-12 16:16:53 -07:00
|
|
|
import Icon from 'soapbox/components/icon';
|
2021-10-15 15:11:16 -07:00
|
|
|
import IconButton from 'soapbox/components/icon_button';
|
2021-10-14 11:26:07 -07:00
|
|
|
import Helmet from 'soapbox/components/helmet';
|
2021-10-15 15:11:16 -07:00
|
|
|
import { openModal } from 'soapbox/actions/modal';
|
2021-09-12 16:16:53 -07:00
|
|
|
|
2021-10-15 16:37:32 -07:00
|
|
|
const messages = defineMessages({
|
|
|
|
back: { id: 'column_back_button.label', defaultMessage: 'Back' },
|
|
|
|
settings: { id: 'column_header.show_settings', defaultMessage: 'Show settings' },
|
|
|
|
});
|
|
|
|
|
2021-10-15 15:11:16 -07:00
|
|
|
const mapDispatchToProps = (dispatch, { settings: Settings }) => {
|
|
|
|
return {
|
|
|
|
onOpenSettings() {
|
|
|
|
dispatch(openModal('COMPONENT', { component: Settings }));
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default @connect(undefined, mapDispatchToProps)
|
2021-10-15 16:37:32 -07:00
|
|
|
@injectIntl
|
2021-10-15 15:11:16 -07:00
|
|
|
class SubNavigation extends React.PureComponent {
|
2021-09-12 16:16:53 -07:00
|
|
|
|
|
|
|
static propTypes = {
|
2021-10-15 16:37:32 -07:00
|
|
|
intl: PropTypes.object.isRequired,
|
2021-09-27 11:38:02 -07:00
|
|
|
message: PropTypes.string,
|
2021-10-15 15:11:16 -07:00
|
|
|
settings: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
|
|
|
|
onOpenSettings: PropTypes.func.isRequired,
|
2021-09-12 16:16:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
router: PropTypes.object.isRequired,
|
|
|
|
}
|
|
|
|
|
2021-10-08 17:09:34 -07:00
|
|
|
state = {
|
2021-10-15 20:05:59 -07:00
|
|
|
scrolled: false,
|
2021-10-08 17:09:34 -07:00
|
|
|
}
|
|
|
|
|
2021-09-12 16:16:53 -07:00
|
|
|
handleBackClick = () => {
|
|
|
|
if (window.history && window.history.length === 1) {
|
|
|
|
this.context.router.history.push('/');
|
|
|
|
} else {
|
|
|
|
this.context.router.history.goBack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleBackKeyUp = (e) => {
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
this.handleClick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 17:09:34 -07:00
|
|
|
componentDidMount() {
|
|
|
|
this.attachScrollListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.detachScrollListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
attachScrollListener() {
|
|
|
|
window.addEventListener('scroll', this.handleScroll);
|
|
|
|
}
|
|
|
|
|
|
|
|
detachScrollListener() {
|
|
|
|
window.removeEventListener('scroll', this.handleScroll);
|
|
|
|
}
|
|
|
|
|
2021-10-15 20:05:59 -07:00
|
|
|
handleScroll = throttle(() => {
|
2021-10-08 17:09:34 -07:00
|
|
|
if (this.node) {
|
2021-10-19 13:13:04 -07:00
|
|
|
const { offsetTop } = this.node;
|
2021-10-08 17:09:34 -07:00
|
|
|
|
2021-10-19 13:13:04 -07:00
|
|
|
if (offsetTop > 0) {
|
2021-10-15 20:05:59 -07:00
|
|
|
this.setState({ scrolled: true });
|
2021-10-08 17:09:34 -07:00
|
|
|
} else {
|
2021-10-15 20:05:59 -07:00
|
|
|
this.setState({ scrolled: false });
|
2021-10-08 17:09:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 150, { trailing: true });
|
|
|
|
|
2021-10-15 15:11:16 -07:00
|
|
|
handleOpenSettings = () => {
|
|
|
|
this.props.onOpenSettings();
|
|
|
|
}
|
|
|
|
|
2021-10-08 17:09:34 -07:00
|
|
|
setRef = c => {
|
|
|
|
this.node = c;
|
|
|
|
}
|
|
|
|
|
2021-09-12 16:16:53 -07:00
|
|
|
render() {
|
2021-10-15 20:05:59 -07:00
|
|
|
const { intl, message, settings: Settings } = this.props;
|
|
|
|
const { scrolled } = this.state;
|
2021-09-12 16:16:53 -07:00
|
|
|
|
|
|
|
return (
|
2021-10-15 20:05:59 -07:00
|
|
|
<div className={classNames('sub-navigation', { 'sub-navigation--scrolled': scrolled })} ref={this.setRef}>
|
2021-09-21 11:42:25 -07:00
|
|
|
<div className='sub-navigation__content'>
|
|
|
|
<button
|
|
|
|
className='sub-navigation__back'
|
|
|
|
onClick={this.handleBackClick}
|
|
|
|
onKeyUp={this.handleBackKeyUp}
|
2021-10-15 16:37:32 -07:00
|
|
|
aria-label={intl.formatMessage(messages.back)}
|
|
|
|
title={intl.formatMessage(messages.back)}
|
2021-09-21 11:42:25 -07:00
|
|
|
>
|
2021-10-19 11:37:13 -07:00
|
|
|
<Icon src={require('@tabler/icons/icons/arrow-left.svg')} />
|
2021-10-15 16:37:32 -07:00
|
|
|
{intl.formatMessage(messages.back)}
|
2021-09-21 11:42:25 -07:00
|
|
|
</button>
|
2021-10-14 11:26:07 -07:00
|
|
|
{message && (
|
|
|
|
<div className='sub-navigation__message'>
|
|
|
|
<Helmet><title>{message}</title></Helmet>
|
|
|
|
{message}
|
|
|
|
</div>
|
|
|
|
)}
|
2021-10-15 15:11:16 -07:00
|
|
|
{Settings && (
|
|
|
|
<div className='sub-navigation__cog'>
|
2021-10-15 16:37:32 -07:00
|
|
|
<IconButton
|
|
|
|
src={require('@tabler/icons/icons/settings.svg')}
|
|
|
|
onClick={this.handleOpenSettings}
|
|
|
|
title={intl.formatMessage(messages.settings)}
|
|
|
|
/>
|
2021-10-15 15:11:16 -07:00
|
|
|
</div>
|
|
|
|
)}
|
2021-09-12 16:16:53 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|