Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190// The conversation under an article's Bluesky post, on request.//// Replies are arbitrary third-party content and a long tree makes the popup// long, so nothing is fetched or drawn until the reader asks. The collapsed// state is one small line; the discussion row above already carries the link// to Bluesky for anyone who wants the whole thing there.
import { bskyProfileUrl } from '../../lib/atproto'import { cached } from '../../lib/cache'import { type BskyPostRef, bskyPostRef, bskyThreadUrl } from '../../lib/discussion'import { type ThreadPost, type ThreadView, fetchThread } from '../../lib/thread'import { badgeMonogram } from '../badge'import { $ } from '../dom'import type { Card, CardHost } from './card'
let thread: { docUri: string; view: ThreadView | null } | undefined/** Article whose conversation the reader has opened. */let openFor: string | undefined
export const threadCard: Card = { id: 'thread',
mount(host) { $('thread-open').addEventListener('click', () => { const doc = host.state()?.doc const ref = doc ? bskyPostRef(doc.record) : undefined if (!doc || !ref) return if (openFor === doc.uri) { openFor = undefined host.rerender() return } openFor = doc.uri host.rerender() if (thread?.docUri !== doc.uri) void run(host, ref, doc.uri) }) },
render(host) { const doc = host.state()?.doc const ref = doc ? bskyPostRef(doc.record) : undefined const box = $('thread') if (!doc || !ref) { box.hidden = true return } box.hidden = false const open = openFor === doc.uri const button = $<HTMLButtonElement>('thread-open') button.setAttribute('aria-expanded', String(open)) button.textContent = open ? 'Hide conversation' : 'Show conversation' const body = $('thread-body') body.hidden = !open if (!open) return const view = thread?.docUri === doc.uri ? thread.view : undefined if (view === undefined) { body.replaceChildren(note('Loading conversation…')) return } if (view === null) { body.replaceChildren(note('No conversation found.')) return } body.replaceChildren( postNode(view.root, 0, true), repliesNodes(view.root.replies, 1), moreLink(bskyThreadUrl(ref), view.more), ) },
reset() { thread = undefined openFor = undefined },}
async function run(host: CardHost, ref: BskyPostRef, docUri: string): Promise<void> { const view = await cached( { scope: 'world', subject: docUri, name: 'thread' }, () => fetchThread(ref), ).catch((err: unknown) => { console.debug('[substandard] thread lookup failed', docUri, err) return null }) thread = { docUri, view } host.rerender()}
function note(text: string): HTMLElement { const el = document.createElement('p') el.className = 'note' el.textContent = text return el}
function postNode(post: ThreadPost, depth: number, root = false): HTMLElement { const el = document.createElement('div') el.className = [ 'thread-post', root ? 'thread-root' : 'thread-reply', depth > 0 ? `depth-${Math.min(depth, 2)}` : '', ] .filter(Boolean) .join(' ')
const head = document.createElement('div') head.className = 'thread-head' head.append(avatarFor(post))
const who = document.createElement('a') who.className = 'thread-author' who.href = bskyProfileUrl(post.author.handle ?? post.author.did) who.target = '_blank' who.rel = 'noopener noreferrer' who.textContent = post.author.displayName ?? `@${post.author.handle ?? post.author.did}` head.append(who)
if (post.author.handle) { const handle = document.createElement('span') handle.className = 'thread-handle' handle.textContent = `@${post.author.handle}` head.append(handle) }
const body = document.createElement('p') body.className = 'thread-text' body.textContent = post.text || 'View post'
const stats = statsLine(post) el.append(head, body) if (stats) el.append(stats) return el}
function repliesNodes(posts: ThreadPost[], depth: number): HTMLElement { const wrap = document.createElement('div') wrap.className = 'thread-replies' for (const post of posts) { wrap.append(postNode(post, depth)) if (post.replies.length > 0) wrap.append(repliesNodes(post.replies, depth + 1)) } return wrap}
function statsLine(post: ThreadPost): HTMLElement | undefined { const parts: string[] = [] if (post.likeCount) parts.push(`${post.likeCount} likes`) if (post.repostCount) parts.push(`${post.repostCount} reposts`) if (post.replyCount) parts.push(`${post.replyCount} replies`) if (parts.length === 0) return undefined const el = document.createElement('div') el.className = 'thread-stats' el.textContent = parts.join(' · ') return el}
function moreLink(url: string | undefined, more: boolean): HTMLElement { const link = document.createElement('a') link.className = 'thread-more' if (url) { link.href = url link.target = '_blank' link.rel = 'noopener noreferrer' link.textContent = more ? 'View the rest on Bluesky' : 'View on Bluesky' } else { link.href = '#' link.textContent = 'View on Bluesky' } return link}
function avatarFor(post: ThreadPost): HTMLElement { const fallback = () => { const tile = document.createElement('span') tile.className = 'thread-avatar thread-avatar-fallback' tile.textContent = badgeMonogram(post.author.displayName ?? post.author.handle ?? post.author.did) return tile } if (!post.author.avatarUrl) return fallback() const img = document.createElement('img') img.className = 'thread-avatar' img.alt = '' img.src = post.author.avatarUrl img.onerror = () => { console.debug('[substandard] thread avatar failed to load', post.author.avatarUrl) img.replaceWith(fallback()) } return img}