phanpy/src/pages/hashtag.jsx

76 lines
1.9 KiB
React
Raw Normal View History

import { useRef } from 'preact/hooks';
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-18 12:48:24 +00:00
function Hashtags(props) {
let { hashtag, ...params } = useParams();
2023-02-18 12:48:24 +00:00
if (props.hashtag) hashtag = props.hashtag;
const { masto, instance } = api({ instance: params.instance });
const title = instance ? `#${hashtag} on ${instance}` : `#${hashtag}`;
2023-02-11 08:27:40 +00:00
useTitle(title, `/:instance?/t/:hashtag`);
2023-02-11 08:28:03 +00:00
const latestItem = useRef();
const hashtagsIterator = useRef();
async function fetchHashtags(firstLoad) {
if (firstLoad || !hashtagsIterator.current) {
hashtagsIterator.current = masto.v1.timelines.listHashtag(hashtag, {
limit: LIMIT,
});
}
2023-02-11 08:28:03 +00:00
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 (
<Timeline
key={hashtag}
title={title}
titleComponent={
!!instance && (
<h1 class="header-account">
<b>#{hashtag}</b>
<div>{instance}</div>
</h1>
)
}
id="hashtags"
instance={instance}
emptyText="No one has posted anything with this tag yet."
errorText="Unable to load posts with this tag"
fetchItems={fetchHashtags}
2023-02-11 08:28:03 +00:00
checkForUpdates={checkForUpdates}
/>
);
}
export default Hashtags;