2022-12-10 12:14:48 +03:00
|
|
|
import { Link } from 'preact-router/match';
|
|
|
|
import {
|
|
|
|
useEffect,
|
|
|
|
useLayoutEffect,
|
|
|
|
useMemo,
|
|
|
|
useRef,
|
|
|
|
useState,
|
|
|
|
} from 'preact/hooks';
|
|
|
|
import { useSnapshot } from 'valtio';
|
|
|
|
|
|
|
|
import Icon from '../components/icon';
|
|
|
|
import Loader from '../components/loader';
|
|
|
|
import Status from '../components/status';
|
2022-12-19 12:38:20 +03:00
|
|
|
import shortenNumber from '../utils/shorten-number';
|
2022-12-10 12:14:48 +03:00
|
|
|
import states from '../utils/states';
|
2022-12-20 20:02:48 +03:00
|
|
|
import store from '../utils/store';
|
2022-12-10 12:14:48 +03:00
|
|
|
import useTitle from '../utils/useTitle';
|
|
|
|
|
2022-12-16 08:27:04 +03:00
|
|
|
function StatusPage({ id }) {
|
2022-12-10 12:14:48 +03:00
|
|
|
const snapStates = useSnapshot(states);
|
2022-12-20 20:02:48 +03:00
|
|
|
const cachedStatuses = store.session.getJSON('statuses-' + id);
|
|
|
|
const [statuses, setStatuses] = useState(cachedStatuses || [{ id }]);
|
2022-12-10 12:14:48 +03:00
|
|
|
const [uiState, setUIState] = useState('default');
|
|
|
|
const heroStatusRef = useRef();
|
|
|
|
|
2022-12-20 20:02:48 +03:00
|
|
|
useEffect(() => {
|
2022-12-18 19:19:19 +03:00
|
|
|
const containsStatus = statuses.find((s) => s.id === id);
|
2022-12-20 10:32:31 +03:00
|
|
|
if (!containsStatus) {
|
2022-12-10 12:14:48 +03:00
|
|
|
setStatuses([{ id }]);
|
2022-12-20 20:02:48 +03:00
|
|
|
} else {
|
|
|
|
const cachedStatuses = store.session.getJSON('statuses-' + id);
|
|
|
|
if (cachedStatuses) {
|
|
|
|
setStatuses(cachedStatuses);
|
|
|
|
}
|
2022-12-10 12:14:48 +03:00
|
|
|
}
|
2022-12-20 20:02:48 +03:00
|
|
|
}, [id]);
|
2022-12-10 12:14:48 +03:00
|
|
|
|
2022-12-20 20:02:48 +03:00
|
|
|
useEffect(async () => {
|
2022-12-10 12:14:48 +03:00
|
|
|
setUIState('loading');
|
|
|
|
|
2022-12-18 15:46:13 +03:00
|
|
|
const hasStatus = snapStates.statuses.has(id);
|
|
|
|
let heroStatus = snapStates.statuses.get(id);
|
|
|
|
try {
|
|
|
|
heroStatus = await masto.statuses.fetch(id);
|
|
|
|
states.statuses.set(id, heroStatus);
|
|
|
|
} catch (e) {
|
|
|
|
// Silent fail if status is cached
|
|
|
|
if (!hasStatus) {
|
2022-12-10 12:14:48 +03:00
|
|
|
setUIState('error');
|
|
|
|
alert('Error fetching status');
|
|
|
|
}
|
2022-12-18 15:46:13 +03:00
|
|
|
return;
|
2022-12-10 12:14:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const context = await masto.statuses.fetchContext(id);
|
|
|
|
const { ancestors, descendants } = context;
|
|
|
|
|
|
|
|
ancestors.forEach((status) => {
|
|
|
|
states.statuses.set(status.id, status);
|
|
|
|
});
|
2022-12-18 15:46:13 +03:00
|
|
|
const nestedDescendants = [];
|
2022-12-10 12:14:48 +03:00
|
|
|
descendants.forEach((status) => {
|
|
|
|
states.statuses.set(status.id, status);
|
2022-12-18 15:46:13 +03:00
|
|
|
if (status.inReplyToAccountId === status.account.id) {
|
|
|
|
// If replying to self, it's part of the thread, level 1
|
|
|
|
nestedDescendants.push(status);
|
|
|
|
} else if (status.inReplyToId === heroStatus.id) {
|
|
|
|
// If replying to the hero status, it's a reply, level 1
|
|
|
|
nestedDescendants.push(status);
|
|
|
|
} else {
|
|
|
|
// If replying to someone else, it's a reply to a reply, level 2
|
|
|
|
const parent = descendants.find((s) => s.id === status.inReplyToId);
|
|
|
|
if (parent) {
|
|
|
|
if (!parent.__replies) {
|
|
|
|
parent.__replies = [];
|
|
|
|
}
|
|
|
|
parent.__replies.push(status);
|
|
|
|
} else {
|
|
|
|
// If no parent, it's probably a reply to a reply to a reply, level 3
|
|
|
|
console.warn('[LEVEL 3] No parent found for', status);
|
|
|
|
}
|
2022-12-10 12:14:48 +03:00
|
|
|
}
|
|
|
|
});
|
2022-12-18 15:46:13 +03:00
|
|
|
|
|
|
|
console.log({ ancestors, descendants, nestedDescendants });
|
2022-12-10 12:14:48 +03:00
|
|
|
|
|
|
|
const allStatuses = [
|
2022-12-18 19:19:19 +03:00
|
|
|
...ancestors.map((s) => ({
|
|
|
|
id: s.id,
|
|
|
|
ancestor: true,
|
|
|
|
accountID: s.account.id,
|
|
|
|
})),
|
|
|
|
{ id, accountID: heroStatus.account.id },
|
2022-12-18 15:46:13 +03:00
|
|
|
...nestedDescendants.map((s) => ({
|
2022-12-10 12:14:48 +03:00
|
|
|
id: s.id,
|
2022-12-18 19:19:19 +03:00
|
|
|
accountID: s.account.id,
|
2022-12-10 12:14:48 +03:00
|
|
|
descendant: true,
|
2022-12-18 15:46:13 +03:00
|
|
|
thread: s.account.id === heroStatus.account.id,
|
|
|
|
replies: s.__replies?.map((r) => r.id),
|
2022-12-10 12:14:48 +03:00
|
|
|
})),
|
|
|
|
];
|
2022-12-18 15:46:13 +03:00
|
|
|
console.log({ allStatuses });
|
2022-12-10 12:14:48 +03:00
|
|
|
setStatuses(allStatuses);
|
2022-12-20 20:02:48 +03:00
|
|
|
store.session.setJSON('statuses-' + id, allStatuses);
|
2022-12-10 12:14:48 +03:00
|
|
|
} catch (e) {
|
2022-12-18 15:46:13 +03:00
|
|
|
console.error(e);
|
2022-12-10 12:14:48 +03:00
|
|
|
setUIState('error');
|
|
|
|
}
|
|
|
|
|
|
|
|
setUIState('default');
|
|
|
|
}, [id, snapStates.reloadStatusPage]);
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (heroStatusRef.current && statuses.length > 1) {
|
|
|
|
heroStatusRef.current.scrollIntoView({
|
|
|
|
behavior: 'smooth',
|
|
|
|
block: 'start',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [id]);
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
2022-12-18 08:43:34 +03:00
|
|
|
const hasAncestor = statuses.some((s) => s.ancestor);
|
|
|
|
if (hasAncestor) {
|
|
|
|
heroStatusRef.current?.scrollIntoView({
|
|
|
|
// behavior: 'smooth',
|
|
|
|
block: 'start',
|
|
|
|
});
|
|
|
|
}
|
2022-12-10 12:14:48 +03:00
|
|
|
}, [statuses]);
|
|
|
|
|
2022-12-18 15:46:13 +03:00
|
|
|
const heroStatus = snapStates.statuses.get(id);
|
2022-12-10 12:14:48 +03:00
|
|
|
const heroDisplayName = useMemo(() => {
|
|
|
|
// Remove shortcodes from display name
|
|
|
|
if (!heroStatus) return '';
|
|
|
|
const { account } = heroStatus;
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.innerHTML = account.displayName;
|
|
|
|
return div.innerText.trim();
|
|
|
|
}, [heroStatus]);
|
|
|
|
const heroContentText = useMemo(() => {
|
|
|
|
if (!heroStatus) return '';
|
2022-12-15 12:14:33 +03:00
|
|
|
const { spoilerText, content } = heroStatus;
|
|
|
|
let text;
|
|
|
|
if (spoilerText) {
|
|
|
|
text = spoilerText;
|
|
|
|
} else {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.innerHTML = content;
|
|
|
|
text = div.innerText.trim();
|
|
|
|
}
|
2022-12-10 12:14:48 +03:00
|
|
|
if (text.length > 64) {
|
2022-12-15 12:14:33 +03:00
|
|
|
// "The title should ideally be less than 64 characters in length"
|
|
|
|
// https://www.w3.org/Provider/Style/TITLE.html
|
2022-12-10 12:14:48 +03:00
|
|
|
text = text.slice(0, 64) + '…';
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}, [heroStatus]);
|
|
|
|
useTitle(
|
|
|
|
heroDisplayName && heroContentText
|
2022-12-15 12:14:33 +03:00
|
|
|
? `${heroDisplayName}: "${heroContentText}"`
|
2022-12-10 12:14:48 +03:00
|
|
|
: 'Status',
|
|
|
|
);
|
|
|
|
|
|
|
|
const prevRoute = states.history.findLast((h) => {
|
|
|
|
return h === '/' || /notifications/i.test(h);
|
|
|
|
});
|
|
|
|
const closeLink = `#${prevRoute || '/'}`;
|
|
|
|
|
2022-12-18 15:46:13 +03:00
|
|
|
const [limit, setLimit] = useState(40);
|
|
|
|
const showMore = useMemo(() => {
|
|
|
|
// return number of statuses to show
|
|
|
|
return statuses.length - limit;
|
|
|
|
}, [statuses.length, limit]);
|
|
|
|
|
2022-12-19 05:05:27 +03:00
|
|
|
const hasManyStatuses = statuses.length > 40;
|
|
|
|
|
2022-12-10 12:14:48 +03:00
|
|
|
return (
|
|
|
|
<div class="deck-backdrop">
|
|
|
|
<Link href={closeLink}></Link>
|
|
|
|
<div
|
|
|
|
class={`status-deck deck contained ${
|
|
|
|
statuses.length > 1 ? 'padded-bottom' : ''
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
<header>
|
2022-12-19 11:25:57 +03:00
|
|
|
{/* <div>
|
2022-12-18 15:53:32 +03:00
|
|
|
<Link class="button plain deck-close" href={closeLink}>
|
|
|
|
<Icon icon="chevron-left" size="xl" />
|
|
|
|
</Link>
|
2022-12-19 11:25:57 +03:00
|
|
|
</div> */}
|
2022-12-10 12:14:48 +03:00
|
|
|
<h1>Status</h1>
|
|
|
|
<div class="header-side">
|
|
|
|
<Loader hidden={uiState !== 'loading'} />
|
2022-12-19 11:25:57 +03:00
|
|
|
<Link class="button plain deck-close" href={closeLink}>
|
|
|
|
<Icon icon="x" size="xl" />
|
|
|
|
</Link>
|
2022-12-10 12:14:48 +03:00
|
|
|
</div>
|
|
|
|
</header>
|
2022-12-20 10:32:31 +03:00
|
|
|
<ul
|
|
|
|
class={`timeline flat contextual ${
|
|
|
|
uiState === 'loading' ? 'loading' : ''
|
|
|
|
}`}
|
|
|
|
>
|
2022-12-18 15:46:13 +03:00
|
|
|
{statuses.slice(0, limit).map((status) => {
|
|
|
|
const {
|
|
|
|
id: statusID,
|
|
|
|
ancestor,
|
|
|
|
descendant,
|
|
|
|
thread,
|
|
|
|
replies,
|
|
|
|
} = status;
|
2022-12-10 12:14:48 +03:00
|
|
|
const isHero = statusID === id;
|
|
|
|
return (
|
|
|
|
<li
|
|
|
|
key={statusID}
|
|
|
|
ref={isHero ? heroStatusRef : null}
|
2022-12-17 20:14:44 +03:00
|
|
|
class={`${ancestor ? 'ancestor' : ''} ${
|
|
|
|
descendant ? 'descendant' : ''
|
2022-12-20 10:32:31 +03:00
|
|
|
} ${thread ? 'thread' : ''} ${isHero ? 'hero' : ''}`}
|
2022-12-10 12:14:48 +03:00
|
|
|
>
|
|
|
|
{isHero ? (
|
|
|
|
<Status statusID={statusID} withinContext size="l" />
|
|
|
|
) : (
|
|
|
|
<Link
|
|
|
|
class="
|
|
|
|
status-link
|
|
|
|
"
|
|
|
|
href={`#/s/${statusID}`}
|
|
|
|
>
|
2022-12-18 15:46:13 +03:00
|
|
|
<Status
|
|
|
|
statusID={statusID}
|
|
|
|
withinContext
|
|
|
|
size={thread || ancestor ? 'm' : 's'}
|
|
|
|
/>
|
2022-12-10 12:14:48 +03:00
|
|
|
</Link>
|
|
|
|
)}
|
2022-12-18 15:46:13 +03:00
|
|
|
{descendant && replies?.length > 0 && (
|
2022-12-19 05:05:27 +03:00
|
|
|
<details class="replies" open={!hasManyStatuses}>
|
|
|
|
<summary hidden={!hasManyStatuses}>
|
2022-12-19 12:38:20 +03:00
|
|
|
<span title={replies.length}>
|
|
|
|
{shortenNumber(replies.length)}
|
|
|
|
</span>{' '}
|
|
|
|
repl{replies.length === 1 ? 'y' : 'ies'}
|
2022-12-18 15:46:13 +03:00
|
|
|
</summary>
|
|
|
|
<ul>
|
|
|
|
{replies.map((replyID) => (
|
|
|
|
<li key={replyID}>
|
|
|
|
<Link class="status-link" href={`#/s/${replyID}`}>
|
|
|
|
<Status statusID={replyID} withinContext size="s" />
|
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</details>
|
|
|
|
)}
|
2022-12-10 12:14:48 +03:00
|
|
|
{uiState === 'loading' &&
|
|
|
|
isHero &&
|
2022-12-10 16:19:38 +03:00
|
|
|
!!heroStatus?.repliesCount &&
|
|
|
|
statuses.length === 1 && (
|
2022-12-10 12:14:48 +03:00
|
|
|
<div class="status-loading">
|
2022-12-10 16:19:38 +03:00
|
|
|
<Loader />
|
2022-12-10 12:14:48 +03:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
})}
|
2022-12-19 05:04:50 +03:00
|
|
|
{showMore > 0 && (
|
|
|
|
<li>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="plain block"
|
|
|
|
disabled={uiState === 'loading'}
|
|
|
|
onClick={() => setLimit((l) => l + 40)}
|
|
|
|
style={{ marginBlockEnd: '6em' }}
|
|
|
|
>
|
|
|
|
Show more…{' '}
|
|
|
|
<span class="tag">{showMore > 40 ? '40+' : showMore}</span>
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
)}
|
2022-12-10 12:14:48 +03:00
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2022-12-16 08:27:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default StatusPage;
|