Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/danielroe/roe.dev. This is the code and content for my personal website, built in Nuxt. roe.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import type { H3Event } from 'h3'
import { listRecords, blobImage } from '../atproto'import type { UsesCategory, UsesItem } from '#shared/cms/uses'
export type { UsesCategory, UsesItem } from '#shared/cms/uses'
/** * Fetch all categories with their items, joined by strong-ref. Items whose * parent category isn't in the repo are dropped (defence against orphans * if a category was deleted without cascading to its items). */export async function getUses (event: H3Event): Promise<UsesCategory[]> { const [categories, items] = await Promise.all([ listRecords(event, 'dev.roe.usesCategory'), listRecords(event, 'dev.roe.usesItem'), ])
const itemsByCategoryUri = new Map<string, typeof items>() for (const item of items) { const parentUri = item.value.category?.uri if (!parentUri) continue const bucket = itemsByCategoryUri.get(parentUri) ?? [] bucket.push(item) itemsByCategoryUri.set(parentUri, bucket) }
return Promise.all(categories.map(async cat => { const bucket = itemsByCategoryUri.get(cat.uri) ?? [] const mappedItems: UsesItem[] = await Promise.all(bucket.map(async it => { const { $type, category, image, aspectRatio, createdAt, ...passthrough } = it.value return { ...passthrough, image: image ? await blobImage(event, image, aspectRatio) : null, } }))
const { $type, createdAt, ...passthrough } = cat.value return { ...passthrough, _id: rkeyFromUri(cat.uri), items: mappedItems, } }))}
function rkeyFromUri (uri: string): string { const i = uri.lastIndexOf('/') return i === -1 ? uri : uri.slice(i + 1)}