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

remove-gallery.ts

57 lines · 1.8 KB · TypeScript
import { packageStorage } from 'kody:runtime'
import * as s from 'remix/data-schema'
import { removeGallery } 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 RemoveGalleryInput = { gallery: string; confirm?: boolean }

const removeGalleryInput = strictObject({
	gallery: s.string(),
	confirm: s.optional(s.boolean()),
})

/**
 * Remove a gallery from the draft. Without `confirm: true` nothing changes and
 * the result describes what would be removed, so confirm with her first. The
 * live site keeps it until publish-changes, and rollback can bring it back.
 *
 * @param raw - `{ gallery: slug | id, confirm?: boolean }`
 * @returns `{ removed: false, wouldRemove }` without confirm, else `{ removed: true, gallery, previewUrl, liveUrl }`
 *
 * @example
 * import removeGallery from 'kody:@kentcdodds/photo-portfolio/remove-gallery'
 * await removeGallery({ gallery: 'golden-hour', confirm: true })
 */
export default async function removeGalleryExport(raw: RemoveGalleryInput) {
	const input = parseInput<RemoveGalleryInput>(
		removeGalleryInput,
		raw,
		'remove-gallery',
	)
	const storage = packageStorage()
	const result = removeGallery(
		await readDraft(storage),
		input.gallery,
		nowIso(),
	)
	const summary = {
		slug: result.gallery.slug,
		title: result.gallery.title,
		photoCount: result.gallery.photos.length,
	}
	if (!input.confirm) {
		return {
			removed: false,
			wouldRemove: summary,
			next: 'Call again with confirm: true to remove it from the draft.',
		}
	}
	await writeDraft(storage, result.content)
	return {
		removed: true,
		gallery: summary,
		...(await siteLinks(storage, result.content.config)),
	}
}