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

add-gallery.ts

120 lines · 3.5 KB · TypeScript
import { packageStorage } from 'kody:runtime'
import * as s from 'remix/data-schema'
import { addGallery, summarizeGallery } from './domain/content.ts'
import {
	longText,
	parseInput,
	strictObject,
	text,
} from './domain/validation.ts'
import {
	ingestPhotos,
	photoBatchSchema,
	type PhotoInput,
} from './images/ingest.ts'
import { nowIso, siteLinks } from './site-links.ts'
import {
	discardMedia,
	publishDraft,
	readDraft,
	writeDraft,
} from './store/site-store.ts'

export type AddGalleryInput = {
	title: string
	slug?: string
	description?: string
	photos: Array<PhotoInput>
	/** Index into `photos` for the cover (default 0). */
	cover?: number
	/** Show on the home page (default true). */
	featured?: boolean
	/** Where the gallery goes in the list (default 'start'). */
	position?: 'start' | 'end'
	/** false adds and publishes every pending change (default true). */
	draft?: boolean
}

const addGalleryInput = strictObject({
	title: text(100),
	slug: s.optional(s.string()),
	description: s.optional(longText(1000)),
	photos: photoBatchSchema,
	cover: s.optional(s.number()),
	featured: s.optional(s.boolean()),
	position: s.optional(s.enum_(['start', 'end'] as const)),
	draft: s.optional(s.boolean()),
})

/**
 * Create a gallery from 1–6 photos. Each photo is resized to 480/960/1600/2400px
 * WebP + JPEG, GPS and other EXIF stripped, and given a blur placeholder.
 * Lands in the draft unless `draft: false` (which also publishes every other
 * pending change). Add more photos with add-photos.
 *
 * @param raw - `{ title, slug?, description?, photos: [{ url | base64, alt?, caption? }], cover?: index (default 0), featured?: boolean (default true), position?: 'start' | 'end' (default 'start'), draft?: boolean (default true) }`
 * @returns `{ gallery, published, version?, previewUrl, liveUrl }`
 *
 * @example
 * import addGallery from 'kody:@kentcdodds/photo-portfolio/add-gallery'
 * await addGallery({
 *   title: 'Smith Family, Fall 2026',
 *   description: 'An evening at the park.',
 *   photos: [
 *     { url: 'https://example.com/smith-1.jpg', alt: 'Family laughing on a bench', caption: 'Golden hour' },
 *     { url: 'https://example.com/smith-2.jpg', alt: 'Kids running through leaves' },
 *   ],
 * })
 */
export default async function addGalleryExport(raw: AddGalleryInput) {
	const input = parseInput<AddGalleryInput>(addGalleryInput, raw, 'add-gallery')
	const storage = packageStorage()
	const now = nowIso()
	const draft = await readDraft(storage)
	const photos = await ingestPhotos(storage, input.photos, {
		config: draft.config,
		fallbackAlt: `Photo from ${input.title.trim()}`,
		now,
	})
	let result: ReturnType<typeof addGallery>
	try {
		result = addGallery(
			draft,
			{
				id: `g_${crypto.randomUUID().slice(0, 8)}`,
				title: input.title,
				slug: input.slug,
				description: input.description,
				featured: input.featured ?? true,
				photos,
				cover: input.cover ?? 0,
				position: input.position ?? 'start',
			},
			now,
		)
	} catch (error) {
		await discardMedia(
			storage,
			photos.map((photo) => photo.id),
		)
		throw error
	}
	await writeDraft(storage, result.content)
	const published =
		input.draft === false
			? await publishDraft(storage, {
					note: `Added ${result.gallery.title}`,
					now,
				})
			: null
	return {
		gallery: summarizeGallery(result.gallery),
		published: Boolean(published),
		...(published ? { version: published.entry.version } : {}),
		...(await siteLinks(
			storage,
			result.content.config,
			`/galleries/${result.gallery.slug}`,
		)),
	}
}