Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154// The account behind the publication: avatar, name, proven handle, bio.//// Off the render path, because it is worth a card and not a wait. It replaced// the "by @handle" byline the publication card used to carry, which showed the// handle a DID document claimed without checking that the handle pointed back// (src/lib/profile.ts).
import { bskyProfileUrl } from '../../lib/atproto'import { cached } from '../../lib/cache'import { type Owner, blockedIdentity, fetchOwner, ownerName, ownerSubtitle, showsFollowing,} from '../../lib/profile'import { showsAuthorRole } from '../../lib/site-handle'import type { Viewer } from '../../lib/subscribers'import { $, failedIconUrls } from '../dom'import type { Card, CardHost } from './card'import { resetFollows, viewerFollows } from './follows'import { placeholder } from './loading'
/** Kept with the account it describes, so a redraw mid-load draws nothing wrong. */let owner: { did: string; info: Owner } | undefined/** Account whose lookup already ran this popup open. */let loadedFor: string | undefined/** Who the signed-in account follows, for the one question this card asks of it. */let follows: Viewer | undefined
export const ownerCard: Card = { id: 'owner',
async load(host, refresh) { const did = host.state()?.pub?.did if (!did) return // Two unrelated reads — somebody else's profile, and the reader's own // follows — so neither waits on the other to draw. await Promise.all([loadProfile(host, did, refresh), loadFollows(host, refresh)]) },
render(host) { const did = host.state()?.pub?.did // Only a second, different account above makes this one "the author". $('owner-role').hidden = !showsAuthorRole(host.siteAccount(), did) const loaded = did && owner?.did === did ? owner.info : undefined // A blocked account gets a card that does not show them (see profile.ts). // Applied here rather than at load, so the block landing while the popup is // open takes effect on the next redraw and nothing cached has to be dropped. const blocked = !!loaded && !!host.state()?.blocked const info = blocked ? blockedIdentity(loaded) : loaded const card = $<HTMLAnchorElement>('owner') if (!info) { // Nothing yet. An account always resolves to something — fetchOwner // answers with the DID when everything else fails — so the card is // coming, and standing a placeholder in its place costs no layout. renderPlaceholder(card, did ? host.loading('owner') : false) return } placeholder(card, false) card.hidden = false // The proven handle when there is one; bsky.app takes a DID just as well, // and an unproven handle must not become a link that vouches for it. card.href = bskyProfileUrl(info.handle ?? info.did) $('owner-name').textContent = ownerName(info) const subtitle = ownerSubtitle(info) $('owner-handle').textContent = subtitle ?? '' $('owner-handle').hidden = !subtitle $('owner-desc').textContent = info.description ?? '' $('owner-desc').hidden = !info.description // A follow set read for one account says nothing about the next one, and // the account can change while the popup is open (Switch account). const viewer = follows?.did === host.session()?.did ? follows : undefined $('owner-follows').hidden = !showsFollowing(info, blocked, viewer) renderAvatar(info, blocked) },
reset() { // The publication owner's profile is public and survives a sign-out; the // follows were the old account's. follows = undefined resetFollows() },}
/** * The card's own shape in grey: avatar, name, handle, a line of bio. The same * elements the real card uses, emptied and sized by the stylesheet, so the * profile lands in the boxes instead of pushing them around. */function renderPlaceholder(card: HTMLAnchorElement, on: boolean) { card.hidden = !on placeholder(card, on) if (!on) return // Nothing to open and nothing to tab to until there is a profile behind it. card.removeAttribute('href') $('owner-name').textContent = '' $('owner-handle').textContent = '' $('owner-handle').hidden = false $('owner-desc').textContent = '' $('owner-desc').hidden = false // The reader's own relationship to an account nobody has named yet. $('owner-follows').hidden = true $<HTMLImageElement>('owner-avatar').hidden = true const fallback = $('owner-avatar-fallback') fallback.textContent = '' fallback.hidden = false}
async function loadProfile(host: CardHost, did: string, refresh: boolean) { if (!refresh && loadedFor === did) return loadedFor = did const info = await cached({ scope: 'world', subject: did, name: 'owner' }, () => fetchOwner(did), { refresh, }) owner = { did, info } host.rerender()}
/** Never throws: viewerFollows answers a failed walk with undefined, and no line. */async function loadFollows(host: CardHost, refresh: boolean) { if (!refresh && follows?.did === host.session()?.did) return follows = await viewerFollows(host, refresh) host.rerender()}
function renderAvatar(info: Owner, blocked: boolean) { const img = $<HTMLImageElement>('owner-avatar') const fallback = $('owner-avatar-fallback') // An initial is part of a name, so a blocked card gets a neutral mark // instead: "Blocked user" is not what this account calls itself. fallback.textContent = blocked ? '·' : ([...ownerName(info).replace(/^@/, '')][0]?.toUpperCase() ?? '·') const url = info.avatarUrl const showFallback = () => { img.hidden = true fallback.hidden = false } if (!url || failedIconUrls.has(url)) { showFallback() return } img.onerror = () => { console.debug('[substandard] owner avatar failed to load', url) failedIconUrls.add(url) showFallback() } if (img.src !== url) img.src = url img.hidden = false fallback.hidden = true}