phanpy/src/pages/following.jsx

52 lines
1.3 KiB
React
Raw Normal View History

2023-02-03 13:08:08 +00:00
import { useRef } from 'preact/hooks';
import { useSnapshot } from 'valtio';
import Timeline from '../components/timeline';
import { api } from '../utils/api';
2023-02-05 17:10:49 +00:00
import states from '../utils/states';
import { saveStatus } from '../utils/states';
2023-02-03 13:08:08 +00:00
import useTitle from '../utils/useTitle';
const LIMIT = 20;
function Following() {
useTitle('Following', '/l/f');
const { masto, instance } = api();
2023-02-03 13:08:08 +00:00
const snapStates = useSnapshot(states);
const homeIterator = useRef();
async function fetchHome(firstLoad) {
if (firstLoad || !homeIterator.current) {
homeIterator.current = masto.v1.timelines.listHome({ limit: LIMIT });
}
const results = await homeIterator.current.next();
const { value } = results;
if (value?.length) {
value.forEach((item) => {
saveStatus(item, instance);
});
// ENFORCE sort by datetime (Latest first)
value.sort((a, b) => {
const aDate = new Date(a.createdAt);
const bDate = new Date(b.createdAt);
return bDate - aDate;
});
}
return results;
2023-02-03 13:08:08 +00:00
}
return (
<Timeline
title="Following"
id="following"
emptyText="Nothing to see here."
errorText="Unable to load posts."
fetchItems={fetchHome}
useItemID
2023-02-03 13:08:08 +00:00
boostsCarousel={snapStates.settings.boostsCarousel}
/>
);
}
export default Following;