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

reorder-galleries.ts

49 lines · 1.4 KB · TypeScript
import { packageStorage } from 'kody:runtime'
import * as s from 'remix/data-schema'
import { reorderGalleries } 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 ReorderGalleriesInput = { order: Array<string> }

const reorderInput = strictObject({
	order: s
		.array(s.string())
		.refine(
			(order: Array<string>) => order.length > 0,
			'list at least one gallery',
		),
})

/**
 * Set gallery order in the draft (home page and gallery index). Listed
 * galleries move to the front in that order; unlisted ones keep their order after them.
 *
 * @param raw - `{ order: [slug | id, …] }`
 * @returns `{ order: [slug…], previewUrl, liveUrl }`
 *
 * @example
 * import reorderGalleries from 'kody:@kentcdodds/photo-portfolio/reorder-galleries'
 * await reorderGalleries({ order: ['weddings', 'portraits'] })
 */
export default async function reorderGalleriesExport(
	raw: ReorderGalleriesInput,
) {
	const input = parseInput<ReorderGalleriesInput>(
		reorderInput,
		raw,
		'reorder-galleries',
	)
	const storage = packageStorage()
	const content = reorderGalleries(
		await readDraft(storage),
		input.order,
		nowIso(),
	)
	await writeDraft(storage, content)
	return {
		order: content.galleries.map((g) => g.slug),
		...(await siteLinks(storage, content.config, '/galleries')),
	}
}