Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380// The publication's standing in panoptiblog, the rankings the site publishes.//// The dataset is one static file on substandard.blog. It is somebody else's// read — nothing here writes — and the popup can live without it, so every// failure costs the row rather than the popup.
import { cached } from './cache'import { type RequestOptions, requestJson } from './http'
export const PANOPTIBLOG_URL = 'https://substandard.blog/panoptiblog/data.json'export const PANOPTIBLOG_PAGE = 'https://panoptiblog.substandard.blog/'
/** Third-party and optional: fail fast rather than hold the card open. */const OPTIONS: RequestOptions = { timeoutMs: 8_000, retries: 1 }
export interface PanoptiblogAuthor { did: string handle?: string displayName?: string avatar?: string}
export interface PanoptiblogComposite { raw: number percentile: number}
export type PanoptiblogCompositeName = | 'audience' | 'output' | 'longevity' | 'craft' | 'activity' | 'sourcing'
/** A published upward rank jump: where it started, where it landed, and when. */export interface PanoptiblogTrend { jump: number fromRank: number toRank: number jumpedAt: string windowDays: number}
export interface PanoptiblogPublication { uri: string did: string rank?: number /** The rank in the previous published run, when the job had one. */ previousRank?: number /** The jump that put this publication on Trending, when it is a mover. */ trend?: PanoptiblogTrend name: string handle?: string url?: string iconUrl?: string description?: string author?: PanoptiblogAuthor wordCount?: number docCount?: number readingMinutes?: number readingHours?: number subscribers: number recommends: number percentile?: number composites?: Partial<Record<PanoptiblogCompositeName, PanoptiblogComposite>> topics?: string[] score: number earlyAdopter?: boolean /** Kept from an earlier crawl because this run could not refresh it. */ stale?: boolean}
export interface PanoptiblogTotals { /** How many publications the run ranked. */ rankedPublications?: number [key: string]: unknown}
export interface PanoptiblogData { generatedAt: string /** When the run before this one was generated. */ previousGeneratedAt?: string totals?: PanoptiblogTotals publications: PanoptiblogPublication[] /** The published movers, when the run had history to measure them. */ trending?: PanoptiblogTrending method?: { weights?: Record<string, number> }}
/** That page with one publication's details open. */export function panoptiblogPageFor(uri: string): string { return `${PANOPTIBLOG_PAGE}#pub=${encodeURIComponent(uri)}`}
/** A number the card can format, or nothing when the field is malformed. */function finiteNumber(value: unknown): number | undefined { return typeof value === 'number' && Number.isFinite(value) ? value : undefined}
function positiveInteger(value: unknown): number | undefined { return typeof value === 'number' && Number.isInteger(value) && value > 0 ? value : undefined}
function percentile(value: unknown): number | undefined { const n = finiteNumber(value) return n !== undefined && n >= 0 && n <= 100 ? n : undefined}
function httpUrl(value: unknown): string | undefined { if (typeof value !== 'string') return undefined try { const url = new URL(value) return url.protocol === 'https:' || url.protocol === 'http:' ? url.href : undefined } catch { return undefined }}
function datestamp(value: unknown): string | undefined { if (typeof value !== 'string' || value.length === 0) return undefined return Number.isNaN(Date.parse(value)) ? undefined : value}
/** A mover's published jump, or nothing when any part is malformed. */export function parseTrend(value: unknown): PanoptiblogTrend | undefined { if (typeof value !== 'object' || value === null || Array.isArray(value)) return undefined const row = value as Record<string, unknown> const jump = positiveInteger(row.jump) const fromRank = positiveInteger(row.fromRank) const toRank = positiveInteger(row.toRank) const jumpedAt = datestamp(row.jumpedAt) const windowDays = positiveInteger(row.windowDays) if ( jump === undefined || fromRank === undefined || toRank === undefined || jumpedAt === undefined || windowDays === undefined || toRank >= fromRank || jump !== fromRank - toRank ) { return undefined } return { jump, fromRank, toRank, jumpedAt, windowDays }}
/** * One published row, or nothing when it cannot name a publication. Only * http(s) links survive, ranks are whole places, and any other field with * the wrong type is dropped rather than handed to a formatter. */export function parsePublication(value: unknown): PanoptiblogPublication | undefined { if (typeof value !== 'object' || value === null || Array.isArray(value)) return undefined const row = value as Record<string, unknown> const uri = row.uri if (typeof uri !== 'string' || uri.length === 0) return undefined return { ...row, uri, rank: positiveInteger(row.rank), previousRank: positiveInteger(row.previousRank), url: httpUrl(row.url), iconUrl: httpUrl(row.icon), wordCount: finiteNumber(row.wordCount), docCount: finiteNumber(row.docCount), readingMinutes: finiteNumber(row.readingMinutes), readingHours: finiteNumber(row.readingHours), percentile: percentile(row.percentile), earlyAdopter: typeof row.earlyAdopter === 'boolean' ? row.earlyAdopter : undefined, stale: row.stale === true ? true : undefined, trend: parseTrend(row.trend), } as PanoptiblogPublication}
/** The movers a run publishes alongside the ranking. */export interface PanoptiblogTrending { windowDays: number minJump: number asOf?: string publications: PanoptiblogPublication[]}
/** The published movers, or nothing when the dataset carries none. */export function parsePanoptiblogTrending(value: unknown): PanoptiblogTrending | undefined { if (typeof value !== 'object' || value === null || Array.isArray(value)) return undefined const row = value as Record<string, unknown> if (!Array.isArray(row.publications)) return undefined const windowDays = positiveInteger(row.windowDays) const minJump = positiveInteger(row.minJump) if (windowDays === undefined || minJump === undefined) return undefined const asOf = datestamp(row.asOf) return { windowDays, minJump, ...(asOf ? { asOf } : {}), publications: row.publications .map(parsePublication) .filter((publication): publication is PanoptiblogPublication => publication !== undefined), }}
/** Rank movement since the previous published run. */export interface PanoptiblogMovement { delta: number label: string title: string}
/** A compact movement label, or nothing when the rank did not move. */export function rankMovement( previousRank: number | undefined, rank: number | undefined,): PanoptiblogMovement | undefined { if (previousRank === undefined || rank === undefined || previousRank === rank) return undefined const delta = previousRank - rank const places = Math.abs(delta) const dir = delta > 0 ? 'Up' : 'Down' return { delta, label: `${delta > 0 ? '▲' : '▼'}${places}`, title: `${dir} ${places} ${places === 1 ? 'place' : 'places'} since the previous run`, }}
/** One publication's place in the current ranking. */export interface PanoptiblogStanding { rank: number of?: number movement?: PanoptiblogMovement asOf: string stale: boolean}
/** How old a ranking may be before the source line calls it stale. */export const STALE_AFTER_MS = 36 * 60 * 60 * 1000
function time(value: unknown): number | undefined { if (typeof value !== 'string' || value.length === 0) return undefined const parsed = Date.parse(value) return Number.isNaN(parsed) ? undefined : parsed}
const updatedFormat = new Intl.DateTimeFormat('en', { year: 'numeric', month: 'short', day: 'numeric', timeZone: 'UTC',})
const numbered = new Intl.NumberFormat('en')
/** The source line, dated from the dataset and marked once it is old. */export function sourceLine( generatedAt: string, now = Date.now(), total?: number,): { text: string; stale: boolean } { const base = 'Ranked by Panoptiblog' const field = total !== undefined && Number.isFinite(total) && total > 0 ? ` · ${numbered.format(total)} ${total === 1 ? 'publication' : 'publications'}` : '' const date = new Date(generatedAt) if (!generatedAt || Number.isNaN(date.getTime())) return { text: `${base}${field}`, stale: false } const stale = now - date.getTime() > STALE_AFTER_MS return { text: `${base}${field} · updated ${updatedFormat.format(date)}${stale ? ' · stale' : ''}`, stale, }}
const compact = new Intl.NumberFormat('en', { notation: 'compact', maximumFractionDigits: 1,})
/** A subscriber or document count, short enough for one 400px row. */export function formatCount(value: number): string { return compact.format(value)}
/** The publication's reading time, when the dataset measured one. */export function readingTime(minutes: number | undefined): string | undefined { if (!Number.isFinite(minutes) || (minutes ?? 0) <= 0) return undefined const value = minutes as number if (value < 60) return `${Math.round(value)} min` const hours = Math.floor(value / 60) const remainder = Math.round(value % 60) return remainder > 0 ? `${hours}h ${remainder}m` : `${hours}h`}
/** The overall percentile said as the population it sits above. */export function topPercent(value: number | undefined): string | undefined { if (value === undefined || !Number.isFinite(value)) return undefined const top = Math.max(1, Math.ceil(100 - value)) return `top ${top}%`}
/** * The publication's rank, field size and movement, or nothing when it is * absent or unranked. Movement needs a fresh dataset and a dated previous * run, so an old snapshot does not claim a change it cannot place in time. */export function panoptiblogStanding( data: PanoptiblogData | null, uri: string, now = Date.now(),): PanoptiblogStanding | undefined { const publication = publicationStanding(data, uri) const rank = positiveInteger(publication?.rank) if (!publication || rank === undefined) return undefined
const generatedAt = typeof data?.generatedAt === 'string' ? data.generatedAt : '' const generated = time(generatedAt) const stale = generated !== undefined && now - generated > STALE_AFTER_MS const previous = time(data?.previousGeneratedAt) !== undefined const movement = !stale && previous ? rankMovement(positiveInteger(publication.previousRank), rank) : undefined const field = positiveInteger(data?.totals?.rankedPublications)
return { rank, ...(field !== undefined && field >= rank ? { of: field } : {}), ...(movement ? { movement } : {}), asOf: generatedAt, stale, }}
/** * The dataset, or null when it cannot be read. Cached in `world` for the same * day as every other stranger's data: the ranking changes when the scheduled * job runs, not between two popup opens. */export async function fetchPanoptiblog(refresh = false): Promise<PanoptiblogData | null> { try { return await cached( { scope: 'world', subject: 'panoptiblog', name: 'data' }, async () => { const data = await requestJson<PanoptiblogData>(PANOPTIBLOG_URL, OPTIONS) if (!Array.isArray(data?.publications)) { throw new Error('panoptiblog dataset has the wrong shape') } const totals = typeof data.totals === 'object' && data.totals !== null && !Array.isArray(data.totals) ? data.totals : {} return { ...data, previousGeneratedAt: typeof data.previousGeneratedAt === 'string' ? data.previousGeneratedAt : undefined, totals: { ...totals, rankedPublications: positiveInteger(totals.rankedPublications), }, publications: data.publications .map(parsePublication) .filter((row): row is PanoptiblogPublication => row !== undefined), trending: parsePanoptiblogTrending(data.trending), } }, { refresh }, ) } catch (err) { console.debug('[substandard] panoptiblog dataset unavailable', err) return null }}
/** The publication's own row, when it ranked. */export function publicationStanding( data: PanoptiblogData | null, uri: string,): PanoptiblogPublication | undefined { return data?.publications.find((publication) => publication.uri === uri)}