From 13cae5174fd6a554358423c32942beba7ca73d53 Mon Sep 17 00:00:00 2001 From: TheGeneralist <180094941+thegeneralist01@users.noreply.github.com> Date: Sun, 11 Oct 2026 12:56:26 +0200 Subject: [PATCH] ui: render archived X threads as conversations --- docs/README.md | 2 + frontend/src/components/PreviewModal.jsx | 8 +- frontend/src/components/PreviewPage.jsx | 2 +- frontend/src/components/PreviewPanel.jsx | 1 + frontend/src/components/TweetPreview.jsx | 272 ++++++++++++++------ frontend/src/components/tweetThread.js | 109 ++++++++ frontend/src/components/tweetThread.test.js | 78 ++++++ frontend/src/styles.css | 124 ++++++++- 8 files changed, 516 insertions(+), 80 deletions(-) create mode 100644 frontend/src/components/tweetThread.js create mode 100644 frontend/src/components/tweetThread.test.js diff --git a/docs/README.md b/docs/README.md index 80f6b9a..d81cff5 100644 --- a/docs/README.md +++ b/docs/README.md @@ -145,6 +145,8 @@ 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”. + 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 while their title still equals the old auto-generated bare-link title; a renamed entry is left alone. Titles have no diff --git a/frontend/src/components/PreviewModal.jsx b/frontend/src/components/PreviewModal.jsx index 86ce661..a4d96f8 100644 --- a/frontend/src/components/PreviewModal.jsx +++ b/frontend/src/components/PreviewModal.jsx @@ -2,6 +2,7 @@ import { useEffect } from 'react' import PreviewPanel from './PreviewPanel' export default function PreviewModal({ archiveId, entry, detail, onClose }) { + const entityKind = detail?.summary?.entity_kind // Close on Escape key useEffect(() => { const handler = (e) => { if (e.key === 'Escape') onClose() } @@ -13,11 +14,8 @@ export default function PreviewModal({ archiveId, entry, detail, onClose }) {
e.stopPropagation()} >
diff --git a/frontend/src/components/PreviewPage.jsx b/frontend/src/components/PreviewPage.jsx index 7bf9d56..625cc8b 100644 --- a/frontend/src/components/PreviewPage.jsx +++ b/frontend/src/components/PreviewPage.jsx @@ -81,7 +81,7 @@ export default function PreviewPage({ archiveId, entryUid }) { target="_blank" rel="noopener noreferrer" style={{ color: xArticle ? '#71767b' : 'var(--muted)', textDecoration: 'none', fontSize: '13px', flexShrink: 0 }} - >Original ↗ + >Original )}
diff --git a/frontend/src/components/PreviewPanel.jsx b/frontend/src/components/PreviewPanel.jsx index 196005d..701235f 100644 --- a/frontend/src/components/PreviewPanel.jsx +++ b/frontend/src/components/PreviewPanel.jsx @@ -47,6 +47,7 @@ export default function PreviewPanel({ archiveId, entry, detail, fullPage, onXAr entryUid={entryUid} artifacts={artifacts} entityKind={entityKind} + originalUrl={summary.original_url} fullPage={fullPage} onXArticle={onXArticle} /> diff --git a/frontend/src/components/TweetPreview.jsx b/frontend/src/components/TweetPreview.jsx index eb2f404..c42fce3 100644 --- a/frontend/src/components/TweetPreview.jsx +++ b/frontend/src/components/TweetPreview.jsx @@ -1,5 +1,6 @@ import { useState, useEffect } from 'react'; import { fetchEntryArtifacts, resolveTcoUrls } from '../api'; +import { buildTweetThread, linkedStatusIds, resolveEntityBounds, statusIdFromUrl } from './tweetThread'; // ── Date helper ──────────────────────────────────────────────────────────────── @@ -12,6 +13,10 @@ function fmtDate(secs) { }).format(new Date(secs * 1000)); } +function focusedTweetIdFromHash() { + return window.location.hash.match(/^#tweet-(\d+)$/)?.[1] || null; +} + // Decode common HTML entities Twitter stores in full_text. // MUST be called on already-sliced segments, not on the raw string before // entity-offset arithmetic, because entity offsets are into the stored text. @@ -40,15 +45,6 @@ const S = { margin: '0 auto', fontFamily: 'var(--sans, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif)', }, - threadOuter: { - border: '1px solid var(--line)', - borderRadius: '12px', - overflow: 'hidden', - maxWidth: '560px', - margin: '0 auto', - background: 'var(--paper)', - fontFamily: 'var(--sans, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif)', - }, tweetRow: { display: 'flex', gap: '10px', @@ -86,14 +82,6 @@ const S = { background: 'var(--line)', flexShrink: 0, }, - threadLine: { - flex: 1, - width: '2px', - background: 'var(--line-soft, var(--line))', - margin: '4px 0', - minHeight: '12px', - borderRadius: '1px', - }, authorRow: { display: 'flex', alignItems: 'baseline', @@ -536,24 +524,6 @@ function XLogo() { // links and @mentions with x.com links. Returns an array of React nodes. // white-space: pre-line on the container preserves newlines in plain segments. -// Resolve start/end offsets for a URL or mention entity, handling three -// storage formats: camelCase fromIndex/toIndex (article inline), Twitter -// native indices:[s,e] array, and fallback exact-string search in fullText. -function resolveEntityBounds(ent, fullText, ...candidates) { - if (ent.fromIndex != null && ent.toIndex != null) - return { s: ent.fromIndex, e: ent.toIndex }; - if (ent.indices?.length === 2) - return { s: ent.indices[0], e: ent.indices[1] }; - if (fullText) { - for (const c of candidates) { - if (!c) continue; - const idx = fullText.indexOf(c); - if (idx !== -1) return { s: idx, e: idx + c.length }; - } - } - return null; -} - // Build a canonical URL annotation from any entity format. // Search candidates: short url first (appears in fullText), then expanded. function normalizeUrlAnn(u, fullText) { @@ -578,7 +548,7 @@ function normalizeMentionAnn(m, fullText) { const URL_RE = /https?:\/\/[^\s<>"'\])]+/g; // Characters that commonly trail a URL but aren't part of it const TRAIL_PUNCT = /[.,;:!?)()]+$/; -function linkifyText(text, linkStyle) { +function linkifyText(text, linkStyle, mainIds) { const parts = []; let last = 0; let m; @@ -586,8 +556,11 @@ function linkifyText(text, linkStyle) { while ((m = URL_RE.exec(text)) !== null) { if (m.index > last) parts.push(text.slice(last, m.index)); let href = m[0].replace(TRAIL_PUNCT, ''); + const linkedId = statusIdFromUrl(href); + const local = linkedId && mainIds?.has(linkedId); parts.push( - + {href} ); @@ -601,7 +574,7 @@ function linkifyText(text, linkStyle) { return parts; } -function renderTweetTextJSX(fullText, entities, skipSpans = []) { +function renderTweetTextJSX(fullText, entities, skipSpans = [], mainIds) { if (!fullText) return null; const anns = [ @@ -610,7 +583,7 @@ function renderTweetTextJSX(fullText, entities, skipSpans = []) { ]; // If there are no annotations and no spans to skip, fast path. - if (anns.length === 0 && skipSpans.length === 0) return linkifyText(decodeEnt(fullText), S.link); + if (anns.length === 0 && skipSpans.length === 0) return linkifyText(decodeEnt(fullText), S.link, mainIds); const pts = new Set([0, fullText.length]); for (const a of anns) { @@ -637,8 +610,11 @@ function renderTweetTextJSX(fullText, entities, skipSpans = []) { const url = active.find(a => a.kind === 'url'); if (url) { + const linkedId = statusIdFromUrl(url.href); + const local = linkedId && mainIds?.has(linkedId); return ( - + {url.display || seg} ); @@ -659,7 +635,7 @@ function renderTweetTextJSX(fullText, entities, skipSpans = []) { ); } - return {linkifyText(decodeEnt(seg), S.link)}; + return {linkifyText(decodeEnt(seg), S.link, mainIds)}; }).filter(n => n !== null); } @@ -1009,8 +985,7 @@ function ArticleRenderer({ article, tweetAuthor, artifactMap, fullPage, onXArtic } // ── Tweet card ───────────────────────────────────────────────────────────────── -// Renders one tweet. When isInThread, omits bottom padding from the row and -// shows a thread connector line below the avatar (except on the last card). +// Renders one status, including archived quote and linked-post context. // ── Photo grid ───────────────────────────────────────────────────────────────── @@ -1089,7 +1064,7 @@ function MediaLightbox({ items, startIndex, onClose }) { const item = items[idx]; return ( -
+
e.stopPropagation()}>
{items.length > 1 && ( @@ -1101,7 +1076,7 @@ function MediaLightbox({ items, startIndex, onClose }) { rel="noopener noreferrer" style={S.lightboxLink} title="Open in new tab" - >↗ + >Open image
{item.alt @@ -1124,17 +1099,36 @@ function MediaLightbox({ items, startIndex, onClose }) { ); } -function TweetCard({ tweet, isInThread, isLast, artifactMap }) { +function TweetCard({ tweet, isInThread, artifactMap, byId, mainIds, embedded = false, trail = [] }) { const [lightboxIdx, setLightboxIdx] = useState(null); + const [expanded, setExpanded] = useState(false); const author = tweet.author || {}; const date = tweet.created_at_secs ? fmtDate(tweet.created_at_secs) : ''; const entities = tweet.entities || {}; const avatarSrc = resolveUrl(author.avatar_local_path, author.avatar_url, artifactMap); - const showConnector = isInThread && !isLast; const isQT = tweet.is_quote_status === true; - const rowStyle = isInThread ? S.tweetRowThread : S.tweetRow; + const rowStyle = embedded + ? { ...S.tweetRowThread, padding: '9px 11px' } + : isInThread ? S.tweetRowThread : S.tweetRow; const ft = tweet.full_text || ''; + const tweetId = String(tweet.id || ''); + const quoteId = String(tweet.quoted_status_id || ''); + const quoted = byId?.get(quoteId); + const replyTo = byId?.get(String(tweet.in_reply_to_status_id || '')); + const canEmbed = trail.length < 4 && !trail.includes(tweetId); + const sourceUrl = tweet.id + ? `https://x.com/${encodeURIComponent(author.screen_name || 'i')}/status/${tweetId}` + : null; + + // Linked X posts already in the archive get a local preview if they are not + // 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)) { + linked.set(id, byId.get(id)); + } + } // Build resolved media lists first; skip the grid entirely if every item resolves to null. const rawMedia = (tweet.extended_entities?.media?.length @@ -1158,7 +1152,12 @@ function TweetCard({ tweet, isInThread, isLast, artifactMap }) { 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 collapsed = canCollapse && !expanded; + const mediaSummary = [ + photos.length && `${photos.length} ${photos.length === 1 ? 'photo' : 'photos'}`, + videoItems.length && `${videoItems.length} ${videoItems.length === 1 ? 'video' : 'videos'}`, + ].filter(Boolean).join(' · '); // Suppress media attachment t.co links from the tweet text when the media // actually rendered. Uses resolveEntityBounds (prefers indices/fromIndex, // falls back to indexOf) so the span is always precise. @@ -1173,6 +1172,13 @@ function TweetCard({ tweet, isInThread, isLast, artifactMap }) { const b = resolveEntityBounds(m, ft, m.url); if (b) mediaSkipSpans.push([b.s, b.e]); } + if (quoted) { + for (const u of entities.urls || []) { + if (statusIdFromUrl(u.expanded_url || u.url) !== quoteId) continue; + const b = resolveEntityBounds(u, ft, u.url); + if (b) mediaSkipSpans.push([b.s, b.e]); + } + } return ( <> @@ -1183,18 +1189,27 @@ function TweetCard({ tweet, isInThread, isLast, artifactMap }) { onClose={() => setLightboxIdx(null)} /> )} -
+
{avatarSrc ? {author.name :
} - {showConnector &&
}
+ {isInThread && tweet.in_reply_to_status_id && ( +
+ Replying to {replyTo ? `@${replyTo.author?.screen_name || 'unknown'}` : 'a post outside this archive'} +
+ )}
- {author.name || author.screen_name || 'Unknown'} + {sourceUrl + ? + {author.name || author.screen_name || 'Unknown'} + + : {author.name || author.screen_name || 'Unknown'}} {author.screen_name && ( @{author.screen_name} )} @@ -1204,17 +1219,17 @@ function TweetCard({ tweet, isInThread, isLast, artifactMap }) { )}
-
- {renderTweetTextJSX(ft, entities, mediaSkipSpans)} +
+ {renderTweetTextJSX(ft, entities, mediaSkipSpans, mainIds)}
- {photos.length > 0 && ( + {!collapsed && photos.length > 0 && (
setLightboxIdx(i)} />
)} - {videoItems.map((v, i) => ( + {!collapsed && videoItems.map((v, i) => (