phanpy/src/pages/list.jsx

89 lines
2 KiB
React
Raw Normal View History

2023-02-10 16:05:18 +00:00
import { useEffect, useRef, useState } from 'preact/hooks';
import { useParams } from 'react-router-dom';
import Icon from '../components/icon';
import Link from '../components/link';
import Timeline from '../components/timeline';
import { api } from '../utils/api';
import { saveStatus } from '../utils/states';
2023-02-10 16:05:18 +00:00
import useTitle from '../utils/useTitle';
const LIMIT = 20;
2023-02-18 12:48:24 +00:00
function List(props) {
2023-02-18 16:05:46 +00:00
const { masto, instance } = api();
2023-02-18 12:48:24 +00:00
const id = props?.id || useParams()?.id;
2023-02-11 08:28:03 +00:00
const latestItem = useRef();
2023-02-10 16:05:18 +00:00
const listIterator = useRef();
async function fetchList(firstLoad) {
if (firstLoad || !listIterator.current) {
listIterator.current = masto.v1.timelines.listList(id, {
limit: LIMIT,
});
}
2023-02-11 08:28:03 +00:00
const results = await listIterator.current.next();
const { value } = results;
if (value?.length) {
if (firstLoad) {
latestItem.current = value[0].id;
}
value.forEach((item) => {
saveStatus(item, instance);
});
2023-02-11 08:28:03 +00:00
}
return results;
}
async function checkForUpdates() {
try {
const results = await masto.v1.timelines.listList(id, {
limit: 1,
since_id: latestItem.current,
});
const { value } = results;
if (value?.length) {
return true;
}
return false;
} catch (e) {
return false;
}
2023-02-10 16:05:18 +00:00
}
const [title, setTitle] = useState(`List`);
useTitle(title, `/l/:id`);
useEffect(() => {
(async () => {
try {
const list = await masto.v1.lists.fetch(id);
setTitle(list.title);
} catch (e) {
console.error(e);
}
})();
}, [id]);
return (
<Timeline
title={title}
id="list"
emptyText="Nothing yet."
errorText="Unable to load posts."
2023-02-18 16:05:46 +00:00
instance={instance}
2023-02-10 16:05:18 +00:00
fetchItems={fetchList}
2023-02-11 08:28:03 +00:00
checkForUpdates={checkForUpdates}
useItemID
2023-02-10 16:05:18 +00:00
boostsCarousel
headerStart={
<Link to="/l" class="button plain">
<Icon icon="list" size="l" />
</Link>
}
/>
);
}
export default List;