2017-05-02 20:04:16 -04:00
|
|
|
import React from 'react';
|
2016-11-16 11:20:52 -05:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2017-04-21 14:05:35 -04:00
|
|
|
import PropTypes from 'prop-types';
|
2019-01-31 18:14:05 -05:00
|
|
|
import Icon from 'mastodon/components/icon';
|
2019-08-01 13:17:17 -04:00
|
|
|
import { createPortal } from 'react-dom';
|
2016-10-19 12:20:19 -04:00
|
|
|
|
2017-06-23 13:36:54 -04:00
|
|
|
export default class ColumnBackButton extends React.PureComponent {
|
2016-10-19 12:20:19 -04:00
|
|
|
|
2017-05-12 08:44:10 -04:00
|
|
|
static contextTypes = {
|
2017-05-20 11:31:47 -04:00
|
|
|
router: PropTypes.object,
|
2017-05-12 08:44:10 -04:00
|
|
|
};
|
2016-10-19 12:20:19 -04:00
|
|
|
|
2019-08-01 13:17:17 -04:00
|
|
|
static propTypes = {
|
|
|
|
multiColumn: PropTypes.bool,
|
2023-04-23 16:24:53 -04:00
|
|
|
onClick: PropTypes.func,
|
2019-08-01 13:17:17 -04:00
|
|
|
};
|
|
|
|
|
2017-07-30 18:18:15 -04:00
|
|
|
handleClick = () => {
|
2023-04-23 16:24:53 -04:00
|
|
|
const { router } = this.context;
|
|
|
|
const { onClick } = this.props;
|
|
|
|
|
|
|
|
if (onClick) {
|
|
|
|
onClick();
|
2023-05-04 02:14:04 -04:00
|
|
|
}
|
|
|
|
// Check if there is a previous page in the app to go back to per https://stackoverflow.com/a/70532858/9703201
|
|
|
|
// When upgrading to V6, check `location.key !== 'default'` instead per https://github.com/remix-run/history/blob/main/docs/api-reference.md#location
|
|
|
|
else if (router.location.key) {
|
2023-04-23 16:24:53 -04:00
|
|
|
router.history.goBack();
|
2023-03-04 17:18:19 -05:00
|
|
|
} else {
|
2023-04-23 16:24:53 -04:00
|
|
|
router.history.push('/');
|
2017-07-28 19:58:53 -04:00
|
|
|
}
|
2023-01-29 19:45:35 -05:00
|
|
|
};
|
2016-10-19 12:20:19 -04:00
|
|
|
|
|
|
|
render () {
|
2019-08-01 13:17:17 -04:00
|
|
|
const { multiColumn } = this.props;
|
|
|
|
|
|
|
|
const component = (
|
2017-07-30 18:18:15 -04:00
|
|
|
<button onClick={this.handleClick} className='column-back-button'>
|
2019-01-31 18:14:05 -05:00
|
|
|
<Icon id='chevron-left' className='column-back-button__icon' fixedWidth />
|
2016-11-16 11:20:52 -05:00
|
|
|
<FormattedMessage id='column_back_button.label' defaultMessage='Back' />
|
2017-07-30 18:18:15 -04:00
|
|
|
</button>
|
2016-10-19 12:20:19 -04:00
|
|
|
);
|
2019-08-01 13:17:17 -04:00
|
|
|
|
|
|
|
if (multiColumn) {
|
|
|
|
return component;
|
|
|
|
} else {
|
2019-08-25 09:49:02 -04:00
|
|
|
// The portal container and the component may be rendered to the DOM in
|
|
|
|
// the same React render pass, so the container might not be available at
|
|
|
|
// the time `render()` is called.
|
|
|
|
const container = document.getElementById('tabs-bar__portal');
|
|
|
|
if (container === null) {
|
|
|
|
// The container wasn't available, force a re-render so that the
|
|
|
|
// component can eventually be inserted in the container and not scroll
|
|
|
|
// with the rest of the area.
|
|
|
|
this.forceUpdate();
|
|
|
|
return component;
|
|
|
|
} else {
|
|
|
|
return createPortal(component, container);
|
|
|
|
}
|
2019-08-01 13:17:17 -04:00
|
|
|
}
|
2016-10-19 12:20:19 -04:00
|
|
|
}
|
|
|
|
|
2017-05-12 08:44:10 -04:00
|
|
|
}
|