2022-06-04 13:51:59 -07:00
|
|
|
|
import React, { useEffect, useRef } from 'react';
|
|
|
|
|
import { defineMessages, useIntl, FormattedMessage } from 'react-intl';
|
|
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
|
|
|
|
|
|
import { expandHomeTimeline } from 'soapbox/actions/timelines';
|
2022-06-22 08:20:10 -07:00
|
|
|
|
import { Column, Stack, Text } from 'soapbox/components/ui';
|
2022-06-04 13:51:59 -07:00
|
|
|
|
import Timeline from 'soapbox/features/ui/components/timeline';
|
|
|
|
|
import { useAppSelector, useAppDispatch } from 'soapbox/hooks';
|
|
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
|
title: { id: 'column.home', defaultMessage: 'Home' },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const HomeTimeline: React.FC = () => {
|
|
|
|
|
const intl = useIntl();
|
|
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
|
const polling = useRef<NodeJS.Timer | null>(null);
|
|
|
|
|
|
2022-06-23 15:33:23 -07:00
|
|
|
|
const isPartial = useAppSelector(state => state.timelines.get('home')?.isPartial === true);
|
2022-06-22 08:20:10 -07:00
|
|
|
|
const currentAccountId = useAppSelector(state => state.timelines.getIn(['home', 'feedAccountId']));
|
2022-06-04 13:51:59 -07:00
|
|
|
|
const siteTitle = useAppSelector(state => state.instance.title);
|
|
|
|
|
|
|
|
|
|
const handleLoadMore = (maxId: string) => {
|
2022-06-22 08:20:10 -07:00
|
|
|
|
dispatch(expandHomeTimeline({ maxId, accountId: currentAccountId }));
|
2022-06-04 13:51:59 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Mastodon generates the feed in Redis, and can return a partial timeline
|
|
|
|
|
// (HTTP 206) for new users. Poll until we get a full page of results.
|
|
|
|
|
const checkIfReloadNeeded = () => {
|
|
|
|
|
if (isPartial) {
|
|
|
|
|
polling.current = setInterval(() => {
|
|
|
|
|
dispatch(expandHomeTimeline());
|
|
|
|
|
}, 3000);
|
|
|
|
|
} else {
|
|
|
|
|
stopPolling();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const stopPolling = () => {
|
|
|
|
|
if (polling.current) {
|
|
|
|
|
clearInterval(polling.current);
|
|
|
|
|
polling.current = null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleRefresh = () => {
|
2022-06-22 08:20:10 -07:00
|
|
|
|
return dispatch(expandHomeTimeline({ maxId: null, accountId: currentAccountId }));
|
2022-06-04 13:51:59 -07:00
|
|
|
|
};
|
|
|
|
|
|
2022-06-06 11:43:38 -07:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
checkIfReloadNeeded();
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
stopPolling();
|
|
|
|
|
};
|
|
|
|
|
}, [isPartial]);
|
|
|
|
|
|
2022-06-04 13:51:59 -07:00
|
|
|
|
return (
|
|
|
|
|
<Column label={intl.formatMessage(messages.title)} transparent>
|
|
|
|
|
<Timeline
|
|
|
|
|
scrollKey='home_timeline'
|
|
|
|
|
onLoadMore={handleLoadMore}
|
|
|
|
|
onRefresh={handleRefresh}
|
|
|
|
|
timelineId='home'
|
|
|
|
|
divideType='space'
|
2022-06-22 08:20:10 -07:00
|
|
|
|
emptyMessage={
|
|
|
|
|
<Stack space={1}>
|
|
|
|
|
<Text size='xl' weight='medium' align='center'>
|
|
|
|
|
You’re not following anyone yet
|
|
|
|
|
</Text>
|
|
|
|
|
|
|
|
|
|
<Text theme='muted' align='center'>
|
|
|
|
|
{siteTitle} gets more interesting once you follow other users.
|
|
|
|
|
</Text>
|
|
|
|
|
</Stack>
|
|
|
|
|
}
|
2022-06-04 13:51:59 -07:00
|
|
|
|
/>
|
|
|
|
|
</Column>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default HomeTimeline;
|