phanpy/src/pages/public.jsx

77 lines
1.9 KiB
React
Raw Normal View History

// EXPERIMENTAL: This is a work in progress and may not work as expected.
import { useRef } from 'preact/hooks';
2023-02-06 12:17:07 +00:00
import { useParams } from 'react-router-dom';
import Timeline from '../components/timeline';
import { api } from '../utils/api';
2023-02-03 13:08:08 +00:00
import useTitle from '../utils/useTitle';
const LIMIT = 20;
2023-02-06 12:17:07 +00:00
function Public({ local }) {
const isLocal = !!local;
const params = useParams();
const { masto, instance } = api({ instance: params.instance });
2023-02-03 13:08:08 +00:00
const title = `${instance} (${isLocal ? 'local' : 'federated'})`;
2023-02-11 08:27:40 +00:00
useTitle(title, `:instance?/p/l?`);
2023-02-11 08:28:03 +00:00
const latestItem = useRef();
const publicIterator = useRef();
async function fetchPublic(firstLoad) {
if (firstLoad || !publicIterator.current) {
publicIterator.current = masto.v1.timelines.listPublic({
limit: LIMIT,
local: isLocal,
});
}
2023-02-11 08:28:03 +00:00
const results = await publicIterator.current.next();
const { value } = results;
if (value?.length) {
if (firstLoad) {
latestItem.current = value[0].id;
}
}
return results;
}
async function checkForUpdates() {
try {
const results = await masto.v1.timelines
.listPublic({
limit: 1,
local: isLocal,
since_id: latestItem.current,
})
.next();
const { value } = results;
if (value?.length) {
return true;
}
return false;
} catch (e) {
return false;
}
}
return (
<Timeline
key={instance + isLocal}
2023-02-03 13:08:08 +00:00
title={title}
titleComponent={
<h1 class="header-account">
<b>{instance}</b>
<div>{isLocal ? 'local' : 'federated'}</div>
</h1>
}
id="public"
instance={instance}
emptyText="No one has posted anything yet."
errorText="Unable to load posts"
fetchItems={fetchPublic}
2023-02-11 08:28:03 +00:00
checkForUpdates={checkForUpdates}
/>
);
}
export default Public;