import { packageStorage } from 'kody:runtime' import * as s from 'remix/data-schema' import { removePhoto, summarizeGallery } from './domain/content.ts' import { parseInput, strictObject } from './domain/validation.ts' import { nowIso, siteLinks } from './site-links.ts' import { readDraft, writeDraft } from './store/site-store.ts' export type RemovePhotoInput = { gallery: string; photoId: string } const removePhotoInput = strictObject({ gallery: s.string(), photoId: s.string(), }) /** * Remove one photo from a gallery in the draft. If it was the cover, the next * photo becomes the cover. A gallery can't be emptied; use remove-gallery. * * @param raw - `{ gallery: slug | id, photoId }` * @returns `{ gallery, removed: { id, alt }, previewUrl, liveUrl }` * * @example * import removePhoto from 'kody:@kentcdodds/photo-portfolio/remove-photo' * await removePhoto({ gallery: 'golden-hour', photoId: 'sample-golden-hour-3' }) */ export default async function removePhotoExport(raw: RemovePhotoInput) { const input = parseInput( removePhotoInput, raw, 'remove-photo', ) const storage = packageStorage() const result = removePhoto( await readDraft(storage), input.gallery, input.photoId, nowIso(), ) await writeDraft(storage, result.content) return { gallery: summarizeGallery(result.gallery), removed: { id: result.photo.id, alt: result.photo.alt }, ...(await siteLinks( storage, result.content.config, `/galleries/${result.gallery.slug}`, )), } }