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 } const reorderInput = strictObject({ order: s .array(s.string()) .refine( (order: Array) => 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( 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')), } }