2022-06-01 18:47:28 -07:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import { throttle } from 'lodash';
|
|
|
|
import React, { useState, useEffect, useCallback } from 'react';
|
|
|
|
import { useIntl, MessageDescriptor } from 'react-intl';
|
|
|
|
|
|
|
|
import Icon from 'soapbox/components/icon';
|
|
|
|
import { Text } from 'soapbox/components/ui';
|
2022-06-02 13:29:20 -07:00
|
|
|
import { useAppSelector, useSettings } from 'soapbox/hooks';
|
2022-06-02 18:59:34 -07:00
|
|
|
import { shortNumberFormat } from 'soapbox/utils/numbers';
|
2022-06-01 18:47:28 -07:00
|
|
|
|
|
|
|
interface ITimelineQueueButtonHeader {
|
|
|
|
onClick: () => void,
|
2022-06-02 13:29:20 -07:00
|
|
|
timelineId: string,
|
2022-06-01 18:47:28 -07:00
|
|
|
message: MessageDescriptor,
|
|
|
|
threshold?: number,
|
|
|
|
autoloadThreshold?: number,
|
|
|
|
}
|
|
|
|
|
|
|
|
const TimelineQueueButtonHeader: React.FC<ITimelineQueueButtonHeader> = ({
|
|
|
|
onClick,
|
2022-06-02 13:29:20 -07:00
|
|
|
timelineId,
|
2022-06-01 18:47:28 -07:00
|
|
|
message,
|
|
|
|
threshold = 400,
|
|
|
|
autoloadThreshold = 50,
|
|
|
|
}) => {
|
|
|
|
const intl = useIntl();
|
|
|
|
const settings = useSettings();
|
2022-06-02 13:29:20 -07:00
|
|
|
const count = useAppSelector(state => state.timelines.getIn([timelineId, 'totalQueuedItemsCount']));
|
2022-06-01 18:47:28 -07:00
|
|
|
|
|
|
|
const [scrolled, setScrolled] = useState<boolean>(false);
|
|
|
|
const autoload = settings.get('autoloadTimelines') === true;
|
|
|
|
|
|
|
|
const handleScroll = useCallback(throttle(() => {
|
|
|
|
const { scrollTop } = (document.scrollingElement || document.documentElement);
|
|
|
|
|
|
|
|
if (autoload && scrollTop <= autoloadThreshold) {
|
|
|
|
onClick();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scrollTop > threshold) {
|
|
|
|
setScrolled(true);
|
|
|
|
} else {
|
|
|
|
setScrolled(false);
|
|
|
|
}
|
|
|
|
}, 150, { trailing: true }), []);
|
|
|
|
|
|
|
|
const scrollUp = () => {
|
|
|
|
window.scrollTo({ top: 0, behavior: 'smooth' });
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleClick: React.MouseEventHandler = () => {
|
|
|
|
setTimeout(scrollUp, 10);
|
|
|
|
onClick();
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
window.addEventListener('scroll', handleScroll);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('scroll', handleScroll);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const visible = count > 0 && scrolled;
|
|
|
|
|
|
|
|
const classes = classNames('left-1/2 -translate-x-1/2 fixed top-20 z-50', {
|
|
|
|
'hidden': !visible,
|
|
|
|
});
|
|
|
|
|
|
|
|
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={handleClick}>
|
|
|
|
<Icon src={require('@tabler/icons/icons/arrow-bar-to-up.svg')} />
|
|
|
|
|
|
|
|
{(count > 0) && (
|
|
|
|
<Text theme='inherit' size='sm'>
|
2022-06-02 18:59:34 -07:00
|
|
|
{intl.formatMessage(message, { count: shortNumberFormat(count) })}
|
2022-06-01 18:47:28 -07:00
|
|
|
</Text>
|
|
|
|
)}
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default TimelineQueueButtonHeader;
|