import { describeRepo, listRecordsRaw, getRecordRaw, fetchLexicon, resolveHandle, getServiceInfo } from '../lib/api.js' function extractRkey(uri: string): string { const parts = uri.split('/') return parts[parts.length - 1] } function formatDate(dateStr: string): string { const date = new Date(dateStr) return date.toLocaleDateString('ja-JP', { year: 'numeric', month: '2-digit', day: '2-digit', }) } function escapeHtml(str: string): string { return str .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') } async function renderCollections(did: string, handle: string): Promise { const collections = await describeRepo(did) if (collections.length === 0) { return '

No collections found

' } const items = collections.map(col => { const service = getServiceInfo(col) const favicon = service ? `` : '' const serviceName = service ? `${service.name}` : '' return `
  • ${favicon} ${col} ${serviceName}
  • ` }).join('') return `

    Collections

    ` } async function renderRecordList(did: string, handle: string, collection: string): Promise { const records = await listRecordsRaw(did, collection) if (records.length === 0) { return '

    No records found

    ' } const items = records.map(rec => { const rkey = extractRkey(rec.uri) const preview = rec.value.title || rec.value.text?.slice(0, 50) || rkey return `
  • ${rkey} ${preview}
  • ` }).join('') return `

    ${collection}

    ${records.length} records

      ${items}
    ` } async function renderRecordDetail(did: string, handle: string, collection: string, rkey: string): Promise { const record = await getRecordRaw(did, collection, rkey) if (!record) { return '

    Record not found

    ' } const lexicon = await fetchLexicon(collection) const schemaStatus = lexicon ? 'verified' : 'none' const schemaLabel = lexicon ? '✓ Schema' : '○ No schema' const json = JSON.stringify(record, null, 2) return `

    ${collection}

    ${record.uri}

    CID: ${record.cid}

    ${schemaLabel}
    ${escapeHtml(json)}
    ` } export async function mountAtBrowser( container: HTMLElement, handle: string, collection: string | null, rkey: string | null ): Promise { container.innerHTML = '

    Loading...

    ' try { const did = handle.startsWith('did:') ? handle : await resolveHandle(handle) let content: string let nav = '' if (collection && rkey) { nav = `← Back` content = await renderRecordDetail(did, handle, collection, rkey) } else if (collection) { nav = `← Collections` content = await renderRecordList(did, handle, collection) } else { content = await renderCollections(did, handle) } container.innerHTML = nav + content } catch (err) { container.innerHTML = `

    Failed to load: ${err}

    ` } }