Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596// Publication stats: audience, output and the shape of what a publication// writes. The numbers are public, so every reader gets them, not only the// owner.//// The subscriber and document counts are Constellation's; the reading-time// average comes from the publication's own public repo.// Nothing here needs a scope the extension does not already hold.
import { listRecords } from '../../lib/atproto'import { cached } from '../../lib/cache'import { DESK_SCAN, type DeskData, buildDesk } from '../../lib/desk'import { documentCount, scanSubscribers } from '../../lib/subscribers'import type { DocumentRecord } from '../../lib/types'import { $ } from '../dom'import type { Card, CardHost } from './card'
let desk: { key: string; data: DeskData } | undefinedlet loadedFor: string | undefined
export const deskCard: Card = { id: 'desk',
async load(host, refresh) { const pub = host.state()?.pub if (!pub) return const key = pub.uri if (!refresh && loadedFor === key) return loadedFor = key const data = await cached( { scope: 'world', subject: pub.uri, name: 'desk' }, async () => { const [scan, posts, records] = await Promise.all([ scanSubscribers(pub.uri, pub.handle, { faces: false }), documentCount(pub.uri, pub.handle), listRecords<DocumentRecord>(pub.pds, pub.did, 'site.standard.document', DESK_SCAN), ]) return buildDesk(records, { subscribers: scan?.total ?? null, posts }) }, { refresh }, ).catch((err: unknown) => { console.debug('[substandard] publication stats unavailable', pub.uri, err) return undefined }) if (!data) return desk = { key, data } host.rerender() },
render(host) { const pub = host.state()?.pub const section = $('desk') if (!pub) { section.hidden = true return } const data = desk?.key === pub.uri ? desk.data : undefined if (!data) { section.hidden = true return } section.replaceChildren(header(), summary(data)) section.hidden = false },
reset() { desk = undefined loadedFor = undefined },}
function header(): HTMLElement { const head = document.createElement('div') head.className = 'desk-head' const title = document.createElement('span') title.className = 'desk-title' title.textContent = 'Publication stats' const hint = document.createElement('span') hint.className = 'desk-hint' hint.textContent = 'From public records' head.append(title, hint) return head}
function summary(data: DeskData): HTMLElement { const el = document.createElement('div') el.className = 'desk-summary' const fallback = data.truncated ? `${data.scanned}+` : String(data.scanned) const parts = [ `${data.subscribers === null ? '—' : data.subscribers} subscribers`, `${data.posts === null ? fallback : data.posts} posts`, ] if (data.averageMinutes) parts.push(`${data.averageMinutes} min avg read`) el.textContent = parts.join(' · ') return el}