import { packageStorage } from 'kody:runtime' import * as s from 'remix/data-schema' import { addPhotos, requireGallery, summarizeGallery, } from './domain/content.ts' import { parseInput, strictObject } from './domain/validation.ts' import { ingestPhotos, photoBatchSchema, type PhotoInput, } from './images/ingest.ts' import { nowIso, siteLinks } from './site-links.ts' import { discardMedia, readDraft, writeDraft } from './store/site-store.ts' export type AddPhotosInput = { gallery: string photos: Array position?: 'start' | 'end' } const addPhotosInput = strictObject({ gallery: s.string(), photos: photoBatchSchema, position: s.optional(s.enum_(['start', 'end'] as const)), }) /** * Add 1–6 photos to an existing gallery in the draft (same processing as * add-gallery). For a big shoot, call it repeatedly with batches. * * @param raw - `{ gallery: slug | id, photos: [{ url | base64, alt?, caption? }], position?: 'start' | 'end' (default 'end') }` * @returns `{ gallery, added: [photoId…], previewUrl, liveUrl }` * * @example * import addPhotos from 'kody:@kentcdodds/photo-portfolio/add-photos' * await addPhotos({ * gallery: 'smith-family-fall-2026', * photos: [{ url: 'https://example.com/smith-3.jpg', alt: 'Grandma holding the baby' }], * }) */ export default async function addPhotosExport(raw: AddPhotosInput) { const input = parseInput(addPhotosInput, raw, 'add-photos') const storage = packageStorage() const now = nowIso() const draft = await readDraft(storage) const gallery = requireGallery(draft, input.gallery) const photos = await ingestPhotos(storage, input.photos, { config: draft.config, fallbackAlt: `Photo from ${gallery.title}`, now, }) let result: ReturnType try { result = addPhotos(draft, gallery.id, photos, input.position ?? 'end', now) } catch (error) { await discardMedia( storage, photos.map((photo) => photo.id), ) throw error } await writeDraft(storage, result.content) return { gallery: summarizeGallery(result.gallery), added: photos.map((photo) => photo.id), ...(await siteLinks( storage, result.content.config, `/galleries/${result.gallery.slug}`, )), } }