diff --git a/src/pages/hashtags.jsx b/src/pages/hashtags.jsx index 7021490e..8ae7a5b9 100644 --- a/src/pages/hashtags.jsx +++ b/src/pages/hashtags.jsx @@ -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} /> ); } diff --git a/src/pages/list.jsx b/src/pages/list.jsx index 3b7e2df4..132ad531 100644 --- a/src/pages/list.jsx +++ b/src/pages/list.jsx @@ -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={ diff --git a/src/pages/public.jsx b/src/pages/public.jsx index 1a417d1b..3d37da65 100644 --- a/src/pages/public.jsx +++ b/src/pages/public.jsx @@ -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} /> ); }