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'; import { useAppSelector, useSettings } from 'soapbox/hooks'; interface ITimelineQueueButtonHeader { onClick: () => void, timelineId: string, message: MessageDescriptor, threshold?: number, autoloadThreshold?: number, } const TimelineQueueButtonHeader: React.FC = ({ onClick, timelineId, message, threshold = 400, autoloadThreshold = 50, }) => { const intl = useIntl(); const settings = useSettings(); const count = useAppSelector(state => state.timelines.getIn([timelineId, 'totalQueuedItemsCount'])); const [scrolled, setScrolled] = useState(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 (
{(count > 0) && ( {intl.formatMessage(message, { count })} )}
); }; export default TimelineQueueButtonHeader;