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

120 lines
3 KiB
JavaScript
Raw Normal View History

import classNames from 'classnames';
import { throttle } from 'lodash';
2020-03-27 13:59:38 -07:00
import PropTypes from 'prop-types';
import React from 'react';
import { injectIntl } from 'react-intl';
import { connect } from 'react-redux';
import { getSettings } from 'soapbox/actions/settings';
import Icon from 'soapbox/components/icon';
2022-03-21 11:09:01 -07:00
import { Text } from 'soapbox/components/ui';
2020-03-27 13:59:38 -07:00
const mapStateToProps = state => {
const settings = getSettings(state);
return {
autoload: settings.get('autoloadTimelines'),
};
};
export default @connect(mapStateToProps)
@injectIntl
class TimelineQueueButtonHeader extends React.PureComponent {
2020-04-14 11:44:40 -07:00
2020-03-27 13:59:38 -07:00
static propTypes = {
onClick: PropTypes.func.isRequired,
count: PropTypes.number,
message: PropTypes.object.isRequired,
threshold: PropTypes.number,
intl: PropTypes.object.isRequired,
autoload: PropTypes.bool,
autoloadThreshold: PropTypes.number,
2020-03-27 13:59:38 -07:00
};
static defaultProps = {
count: 0,
threshold: 400,
autoload: true,
autoloadThreshold: 50,
2020-03-27 13:59:38 -07:00
};
state = {
scrolled: false,
}
componentDidMount() {
this.attachScrollListener();
}
componentWillUnmount() {
this.detachScrollListener();
}
componentDidUpdate(prevProps, prevState) {
const { scrollTop } = (document.scrollingElement || document.documentElement);
const { count, onClick, autoload, autoloadThreshold } = this.props;
if (autoload && scrollTop <= autoloadThreshold && count !== prevProps.count) {
onClick();
}
}
attachScrollListener() {
window.addEventListener('scroll', this.handleScroll);
}
detachScrollListener() {
window.removeEventListener('scroll', this.handleScroll);
}
handleScroll = throttle(() => {
const { scrollTop } = (document.scrollingElement || document.documentElement);
const { threshold, onClick, autoload, autoloadThreshold } = this.props;
if (autoload && scrollTop <= autoloadThreshold) {
onClick();
}
if (scrollTop > threshold) {
this.setState({ scrolled: true });
} else {
this.setState({ scrolled: false });
}
}, 150, { trailing: true });
2022-03-03 07:13:45 -08:00
scrollUp = () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
2022-03-03 07:13:45 -08:00
};
handleClick = e => {
setTimeout(this.scrollUp, 10);
this.props.onClick(e);
}
render() {
const { count, message, intl } = this.props;
const { scrolled } = this.state;
const visible = count > 0 && scrolled;
2020-03-27 13:59:38 -07:00
2022-03-21 11:09:01 -07:00
const classes = classNames('left-1/2 -translate-x-1/2 fixed top-20 z-50', {
'hidden': !visible,
2020-03-27 13:59:38 -07:00
});
return (
<div className={classes}>
<a className='flex items-center bg-primary-600 hover:bg-primary-700 hover:scale-105 active:scale-100 transition-transform text-white rounded-full px-4 py-2 space-x-1.5 cursor-pointer whitespace-nowrap' onClick={this.handleClick}>
<Icon src={require('@tabler/icons/icons/arrow-bar-to-up.svg')} />
2022-03-21 11:09:01 -07:00
{(count > 0) && (
2022-03-21 11:09:01 -07:00
<Text theme='inherit' size='sm'>
{intl.formatMessage(message, { count })}
2022-03-21 11:09:01 -07:00
</Text>
)}
2020-03-27 13:59:38 -07:00
</a>
</div>
);
}
2020-04-14 11:44:40 -07:00
2020-03-27 13:59:38 -07:00
}