mirror of
https://github.com/thegeneralist01/archivr
synced 2026-12-04 18:56:44 +01:00
fix: keep related thread posts accessible offline
This commit is contained in:
parent
13cae5174f
commit
0b591b2e84
5 changed files with 117 additions and 32 deletions
|
|
@ -145,7 +145,7 @@ A separate auth database (`archivr-auth.sqlite`, path set in TOML) holds users,
|
|||
| Snapchat | Direct URL · `snapchat:ID` |
|
||||
| Arbitrary URL / web page | Any `https://` URL |
|
||||
|
||||
Thread previews follow archived reply relationships, group each reply branch under its parent post, and show captured quoted or linked posts inside the post that references them. Long or media-heavy quoted and linked posts start as compact previews; **Show full post** reveals their complete archived content. Links to posts already in the conversation jump to their place in the thread; press Esc or click/tap beside the thread to clear the focused post. Posts with missing relationship data remain available under “Other archived posts”.
|
||||
Thread previews follow archived reply relationships, group each reply branch under its parent post, and show captured quoted, retweeted, or linked posts inside the post that references them. Long or media-heavy embedded posts start as compact previews; **Show full post** reveals their complete archived content. Deep reference chains link to their archived posts under “Other archived posts”. Links to posts already in the conversation jump to their place in the thread; press Esc or click/tap beside the thread to clear the focused post. Posts with missing relationship data remain available under “Other archived posts”.
|
||||
|
||||
X Articles are titled `<article title> — @handle` from the article's own title instead of the tweet text (usually a
|
||||
bare `t.co` link). Article entries archived before this change are retitled on the next `archivr-server` start, but only
|
||||
|
|
|
|||
|
|
@ -3,12 +3,22 @@ import PreviewPanel from './PreviewPanel'
|
|||
|
||||
export default function PreviewModal({ archiveId, entry, detail, onClose }) {
|
||||
const entityKind = detail?.summary?.entity_kind
|
||||
// Close on Escape key
|
||||
// A focused post uses Escape to return to the conversation first.
|
||||
useEffect(() => {
|
||||
const handler = (e) => { if (e.key === 'Escape') onClose() }
|
||||
const handler = (e) => {
|
||||
if (e.key !== 'Escape') return
|
||||
if (entityKind === 'tweet_thread' && /^#tweet-\d+$/.test(window.location.hash)
|
||||
&& !document.querySelector('.tw-media-lightbox')) {
|
||||
e.preventDefault()
|
||||
window.history.replaceState(window.history.state, '', window.location.pathname + window.location.search)
|
||||
window.dispatchEvent(new Event('hashchange'))
|
||||
return
|
||||
}
|
||||
onClose()
|
||||
}
|
||||
window.addEventListener('keydown', handler)
|
||||
return () => window.removeEventListener('keydown', handler)
|
||||
}, [onClose])
|
||||
}, [entityKind, onClose])
|
||||
|
||||
return (
|
||||
<div className="preview-modal-backdrop" onClick={onClose}>
|
||||
|
|
|
|||
|
|
@ -1099,7 +1099,7 @@ function MediaLightbox({ items, startIndex, onClose }) {
|
|||
);
|
||||
}
|
||||
|
||||
function TweetCard({ tweet, isInThread, artifactMap, byId, mainIds, embedded = false, trail = [] }) {
|
||||
function TweetCard({ tweet, isInThread, artifactMap, byId, mainIds, extraIds, embedded = false, trail = [] }) {
|
||||
const [lightboxIdx, setLightboxIdx] = useState(null);
|
||||
const [expanded, setExpanded] = useState(false);
|
||||
|
||||
|
|
@ -1115,6 +1115,8 @@ function TweetCard({ tweet, isInThread, artifactMap, byId, mainIds, embedded = f
|
|||
const tweetId = String(tweet.id || '');
|
||||
const quoteId = String(tweet.quoted_status_id || '');
|
||||
const quoted = byId?.get(quoteId);
|
||||
const retweetId = String(tweet.retweeted_status?.id || '');
|
||||
const retweeted = byId?.get(retweetId);
|
||||
const replyTo = byId?.get(String(tweet.in_reply_to_status_id || ''));
|
||||
const canEmbed = trail.length < 4 && !trail.includes(tweetId);
|
||||
const sourceUrl = tweet.id
|
||||
|
|
@ -1125,7 +1127,7 @@ function TweetCard({ tweet, isInThread, artifactMap, byId, mainIds, embedded = f
|
|||
// part of the reply timeline. Posts on the timeline remain in-place links.
|
||||
const linked = new Map();
|
||||
for (const id of linkedStatusIds(tweet)) {
|
||||
if (id && id !== quoteId && id !== tweetId && byId?.has(id) && !mainIds?.has(id)) {
|
||||
if (id && id !== quoteId && id !== retweetId && id !== tweetId && byId?.has(id) && !mainIds?.has(id)) {
|
||||
linked.set(id, byId.get(id));
|
||||
}
|
||||
}
|
||||
|
|
@ -1152,7 +1154,7 @@ function TweetCard({ tweet, isInThread, artifactMap, byId, mainIds, embedded = f
|
|||
if (src) videoItems.push({ kind: m.type === 'animated_gif' ? 'gif' : 'video', src });
|
||||
}
|
||||
}
|
||||
const canCollapse = embedded && (ft.length > 220 || photos.length > 0 || videoItems.length > 0 || !!quoteId || linked.size > 0);
|
||||
const canCollapse = embedded && (ft.length > 220 || photos.length > 0 || videoItems.length > 0 || !!quoteId || !!retweetId || linked.size > 0);
|
||||
const collapsed = canCollapse && !expanded;
|
||||
const mediaSummary = [
|
||||
photos.length && `${photos.length} ${photos.length === 1 ? 'photo' : 'photos'}`,
|
||||
|
|
@ -1245,23 +1247,45 @@ function TweetCard({ tweet, isInThread, artifactMap, byId, mainIds, embedded = f
|
|||
{!collapsed && quoteId && (
|
||||
<div className="tw-related-post">
|
||||
<div className="tw-related-post__label">Quoted post</div>
|
||||
{quoted && mainIds?.has(quoteId)
|
||||
{quoted && (mainIds?.has(quoteId) || extraIds?.has(quoteId))
|
||||
? <a className="tw-related-post__jump" href={`#tweet-${quoteId}`}>
|
||||
@{quoted.author?.screen_name || 'unknown'} · View in this conversation
|
||||
@{quoted.author?.screen_name || 'unknown'} · View archived post
|
||||
</a>
|
||||
: quoted && canEmbed
|
||||
? <TweetCard tweet={quoted} artifactMap={artifactMap} byId={byId} mainIds={mainIds}
|
||||
? <TweetCard tweet={quoted} artifactMap={artifactMap} byId={byId} mainIds={mainIds} extraIds={extraIds}
|
||||
embedded trail={[...trail, tweetId]} />
|
||||
: <a className="tw-related-post__jump" href={`https://x.com/i/status/${quoteId}`}
|
||||
target="_blank" rel="noopener noreferrer">View quoted post on X</a>}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!collapsed && canEmbed && [...linked].map(([id, post]) => (
|
||||
{!collapsed && retweetId && (
|
||||
<div className="tw-related-post">
|
||||
<div className="tw-related-post__label">Retweeted post</div>
|
||||
{retweeted && (mainIds?.has(retweetId) || extraIds?.has(retweetId))
|
||||
? <a className="tw-related-post__jump" href={`#tweet-${retweetId}`}>
|
||||
@{retweeted.author?.screen_name || 'unknown'} · View archived post
|
||||
</a>
|
||||
: retweeted && canEmbed
|
||||
? <TweetCard tweet={retweeted} artifactMap={artifactMap} byId={byId} mainIds={mainIds} extraIds={extraIds}
|
||||
embedded trail={[...trail, tweetId]} />
|
||||
: <a className="tw-related-post__jump" href={`https://x.com/i/status/${retweetId}`}
|
||||
target="_blank" rel="noopener noreferrer">View retweeted post on X</a>}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!collapsed && [...linked].map(([id, post]) => (
|
||||
<div className="tw-related-post" key={id}>
|
||||
<div className="tw-related-post__label">Linked post</div>
|
||||
<TweetCard tweet={post} artifactMap={artifactMap} byId={byId} mainIds={mainIds}
|
||||
embedded trail={[...trail, tweetId]} />
|
||||
{extraIds?.has(id)
|
||||
? <a className="tw-related-post__jump" href={`#tweet-${id}`}>
|
||||
@{post.author?.screen_name || 'unknown'} · View archived post
|
||||
</a>
|
||||
: canEmbed
|
||||
? <TweetCard tweet={post} artifactMap={artifactMap} byId={byId} mainIds={mainIds} extraIds={extraIds}
|
||||
embedded trail={[...trail, tweetId]} />
|
||||
: <a className="tw-related-post__jump" href={`https://x.com/i/status/${id}`}
|
||||
target="_blank" rel="noopener noreferrer">View linked post on X</a>}
|
||||
</div>
|
||||
))}
|
||||
|
||||
|
|
@ -1289,16 +1313,16 @@ function TweetCard({ tweet, isInThread, artifactMap, byId, mainIds, embedded = f
|
|||
);
|
||||
}
|
||||
|
||||
function ThreadNode({ node, artifactMap, byId, mainIds, focusedTweetId }) {
|
||||
function ThreadNode({ node, artifactMap, byId, mainIds, extraIds, focusedTweetId }) {
|
||||
const { tweet, children } = node;
|
||||
return (
|
||||
<>
|
||||
<article id={`tweet-${tweet.id}`}
|
||||
className={`tw-conversation__post${focusedTweetId === String(tweet.id) ? ' tw-conversation__post--focused' : ''}`}>
|
||||
<TweetCard tweet={tweet} isInThread artifactMap={artifactMap} byId={byId} mainIds={mainIds} />
|
||||
<TweetCard tweet={tweet} isInThread artifactMap={artifactMap} byId={byId} mainIds={mainIds} extraIds={extraIds} />
|
||||
</article>
|
||||
{children.length === 1 && (
|
||||
<ThreadNode node={children[0]} artifactMap={artifactMap} byId={byId} mainIds={mainIds}
|
||||
<ThreadNode node={children[0]} artifactMap={artifactMap} byId={byId} mainIds={mainIds} extraIds={extraIds}
|
||||
focusedTweetId={focusedTweetId} />
|
||||
)}
|
||||
{children.length > 1 && (
|
||||
|
|
@ -1311,7 +1335,7 @@ function ThreadNode({ node, artifactMap, byId, mainIds, focusedTweetId }) {
|
|||
<strong>Branch {index + 1} of {children.length}</strong>
|
||||
{child.tweet.author?.screen_name && <span>@{child.tweet.author.screen_name}</span>}
|
||||
</div>
|
||||
<ThreadNode node={child} artifactMap={artifactMap} byId={byId} mainIds={mainIds}
|
||||
<ThreadNode node={child} artifactMap={artifactMap} byId={byId} mainIds={mainIds} extraIds={extraIds}
|
||||
focusedTweetId={focusedTweetId} />
|
||||
</section>
|
||||
))}
|
||||
|
|
@ -1341,6 +1365,7 @@ export default function TweetPreview({ archiveId, entryUid, artifacts, entityKin
|
|||
if (event.key !== 'Escape' || !focusedTweetIdFromHash()) return;
|
||||
if (document.querySelector('.tw-media-lightbox')) return;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
clearFocus();
|
||||
};
|
||||
const onClick = event => {
|
||||
|
|
@ -1459,18 +1484,31 @@ export default function TweetPreview({ archiveId, entryUid, artifacts, entityKin
|
|||
|
||||
if (entityKind === 'tweet_thread') {
|
||||
const requestedId = statusIdFromUrl(originalUrl);
|
||||
const { tree, rows, byId, mainIds, extra } = buildTweetThread(tweets, requestedId);
|
||||
const { tree, rows, byId, mainIds, extra, extraIds } = buildTweetThread(tweets, requestedId);
|
||||
const jumpToArchivedPost = event => {
|
||||
const link = event.target instanceof Element ? event.target.closest('a[href^="#tweet-"]') : null;
|
||||
if (!link || !event.currentTarget.contains(link)) return;
|
||||
const id = link.getAttribute('href')?.match(/^#tweet-(\d+)$/)?.[1];
|
||||
if (!id) return;
|
||||
const post = document.getElementById(`tweet-${id}`);
|
||||
if (!post) return;
|
||||
event.preventDefault();
|
||||
window.history.replaceState(window.history.state, '', window.location.pathname + window.location.search + `#tweet-${id}`);
|
||||
setFocusedTweetId(id);
|
||||
post.scrollIntoView({ block: 'start' });
|
||||
};
|
||||
return (
|
||||
<div className="tw-conversation">
|
||||
<div className="tw-conversation" onClickCapture={jumpToArchivedPost}>
|
||||
<div className="tw-conversation__intro">Conversation · {rows.length} {rows.length === 1 ? 'post' : 'posts'}</div>
|
||||
{tree && <ThreadNode node={tree} artifactMap={artifactMap} byId={byId} mainIds={mainIds}
|
||||
{tree && <ThreadNode node={tree} artifactMap={artifactMap} byId={byId} mainIds={mainIds} extraIds={extraIds}
|
||||
focusedTweetId={focusedTweetId} />}
|
||||
{extra.length > 0 && (
|
||||
<section className="tw-conversation__extra" aria-label="Other archived posts">
|
||||
<div className="tw-conversation__intro">Other archived posts</div>
|
||||
{extra.map(tweet => (
|
||||
<article key={tweet.id} className="tw-conversation__post">
|
||||
<TweetCard tweet={tweet} isInThread artifactMap={artifactMap} byId={byId} mainIds={mainIds} />
|
||||
<article key={tweet.id} id={`tweet-${tweet.id}`}
|
||||
className={`tw-conversation__post${focusedTweetId === String(tweet.id) ? ' tw-conversation__post--focused' : ''}`}>
|
||||
<TweetCard tweet={tweet} isInThread artifactMap={artifactMap} byId={byId} mainIds={mainIds} extraIds={extraIds} />
|
||||
</article>
|
||||
))}
|
||||
</section>
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ export function buildTweetThread(tweets, requestedId) {
|
|||
});
|
||||
|
||||
const seed = byId.get(String(requestedId)) || [...byId.values()].at(-1);
|
||||
if (!seed) return { tree: null, rows: [], byId, mainIds: new Set(), extra: [] };
|
||||
if (!seed) return { tree: null, rows: [], byId, mainIds: new Set(), extra: [], extraIds: new Set() };
|
||||
|
||||
const children = new Map();
|
||||
for (const tweet of byId.values()) {
|
||||
|
|
@ -43,11 +43,14 @@ export function buildTweetThread(tweets, requestedId) {
|
|||
}
|
||||
const tree = visit(root, 0);
|
||||
|
||||
const relatedIds = tweet => [
|
||||
String(tweet.quoted_status_id || ''),
|
||||
String(tweet.retweeted_status?.id || ''),
|
||||
...linkedStatusIds(tweet),
|
||||
].filter(Boolean);
|
||||
const referenced = new Set();
|
||||
function markReferences(tweet) {
|
||||
const ids = [String(tweet.quoted_status_id || '')];
|
||||
ids.push(...linkedStatusIds(tweet));
|
||||
for (const id of ids) {
|
||||
for (const id of relatedIds(tweet)) {
|
||||
if (!id || !byId.has(id) || referenced.has(id)) continue;
|
||||
referenced.add(id);
|
||||
markReferences(byId.get(id));
|
||||
|
|
@ -55,12 +58,28 @@ export function buildTweetThread(tweets, requestedId) {
|
|||
}
|
||||
rows.forEach(({ tweet }) => markReferences(tweet));
|
||||
|
||||
// Keep any disconnected archived posts accessible, even if the scraper
|
||||
// returned incomplete relationship fields.
|
||||
const extra = [...byId.values()].filter(tweet =>
|
||||
!mainIds.has(String(tweet.id)) && !referenced.has(String(tweet.id))
|
||||
);
|
||||
return { tree, rows, byId, mainIds, extra };
|
||||
// Also surface references that cannot fit within the card nesting limit.
|
||||
// Those posts remain available offline under Other archived posts.
|
||||
const extraIds = new Set([...byId.keys()].filter(id => !mainIds.has(id) && !referenced.has(id)));
|
||||
function markDeferred(tweet, trail = []) {
|
||||
const id = String(tweet.id);
|
||||
const canEmbed = trail.length < 4 && !trail.includes(id);
|
||||
for (const relatedId of relatedIds(tweet)) {
|
||||
if (!byId.has(relatedId) || mainIds.has(relatedId) || extraIds.has(relatedId)) continue;
|
||||
if (!canEmbed) {
|
||||
extraIds.add(relatedId);
|
||||
} else {
|
||||
markDeferred(byId.get(relatedId), [...trail, id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
rows.forEach(({ tweet }) => markDeferred(tweet));
|
||||
// Iteration includes ids added during the walk, so deeper chains can start
|
||||
// another compact branch without losing their final archived post.
|
||||
for (const id of extraIds) markDeferred(byId.get(id));
|
||||
|
||||
const extra = [...byId.values()].filter(tweet => extraIds.has(String(tweet.id)));
|
||||
return { tree, rows, byId, mainIds, extra, extraIds };
|
||||
}
|
||||
|
||||
export function statusIdFromUrl(value) {
|
||||
|
|
|
|||
|
|
@ -37,6 +37,24 @@ describe('buildTweetThread', () => {
|
|||
expect(view.byId.has('quoted')).toBe(true);
|
||||
});
|
||||
|
||||
test('keeps a local destination for quotes beyond the card nesting limit', () => {
|
||||
const tweets = Array.from({ length: 10 }, (_, index) =>
|
||||
post(String(index + 1), null, index < 9 ? String(index + 2) : null)
|
||||
);
|
||||
const view = buildTweetThread(tweets, '1');
|
||||
expect(view.rows.map(row => row.tweet.id)).toEqual(['1']);
|
||||
expect(view.extra.map(tweet => tweet.id)).toEqual(['6']);
|
||||
expect(view.extraIds.has('6')).toBe(true);
|
||||
});
|
||||
|
||||
test('keeps an archived retweet source with its retweet', () => {
|
||||
const retweet = { ...post('retweet'), retweeted_status: { id: 'source' } };
|
||||
const view = buildTweetThread([post('source'), retweet], 'retweet');
|
||||
expect(view.rows.map(row => row.tweet.id)).toEqual(['retweet']);
|
||||
expect(view.extra).toEqual([]);
|
||||
expect(view.byId.has('source')).toBe(true);
|
||||
});
|
||||
|
||||
test('retains disconnected posts when relationship data is incomplete', () => {
|
||||
const view = buildTweetThread([post('orphan'), post('seed', 'missing')], 'seed');
|
||||
expect(view.rows.map(row => row.tweet.id)).toEqual(['seed']);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue