Skip to content
← Public packages

@kentcdodds/photo-portfolio

A fast, photo-first portfolio site you update by chatting with your agent: galleries, about, contact, and one-step rollback.

FollowFork

Files

update-gallery.ts

74 lines · 2.2 KB · TypeScript
import { packageStorage } from 'kody:runtime'
import * as s from 'remix/data-schema'
import {
	summarizeGallery,
	updateGallery,
	type GalleryPatch,
} from './domain/content.ts'
import {
	longText,
	parseInput,
	strictObject,
	text,
} from './domain/validation.ts'
import { nowIso, siteLinks } from './site-links.ts'
import { readDraft, writeDraft } from './store/site-store.ts'

export type UpdateGalleryInput = GalleryPatch & {
	/** Slug or id of the gallery to edit. */
	gallery: string
}

const updateGalleryInput = strictObject({
	gallery: s.string(),
	title: s.optional(text(100)),
	slug: s.optional(s.string()),
	description: s.optional(s.nullable(longText(1000))),
	featured: s.optional(s.boolean()),
	coverPhotoId: s.optional(s.string()),
	photoOrder: s.optional(s.array(s.string())),
	photos: s.optional(
		s.array(
			strictObject({
				id: s.string(),
				caption: s.optional(s.nullable(longText(300))),
				alt: s.optional(text(300)),
			}),
		),
	),
})

/**
 * Edit a gallery in the draft: title, slug, description, whether it is featured
 * on the home page, cover photo, photo order, and per-photo caption/alt text.
 * Photo ids come from list-galleries.
 *
 * @param raw - `{ gallery: slug | id, title?, slug?, description?: string | null, featured?, coverPhotoId?, photoOrder?: [photoId…], photos?: [{ id, caption?: string | null, alt? }] }`
 * @returns `{ gallery, previewUrl, liveUrl }`
 *
 * @example
 * import updateGallery from 'kody:@kentcdodds/photo-portfolio/update-gallery'
 * await updateGallery({
 *   gallery: 'smith-family-fall-2026',
 *   coverPhotoId: 'ph_k3x9q2m1a7b4',
 *   photos: [{ id: 'ph_k3x9q2m1a7b4', caption: 'Their favorite bench' }],
 * })
 */
export default async function updateGalleryExport(raw: UpdateGalleryInput) {
	const { gallery: ref, ...patch } = parseInput<UpdateGalleryInput>(
		updateGalleryInput,
		raw,
		'update-gallery',
	)
	const storage = packageStorage()
	const result = updateGallery(await readDraft(storage), ref, patch, nowIso())
	await writeDraft(storage, result.content)
	return {
		gallery: summarizeGallery(result.gallery),
		...(await siteLinks(
			storage,
			result.content.config,
			`/galleries/${result.gallery.slug}`,
		)),
	}
}