2023-10-03 10:07:47 +03:00
|
|
|
import { MenuItem } from '@szhsin/react-menu';
|
2023-06-27 14:39:33 +03:00
|
|
|
import { useMemo, useRef, useState } from 'preact/hooks';
|
2023-04-05 20:14:38 +03:00
|
|
|
import { useNavigate, useParams } from 'react-router-dom';
|
|
|
|
import { useSnapshot } from 'valtio';
|
|
|
|
|
|
|
|
import Icon from '../components/icon';
|
2023-06-27 14:39:33 +03:00
|
|
|
import Link from '../components/link';
|
2023-06-13 12:46:37 +03:00
|
|
|
import Menu2 from '../components/menu2';
|
2023-04-05 20:14:38 +03:00
|
|
|
import Timeline from '../components/timeline';
|
|
|
|
import { api } from '../utils/api';
|
|
|
|
import { filteredItems } from '../utils/filters';
|
|
|
|
import states from '../utils/states';
|
|
|
|
import { saveStatus } from '../utils/states';
|
|
|
|
import useTitle from '../utils/useTitle';
|
|
|
|
|
|
|
|
const LIMIT = 20;
|
|
|
|
|
2023-07-05 11:59:28 +03:00
|
|
|
function Trending({ columnMode, ...props }) {
|
2023-04-05 20:14:38 +03:00
|
|
|
const snapStates = useSnapshot(states);
|
2023-07-05 11:59:28 +03:00
|
|
|
const params = columnMode ? {} : useParams();
|
2023-04-05 20:14:38 +03:00
|
|
|
const { masto, instance } = api({
|
|
|
|
instance: props?.instance || params.instance,
|
|
|
|
});
|
|
|
|
const title = `Trending (${instance})`;
|
|
|
|
useTitle(title, `/:instance?/trending`);
|
2023-07-05 11:59:28 +03:00
|
|
|
// const navigate = useNavigate();
|
2023-04-05 20:14:38 +03:00
|
|
|
const latestItem = useRef();
|
|
|
|
|
2023-06-27 14:39:33 +03:00
|
|
|
const [hashtags, setHashtags] = useState([]);
|
2023-04-05 20:14:38 +03:00
|
|
|
const trendIterator = useRef();
|
|
|
|
async function fetchTrend(firstLoad) {
|
|
|
|
if (firstLoad || !trendIterator.current) {
|
|
|
|
trendIterator.current = masto.v1.trends.listStatuses({
|
|
|
|
limit: LIMIT,
|
|
|
|
});
|
2023-06-27 14:39:33 +03:00
|
|
|
|
|
|
|
// Get hashtags
|
|
|
|
try {
|
|
|
|
const iterator = masto.v1.trends.listTags();
|
|
|
|
const { value: tags } = await iterator.next();
|
|
|
|
console.log(tags);
|
|
|
|
setHashtags(tags);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2023-04-05 20:14:38 +03:00
|
|
|
}
|
|
|
|
const results = await trendIterator.current.next();
|
|
|
|
let { value } = results;
|
|
|
|
if (value?.length) {
|
|
|
|
if (firstLoad) {
|
|
|
|
latestItem.current = value[0].id;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = filteredItems(value, 'public'); // Might not work here
|
|
|
|
value.forEach((item) => {
|
|
|
|
saveStatus(item, instance);
|
|
|
|
});
|
|
|
|
}
|
2023-05-14 16:13:36 +03:00
|
|
|
return {
|
|
|
|
...results,
|
|
|
|
value,
|
|
|
|
};
|
2023-04-05 20:14:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async function checkForUpdates() {
|
|
|
|
try {
|
|
|
|
const results = await masto.v1.trends
|
|
|
|
.listStatuses({
|
|
|
|
limit: 1,
|
2023-04-06 05:24:52 +03:00
|
|
|
// NOT SUPPORTED
|
|
|
|
// since_id: latestItem.current,
|
2023-04-05 20:14:38 +03:00
|
|
|
})
|
|
|
|
.next();
|
|
|
|
let { value } = results;
|
|
|
|
value = filteredItems(value, 'public');
|
2023-04-06 05:24:52 +03:00
|
|
|
if (value?.length && value[0].id !== latestItem.current) {
|
|
|
|
latestItem.current = value[0].id;
|
2023-04-05 20:14:38 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-27 14:39:33 +03:00
|
|
|
const TimelineStart = useMemo(() => {
|
|
|
|
if (!hashtags.length) return null;
|
|
|
|
return (
|
|
|
|
<div class="filter-bar">
|
|
|
|
<Icon icon="chart" class="insignificant" size="l" />
|
|
|
|
{hashtags.map((tag, i) => {
|
|
|
|
const { name, history } = tag;
|
|
|
|
const total = history.reduce((acc, cur) => acc + +cur.uses, 0);
|
|
|
|
return (
|
|
|
|
<Link to={`/${instance}/t/${name}`}>
|
|
|
|
<span>
|
|
|
|
<span class="more-insignificant">#</span>
|
|
|
|
{name}
|
|
|
|
</span>
|
|
|
|
<span class="filter-count">{total.toLocaleString()}</span>
|
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}, [hashtags]);
|
|
|
|
|
2023-04-05 20:14:38 +03:00
|
|
|
return (
|
|
|
|
<Timeline
|
|
|
|
key={instance}
|
|
|
|
title={title}
|
|
|
|
titleComponent={
|
|
|
|
<h1 class="header-account">
|
|
|
|
<b>Trending</b>
|
|
|
|
<div>{instance}</div>
|
|
|
|
</h1>
|
|
|
|
}
|
|
|
|
id="trending"
|
|
|
|
instance={instance}
|
|
|
|
emptyText="No trending posts."
|
|
|
|
errorText="Unable to load posts"
|
|
|
|
fetchItems={fetchTrend}
|
|
|
|
checkForUpdates={checkForUpdates}
|
2023-04-06 05:24:52 +03:00
|
|
|
checkForUpdatesInterval={5 * 60 * 1000} // 5 minutes
|
2023-04-05 20:14:38 +03:00
|
|
|
useItemID
|
|
|
|
headerStart={<></>}
|
|
|
|
boostsCarousel={snapStates.settings.boostsCarousel}
|
|
|
|
allowFilters
|
2023-06-27 14:39:33 +03:00
|
|
|
timelineStart={TimelineStart}
|
2023-04-05 20:14:38 +03:00
|
|
|
headerEnd={
|
2023-06-13 12:46:37 +03:00
|
|
|
<Menu2
|
|
|
|
portal
|
2023-04-05 20:14:38 +03:00
|
|
|
// setDownOverflow
|
|
|
|
overflow="auto"
|
|
|
|
viewScroll="close"
|
|
|
|
position="anchor"
|
|
|
|
menuButton={
|
|
|
|
<button type="button" class="plain">
|
|
|
|
<Icon icon="more" size="l" />
|
|
|
|
</button>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<MenuItem
|
|
|
|
onClick={() => {
|
|
|
|
let newInstance = prompt(
|
|
|
|
'Enter a new instance e.g. "mastodon.social"',
|
|
|
|
);
|
|
|
|
if (!/\./.test(newInstance)) {
|
|
|
|
if (newInstance) alert('Invalid instance');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (newInstance) {
|
|
|
|
newInstance = newInstance.toLowerCase().trim();
|
2023-07-05 11:59:28 +03:00
|
|
|
// navigate(`/${newInstance}/trending`);
|
|
|
|
location.hash = `/${newInstance}/trending`;
|
2023-04-05 20:14:38 +03:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon icon="bus" /> <span>Go to another instance…</span>
|
|
|
|
</MenuItem>
|
2023-06-13 12:46:37 +03:00
|
|
|
</Menu2>
|
2023-04-05 20:14:38 +03:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Trending;
|