2023-05-05 12:54:16 +03:00
|
|
|
import { useMemo, useRef } from 'preact/hooks';
|
|
|
|
import { useSearchParams } from 'react-router-dom';
|
2023-04-06 14:32:26 +03:00
|
|
|
|
2023-05-05 12:54:16 +03:00
|
|
|
import Link from '../components/link';
|
2023-04-06 14:32:26 +03:00
|
|
|
import Timeline from '../components/timeline';
|
|
|
|
import { api } from '../utils/api';
|
|
|
|
import { saveStatus } from '../utils/states';
|
|
|
|
import useTitle from '../utils/useTitle';
|
|
|
|
|
|
|
|
const LIMIT = 20;
|
|
|
|
|
2023-05-05 12:54:16 +03:00
|
|
|
function Mentions(props) {
|
2023-04-06 14:32:26 +03:00
|
|
|
useTitle('Mentions', '/mentions');
|
|
|
|
const { masto, instance } = api();
|
2023-05-05 12:54:16 +03:00
|
|
|
const [searchParams] = useSearchParams();
|
|
|
|
const type = props?.type || searchParams.get('type');
|
|
|
|
|
2023-04-06 14:32:26 +03: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,
|
2023-05-05 12:54:16 +03:00
|
|
|
value: value?.map((item) => item.status),
|
2023-04-06 14:32:26 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-05-05 12:54:16 +03: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;
|
2023-05-29 15:06:10 +03:00
|
|
|
value = value?.filter((item) => item.lastStatus);
|
2023-05-05 12:54:16 +03:00
|
|
|
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 14:32:26 +03:00
|
|
|
async function checkForUpdates() {
|
2023-05-05 12:54:16 +03:00
|
|
|
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 14:32:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 12:54:16 +03: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 14:32:26 +03:00
|
|
|
return (
|
|
|
|
<Timeline
|
|
|
|
title="Mentions"
|
|
|
|
id="mentions"
|
|
|
|
emptyText="No one mentioned you :("
|
|
|
|
errorText="Unable to load mentions."
|
|
|
|
instance={instance}
|
2023-05-05 12:54:16 +03:00
|
|
|
fetchItems={fetchItems}
|
2023-04-06 14:32:26 +03:00
|
|
|
checkForUpdates={checkForUpdates}
|
|
|
|
useItemID
|
2023-05-05 12:54:16 +03:00
|
|
|
timelineStart={TimelineStart}
|
|
|
|
refresh={type}
|
2023-04-06 14:32:26 +03:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Mentions;
|