// "How do you know?" — the verification chain, drawn as a small timeline. // // The publication card says verified or not. This card exposes the four links // that answer is made of: the record, its claimed home, the well-known answer // there, and whether the two name the same record. import { cached } from '../../lib/cache' import { type VerifyReport, verifyPublication } from '../../lib/verify' import { $ } from '../dom' import type { Card, CardHost } from './card' let report: { key: string; value: VerifyReport } | undefined let loadedFor: string | undefined /** Which publication + page the reader has opened the chain for. */ let openFor: string | undefined function keyFor(pubUri: string, pageUrl: string): string { return `${pubUri}|${pageUrl}` } export const verifyCard: Card = { id: 'verify', mount(host) { $('verify-open').addEventListener('click', () => { const state = host.state() const pub = state?.pub if (!pub) return const key = keyFor(pub.uri, state.url) openFor = openFor === key ? undefined : key host.rerender() }) }, async load(host, refresh) { const state = host.state() const pub = state?.pub if (!pub) return const key = keyFor(pub.uri, state.url) if (!refresh && loadedFor === key) return loadedFor = key const value = await cached( { scope: 'world', subject: pub.uri, name: 'verification' }, () => verifyPublication(pub, state.url, { force: refresh }), { refresh }, ).catch((err: unknown) => { console.debug('[substandard] verification report failed', pub.uri, err) return undefined }) if (!value) return report = { key, value } host.rerender() }, render(host) { const state = host.state() const pub = state?.pub const button = $('verify-open') const panel = $('verify') if (!pub) { button.hidden = true panel.hidden = true return } button.hidden = false const key = keyFor(pub.uri, state.url) const open = openFor === key button.setAttribute('aria-expanded', String(open)) button.textContent = open ? 'Hide verification details' : 'Verification details' const value = open && report?.key === key ? report.value : undefined if (!value) { panel.hidden = true return } panel.replaceChildren(summary(value), ...value.steps.map((step) => stepRow(step, pub.uri))) panel.hidden = false }, reset() { report = undefined loadedFor = undefined openFor = undefined }, } function summary(value: VerifyReport): HTMLElement { const el = document.createElement('div') el.className = `verify-summary ${value.verified ? 'pass' : 'fail'}` const mark = document.createElement('span') mark.className = 'verify-summary-mark' mark.textContent = value.verified ? '✓' : '✗' const text = document.createElement('span') text.textContent = value.verified ? 'Verified: this site names this publication record.' : 'Not verified here: the chain does not close.' el.append(mark, text) return el } function stepRow( step: VerifyReport['steps'][number], pubUri: string, ): HTMLElement { const row = document.createElement('div') row.className = `verify-step ${step.status}` const mark = document.createElement('span') mark.className = 'verify-mark' mark.textContent = step.status === 'pass' ? '✓' : '✗' const body = document.createElement('div') body.className = 'verify-body' const label = document.createElement('span') label.className = 'verify-label' label.textContent = step.label const detail = document.createElement('span') detail.className = 'verify-detail' detail.textContent = step.detail body.append(label, detail) if (step.id === 'record') { const link = document.createElement('a') link.className = 'verify-link' // pdsls renders any at-uri and is already one of the reader destinations; // this is a record inspector, not a reader route. link.href = `https://pdsls.dev/${pubUri}` link.target = '_blank' link.rel = 'noopener noreferrer' link.textContent = 'View the record' body.append(link) } else if (step.href) { const link = document.createElement('a') link.className = 'verify-link' link.href = step.href link.target = '_blank' link.rel = 'noopener noreferrer' link.textContent = step.id === 'home' ? 'Open the home address' : 'Open the well-known' body.append(link) } row.append(mark, body) return row }