phanpy/src/pages/account-statuses.jsx

102 lines
2.6 KiB
React
Raw Normal View History

import { useEffect, useRef, useState } from 'preact/hooks';
import { useParams } from 'react-router-dom';
2023-02-03 16:08:08 +03:00
import { useSnapshot } from 'valtio';
import Timeline from '../components/timeline';
import { api } from '../utils/api';
import emojifyText from '../utils/emojify-text';
2023-01-31 14:08:10 +03:00
import states from '../utils/states';
2023-02-03 16:08:08 +03:00
import useTitle from '../utils/useTitle';
const LIMIT = 20;
function AccountStatuses() {
2023-02-03 16:08:08 +03:00
const snapStates = useSnapshot(states);
const { id, ...params } = useParams();
const { masto, instance } = api({ instance: params.instance });
const accountStatusesIterator = useRef();
async function fetchAccountStatuses(firstLoad) {
2023-02-17 05:12:59 +03:00
const results = [];
if (firstLoad) {
const { value: pinnedStatuses } = await masto.v1.accounts
.listStatuses(id, {
pinned: true,
})
.next();
if (pinnedStatuses?.length) {
pinnedStatuses.forEach((status) => {
status._pinned = true;
});
results.push(...pinnedStatuses);
}
}
if (firstLoad || !accountStatusesIterator.current) {
accountStatusesIterator.current = masto.v1.accounts.listStatuses(id, {
limit: LIMIT,
});
}
2023-02-17 05:12:59 +03:00
const { value, done } = await accountStatusesIterator.current.next();
if (value?.length) {
results.push(...value);
}
return {
value: results,
done,
};
}
const [account, setAccount] = useState({});
useTitle(
`${account?.acct ? '@' + account.acct : 'Posts'}`,
2023-02-11 11:27:40 +03:00
'/:instance?/a/:id',
);
useEffect(() => {
(async () => {
try {
const acc = await masto.v1.accounts.fetch(id);
console.log(acc);
setAccount(acc);
} catch (e) {
console.error(e);
}
})();
}, [id]);
const { displayName, acct, emojis } = account;
return (
<Timeline
key={id}
title={`${account?.acct ? '@' + account.acct : 'Posts'}`}
2023-01-31 14:08:10 +03:00
titleComponent={
<h1
class="header-account"
2023-02-17 05:14:44 +03:00
// onClick={() => {
// states.showAccount = {
// account,
// instance,
// };
// }}
2023-01-31 14:08:10 +03:00
>
<b
dangerouslySetInnerHTML={{
__html: emojifyText(displayName, emojis),
}}
/>
2023-01-31 14:08:10 +03:00
<div>
<span>@{acct}</span>
2023-01-31 14:08:10 +03:00
</div>
</h1>
}
id="account_statuses"
instance={instance}
emptyText="Nothing to see here yet."
errorText="Unable to load statuses"
fetchItems={fetchAccountStatuses}
2023-02-03 16:08:08 +03:00
boostsCarousel={snapStates.settings.boostsCarousel}
/>
);
}
export default AccountStatuses;