phanpy/src/pages/mentions.jsx

158 lines
4 KiB
React
Raw Normal View History

import { useMemo, useRef } from 'preact/hooks';
import { useSearchParams } from 'react-router-dom';
2023-04-06 11:32:26 +00:00
import Link from '../components/link';
2023-04-06 11:32:26 +00:00
import Timeline from '../components/timeline';
import { api } from '../utils/api';
import { saveStatus } from '../utils/states';
import useTitle from '../utils/useTitle';
const LIMIT = 20;
function Mentions(props) {
2023-04-06 11:32:26 +00:00
useTitle('Mentions', '/mentions');
const { masto, instance } = api();
const [searchParams] = useSearchParams();
const type = props?.type || searchParams.get('type');
2023-04-06 11:32:26 +00:00
const mentionsIterator = useRef();
const latestItem = useRef();
async function fetchMentions(firstLoad) {
if (firstLoad || !mentionsIterator.current) {
mentionsIterator.current = masto.v1.notifications.list({
limit: LIMIT,
types: ['mention'],
});
}
const results = await mentionsIterator.current.next();
let { value } = results;
if (value?.length) {
if (firstLoad) {
latestItem.current = value[0].id;
console.log('First load', latestItem.current);
}
value.forEach(({ status: item }) => {
saveStatus(item, instance);
});
}
return {
...results,
value: value?.map((item) => item.status),
2023-04-06 11:32:26 +00:00
};
}
const conversationsIterator = useRef();
const latestConversationItem = useRef();
async function fetchConversations(firstLoad) {
if (firstLoad || !conversationsIterator.current) {
conversationsIterator.current = masto.v1.conversations.list({
limit: LIMIT,
});
}
const results = await conversationsIterator.current.next();
let { value } = results;
if (value?.length) {
if (firstLoad) {
latestConversationItem.current = value[0].lastStatus.id;
console.log('First load', latestConversationItem.current);
}
value.forEach(({ lastStatus: item }) => {
saveStatus(item, instance);
});
}
console.log('results', results);
return {
...results,
value: value?.map((item) => item.lastStatus),
};
}
function fetchItems(...args) {
if (type === 'private') {
return fetchConversations(...args);
}
return fetchMentions(...args);
}
2023-04-06 11:32:26 +00:00
async function checkForUpdates() {
if (type === 'private') {
try {
const results = await masto.v1.conversations
.list({
limit: 1,
since_id: latestConversationItem.current,
})
.next();
let { value } = results;
console.log(
'checkForUpdates PRIVATE',
latestConversationItem.current,
value,
);
if (value?.length) {
latestConversationItem.current = value[0].lastStatus.id;
return true;
}
return false;
} catch (e) {
return false;
}
} else {
try {
const results = await masto.v1.notifications
.list({
limit: 1,
types: ['mention'],
since_id: latestItem.current,
})
.next();
let { value } = results;
console.log('checkForUpdates ALL', latestItem.current, value);
if (value?.length) {
latestItem.current = value[0].id;
return true;
}
return false;
} catch (e) {
return false;
2023-04-06 11:32:26 +00:00
}
}
}
const TimelineStart = useMemo(() => {
return (
<div class="filter-bar centered">
<Link to="/mentions" class={!type ? 'is-active' : ''}>
All
</Link>
<Link
to="/mentions?type=private"
class={type === 'private' ? 'is-active' : ''}
>
Private
</Link>
</div>
);
}, [type]);
2023-04-06 11:32:26 +00:00
return (
<Timeline
title="Mentions"
id="mentions"
emptyText="No one mentioned you :("
errorText="Unable to load mentions."
instance={instance}
fetchItems={fetchItems}
2023-04-06 11:32:26 +00:00
checkForUpdates={checkForUpdates}
useItemID
timelineStart={TimelineStart}
refresh={type}
2023-04-06 11:32:26 +00:00
/>
);
}
export default Mentions;