Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146// The account the page itself is, when its hostname is an atproto handle.//// Distinct from the owner card: that one is the account behind the// publication on the page, which is often a different account (a reader site// showing somebody else's publication). This card names the page's own// account, and the two are labelled separately when both are on screen. They// collapse to the owner card when they are the same DID, which is what a// publication's own home looks like.//// The profile data is the same read the owner card makes, with the same cache// key, so the same-DID case costs nothing extra and a reader page showing a// different account costs one more profile fetch.
import { bskyProfileUrl } from '../../lib/atproto'import { cached } from '../../lib/cache'import { type Owner, fetchOwner, ownerName, ownerSubtitle, showsFollowing } from '../../lib/profile'import { showsSiteProfile } 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 site: { 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 siteCard: Card = { id: 'site',
async load(host, refresh) { const account = host.siteAccount() // A publication's own home has one account, and the owner card covers it. if (!showsSiteProfile(account, host.state()?.pub?.did)) return // Two unrelated reads, as on the owner card: the profile is somebody // else's PDS, the follow set is the reader's own. await Promise.all([loadProfile(host, account!.did, refresh), loadFollows(host, refresh)]) },
render(host) { const account = host.siteAccount() const pubDid = host.state()?.pub?.did const section = $('site') const card = $<HTMLAnchorElement>('site-profile') // A publication's own home is one account, and the owner card already // draws it. Only a distinct page account earns a second card. if (!showsSiteProfile(account, pubDid)) { section.hidden = true return } const loaded = account && site?.did === account.did ? site.info : undefined if (!loaded) { const loading = !!account && host.loading('site') section.hidden = !loading renderPlaceholder(card, loading) return } placeholder(card, false) section.hidden = false card.hidden = false card.href = bskyProfileUrl(loaded.handle ?? loaded.did) $('site-name').textContent = ownerName(loaded) const subtitle = ownerSubtitle(loaded) $('site-handle').textContent = subtitle ?? '' $('site-handle').hidden = !subtitle $('site-desc').textContent = loaded.description ?? '' $('site-desc').hidden = !loaded.description const viewer = follows?.did === host.session()?.did ? follows : undefined $('site-follows').hidden = !showsFollowing(loaded, false, viewer) renderAvatar(loaded) },
reset() { // The profile is public and survives a sign-out; the follows did not. site = undefined loadedFor = undefined follows = undefined resetFollows() },}
/** * The card's own shape in grey, matching the owner card's placeholder so the * two never move each other when one answers before the other. */function renderPlaceholder(card: HTMLAnchorElement, on: boolean) { card.hidden = !on placeholder(card, on) if (!on) return card.removeAttribute('href') $('site-name').textContent = '' $('site-handle').textContent = '' $('site-handle').hidden = false $('site-desc').textContent = '' $('site-desc').hidden = false $('site-follows').hidden = true $<HTMLImageElement>('site-avatar').hidden = true const fallback = $('site-avatar-fallback') fallback.textContent = '' fallback.hidden = false}
async function loadProfile(host: CardHost, did: string, refresh: boolean) { if (!refresh && loadedFor === did) return loadedFor = did // Same key as the owner card: when the two DIDs match, one read serves both. const info = await cached({ scope: 'world', subject: did, name: 'owner' }, () => fetchOwner(did), { refresh, }) site = { 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) { const img = $<HTMLImageElement>('site-avatar') const fallback = $('site-avatar-fallback') fallback.textContent = [...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] site avatar failed to load', url) failedIconUrls.add(url) showFallback() } if (img.src !== url) img.src = url img.hidden = false fallback.hidden = true}