New "Private" tab in Mentions

This is still very clunky
This commit is contained in:
Lim Chee Aun 2023-05-05 17:54:16 +08:00
parent 123da4af79
commit 9c13224aed
2 changed files with 105 additions and 21 deletions

View file

@ -1900,6 +1900,9 @@ ul.link-list li a .icon {
); );
align-items: center; align-items: center;
} }
.filter-bar.centered {
justify-content: center;
}
@media (min-width: 40em) { @media (min-width: 40em) {
.filter-bar { .filter-bar {
background-color: transparent; background-color: transparent;

View file

@ -1,5 +1,7 @@
import { useRef } from 'preact/hooks'; import { useMemo, useRef } from 'preact/hooks';
import { useSearchParams } from 'react-router-dom';
import Link from '../components/link';
import Timeline from '../components/timeline'; import Timeline from '../components/timeline';
import { api } from '../utils/api'; import { api } from '../utils/api';
import { saveStatus } from '../utils/states'; import { saveStatus } from '../utils/states';
@ -7,9 +9,12 @@ import useTitle from '../utils/useTitle';
const LIMIT = 20; const LIMIT = 20;
function Mentions() { function Mentions(props) {
useTitle('Mentions', '/mentions'); useTitle('Mentions', '/mentions');
const { masto, instance } = api(); const { masto, instance } = api();
const [searchParams] = useSearchParams();
const type = props?.type || searchParams.get('type');
const mentionsIterator = useRef(); const mentionsIterator = useRef();
const latestItem = useRef(); const latestItem = useRef();
@ -34,11 +39,68 @@ function Mentions() {
} }
return { return {
...results, ...results,
value: value.map((item) => item.status), value: value?.map((item) => item.status),
}; };
} }
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);
}
async function checkForUpdates() { 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 { try {
const results = await masto.v1.notifications const results = await masto.v1.notifications
.list({ .list({
@ -48,7 +110,7 @@ function Mentions() {
}) })
.next(); .next();
let { value } = results; let { value } = results;
console.log('checkForUpdates', latestItem.current, value); console.log('checkForUpdates ALL', latestItem.current, value);
if (value?.length) { if (value?.length) {
latestItem.current = value[0].id; latestItem.current = value[0].id;
return true; return true;
@ -58,6 +120,23 @@ function Mentions() {
return false; return false;
} }
} }
}
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]);
return ( return (
<Timeline <Timeline
@ -66,9 +145,11 @@ function Mentions() {
emptyText="No one mentioned you :(" emptyText="No one mentioned you :("
errorText="Unable to load mentions." errorText="Unable to load mentions."
instance={instance} instance={instance}
fetchItems={fetchMentions} fetchItems={fetchItems}
checkForUpdates={checkForUpdates} checkForUpdates={checkForUpdates}
useItemID useItemID
timelineStart={TimelineStart}
refresh={type}
/> />
); );
} }