Add update checking to these pages

This commit is contained in:
Lim Chee Aun 2023-02-11 16:28:03 +08:00
parent ee5ab3f22c
commit ffc8d88f82
3 changed files with 85 additions and 3 deletions

View file

@ -12,6 +12,8 @@ function Hashtags() {
const { masto, instance } = api({ instance: params.instance });
const title = instance ? `#${hashtag} on ${instance}` : `#${hashtag}`;
useTitle(title, `/:instance?/t/:hashtag`);
const latestItem = useRef();
const hashtagsIterator = useRef();
async function fetchHashtags(firstLoad) {
if (firstLoad || !hashtagsIterator.current) {
@ -19,7 +21,32 @@ function Hashtags() {
limit: LIMIT,
});
}
return await hashtagsIterator.current.next();
const results = await hashtagsIterator.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
.listHashtag(hashtag, {
limit: 1,
since_id: latestItem.current,
})
.next();
const { value } = results;
if (value?.length) {
return true;
}
return false;
} catch (e) {
return false;
}
}
return (
@ -39,6 +66,7 @@ function Hashtags() {
emptyText="No one has posted anything with this tag yet."
errorText="Unable to load posts with this tag"
fetchItems={fetchHashtags}
checkForUpdates={checkForUpdates}
/>
);
}

View file

@ -12,6 +12,8 @@ const LIMIT = 20;
function List() {
const { masto } = api();
const { id } = useParams();
const latestItem = useRef();
const listIterator = useRef();
async function fetchList(firstLoad) {
if (firstLoad || !listIterator.current) {
@ -19,7 +21,30 @@ function List() {
limit: LIMIT,
});
}
return await listIterator.current.next();
const results = await listIterator.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.listList(id, {
limit: 1,
since_id: latestItem.current,
});
const { value } = results;
if (value?.length) {
return true;
}
return false;
} catch (e) {
return false;
}
}
const [title, setTitle] = useState(`List`);
@ -42,6 +67,7 @@ function List() {
emptyText="Nothing yet."
errorText="Unable to load posts."
fetchItems={fetchList}
checkForUpdates={checkForUpdates}
boostsCarousel
headerStart={
<Link to="/l" class="button plain">

View file

@ -14,6 +14,7 @@ function Public({ local }) {
const { masto, instance } = api({ instance: params.instance });
const title = `${instance} (${isLocal ? 'local' : 'federated'})`;
useTitle(title, `:instance?/p/l?`);
const latestItem = useRef();
const publicIterator = useRef();
async function fetchPublic(firstLoad) {
@ -23,7 +24,33 @@ function Public({ local }) {
local: isLocal,
});
}
return await publicIterator.current.next();
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 (
@ -41,6 +68,7 @@ function Public({ local }) {
emptyText="No one has posted anything yet."
errorText="Unable to load posts"
fetchItems={fetchPublic}
checkForUpdates={checkForUpdates}
/>
);
}