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

109 lines
3 KiB
JavaScript
Raw Normal View History

2021-09-12 16:16:53 -07:00
import React from 'react';
import PropTypes from 'prop-types';
2021-09-26 14:33:54 -07:00
import { connect } from 'react-redux';
2021-09-12 16:16:53 -07:00
import { injectIntl, defineMessages, FormattedMessage } from 'react-intl';
import Icon from 'soapbox/components/icon';
2021-09-26 14:33:54 -07:00
import { withRouter, matchPath } from 'react-router-dom';
2021-09-12 16:16:53 -07:00
const routes = [
['status', { path: '/@:username/posts/:statusId' }],
['account', { path: '/@:username' }],
2021-09-26 14:33:54 -07:00
['local_timeline', { path: '/timeline/local' }],
['fediverse_timeline', { path: '/timeline/fediverse' }],
['remote_timeline', { path: '/timeline/:instance' }],
2021-09-12 16:16:53 -07:00
];
2021-09-26 14:33:54 -07:00
const findRoute = path => routes.find(v => matchPath(path, v[1]));
2021-09-12 16:16:53 -07:00
const findRouteType = path => {
2021-09-26 14:33:54 -07:00
const route = findRoute(path) || [];
2021-09-12 16:16:53 -07:00
return route[0];
};
2021-09-26 14:33:54 -07:00
const findMatch = path => {
const route = findRoute(path) || [];
return matchPath(path, route[1]);
};
2021-09-12 16:16:53 -07:00
const messages = defineMessages({
status: { id: 'sub_navigation.status', defaultMessage: 'Post' },
account: { id: 'sub_navigation.account', defaultMessage: 'Profile' },
2021-09-26 14:33:54 -07:00
local_timeline: { id: 'sub_navigation.local_timeline', defaultMessage: '{siteTitle}' },
fediverse_timeline: { id: 'sub_navigation.fediverse_timeline', defaultMessage: 'Fediverse' },
remote_timeline: { id: 'sub_navigation.remote_timeline', defaultMessage: '{instance}' },
2021-09-12 16:16:53 -07:00
});
2021-09-26 14:33:54 -07:00
const mapStateToProps = state => {
return {
siteTitle: state.getIn(['instance', 'title']),
};
};
2021-09-12 16:16:53 -07:00
export default @withRouter
2021-09-26 14:33:54 -07:00
@connect(mapStateToProps)
2021-09-12 16:16:53 -07:00
@injectIntl
class SubNavigation extends React.PureComponent {
static propTypes = {
intl: PropTypes.object.isRequired,
2021-09-26 14:33:54 -07:00
siteTitle: PropTypes.string,
2021-09-12 16:16:53 -07:00
}
static contextTypes = {
router: PropTypes.object.isRequired,
}
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();
}
}
getMessage = () => {
const path = this.context.router.history.location.pathname;
const type = findRouteType(path);
return messages[type] || null;
}
2021-09-26 14:33:54 -07:00
getParams = () => {
const path = this.context.router.history.location.pathname;
return (findMatch(path) || {}).params;
}
2021-09-12 16:16:53 -07:00
render() {
2021-09-26 14:33:54 -07:00
const { intl, siteTitle } = this.props;
2021-09-12 16:16:53 -07:00
const message = this.getMessage();
2021-09-26 14:33:54 -07:00
const params = this.getParams();
2021-09-12 16:16:53 -07:00
if (!message) return null;
return (
<div className='sub-navigation'>
<div className='sub-navigation__content'>
<button
className='sub-navigation__back'
onClick={this.handleBackClick}
onKeyUp={this.handleBackKeyUp}
>
<Icon src={require('@tabler/icons/icons/arrow-back.svg')} />
<FormattedMessage id='sub_navigation.back' defaultMessage='Back' />
</button>
<div className='sub-navigation__message'>
2021-09-26 14:33:54 -07:00
{intl.formatMessage(message, { siteTitle, ...params })}
</div>
2021-09-12 16:16:53 -07:00
</div>
</div>
);
}
}