import React, { useEffect, useRef } from 'react'; import { FormattedMessage } from 'react-intl'; import { useHistory } from 'react-router-dom'; import { connectRemoteStream } from 'soapbox/actions/streaming'; import { expandRemoteTimeline } from 'soapbox/actions/timelines'; import IconButton from 'soapbox/components/icon-button'; import { Column, HStack, Text } from 'soapbox/components/ui'; import { useAppSelector, useAppDispatch, useSettings } from 'soapbox/hooks'; import Timeline from '../ui/components/timeline'; import PinnedHostsPicker from './components/pinned-hosts-picker'; interface IRemoteTimeline { params?: { instance?: string } } /** View statuses from a remote instance. */ const RemoteTimeline: React.FC = ({ params }) => { const history = useHistory(); const dispatch = useAppDispatch(); const instance = params?.instance as string; const settings = useSettings(); const stream = useRef(null); const timelineId = 'remote'; const onlyMedia = !!settings.getIn(['remote', 'other', 'onlyMedia']); const next = useAppSelector(state => state.timelines.get('remote')?.next); const pinned: boolean = (settings.getIn(['remote_timeline', 'pinnedHosts']) as any).includes(instance); const disconnect = () => { if (stream.current) { stream.current(); } }; const handleCloseClick: React.MouseEventHandler = () => { history.push('/timeline/fediverse'); }; const handleLoadMore = (maxId: string) => { dispatch(expandRemoteTimeline(instance, { url: next, maxId, onlyMedia })); }; useEffect(() => { disconnect(); dispatch(expandRemoteTimeline(instance, { onlyMedia, maxId: undefined })); stream.current = dispatch(connectRemoteStream(instance, { onlyMedia })); return () => { disconnect(); stream.current = null; }; }, [onlyMedia]); return ( {instance && } {!pinned && ( )} } divideType='space' /> ); }; export default RemoteTimeline;