import { packageStorage } from 'kody:runtime' import * as s from 'remix/data-schema' import { summarizeGallery } from './domain/content.ts' import { parseInput, strictObject } from './domain/validation.ts' import { siteLinks } from './site-links.ts' import { pendingChanges, readDraft, readLive } from './store/site-store.ts' export type ListGalleriesInput = { from?: 'draft' | 'live' } const listInput = strictObject({ from: s.optional(s.enum_(['draft', 'live'] as const)), }) /** * List galleries in display order with their photo ids, captions, and alt text. * * @param raw - `{ from?: 'draft' | 'live' }`, default `'draft'` * @returns `{ from, galleries: [{ id, slug, title, featured, photoCount, coverPhotoId, photos: [{ id, alt, caption?, width, height }] }], pending }` * * @example * import listGalleries from 'kody:@kentcdodds/photo-portfolio/list-galleries' * const { galleries } = await listGalleries({}) */ export default async function listGalleries(raw: ListGalleriesInput = {}) { const input = parseInput(listInput, raw, 'list-galleries') const storage = packageStorage() const content = input.from === 'live' ? await readLive(storage) : await readDraft(storage) return { from: input.from ?? 'draft', galleries: content.galleries.map(summarizeGallery), pending: await pendingChanges(storage), ...(await siteLinks(storage, content.config, '/galleries')), } }