From 0b591b2e845da048e286148b284a4e380184364b Mon Sep 17 00:00:00 2001 From: TheGeneralist <180094941+thegeneralist01@users.noreply.github.com> Date: Sun, 11 Oct 2026 13:12:10 +0200 Subject: [PATCH] fix: keep related thread posts accessible offline --- docs/README.md | 2 +- frontend/src/components/PreviewModal.jsx | 16 ++++- frontend/src/components/TweetPreview.jsx | 74 ++++++++++++++++----- frontend/src/components/tweetThread.js | 39 ++++++++--- frontend/src/components/tweetThread.test.js | 18 +++++ 5 files changed, 117 insertions(+), 32 deletions(-) diff --git a/docs/README.md b/docs/README.md index d81cff5..5b71b2c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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 `
— @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 diff --git a/frontend/src/components/PreviewModal.jsx b/frontend/src/components/PreviewModal.jsx index a4d96f8..d969f1d 100644 --- a/frontend/src/components/PreviewModal.jsx +++ b/frontend/src/components/PreviewModal.jsx @@ -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 (
diff --git a/frontend/src/components/TweetPreview.jsx b/frontend/src/components/TweetPreview.jsx index c42fce3..0948b65 100644 --- a/frontend/src/components/TweetPreview.jsx +++ b/frontend/src/components/TweetPreview.jsx @@ -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 && (
Quoted post
- {quoted && mainIds?.has(quoteId) + {quoted && (mainIds?.has(quoteId) || extraIds?.has(quoteId)) ? - @{quoted.author?.screen_name || 'unknown'} · View in this conversation + @{quoted.author?.screen_name || 'unknown'} · View archived post : quoted && canEmbed - ? : View quoted post on X}
)} - {!collapsed && canEmbed && [...linked].map(([id, post]) => ( + {!collapsed && retweetId && ( +
+
Retweeted post
+ {retweeted && (mainIds?.has(retweetId) || extraIds?.has(retweetId)) + ? + @{retweeted.author?.screen_name || 'unknown'} · View archived post + + : retweeted && canEmbed + ? + : View retweeted post on X} +
+ )} + + {!collapsed && [...linked].map(([id, post]) => (
Linked post
- + {extraIds?.has(id) + ? + @{post.author?.screen_name || 'unknown'} · View archived post + + : canEmbed + ? + : View linked post on X}
))} @@ -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 ( <>
- +
{children.length === 1 && ( - )} {children.length > 1 && ( @@ -1311,7 +1335,7 @@ function ThreadNode({ node, artifactMap, byId, mainIds, focusedTweetId }) { Branch {index + 1} of {children.length} {child.tweet.author?.screen_name && @{child.tweet.author.screen_name}}
- ))} @@ -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 ( -
+
Conversation · {rows.length} {rows.length === 1 ? 'post' : 'posts'}
- {tree && } {extra.length > 0 && (
Other archived posts
{extra.map(tweet => ( -
- +
+
))}
diff --git a/frontend/src/components/tweetThread.js b/frontend/src/components/tweetThread.js index 19fddac..efafb7f 100644 --- a/frontend/src/components/tweetThread.js +++ b/frontend/src/components/tweetThread.js @@ -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) { diff --git a/frontend/src/components/tweetThread.test.js b/frontend/src/components/tweetThread.test.js index 2a4cb7a..349df5b 100644 --- a/frontend/src/components/tweetThread.test.js +++ b/frontend/src/components/tweetThread.test.js @@ -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']);