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-config.ts

101 lines · 3.3 KB · TypeScript
import { packageStorage } from 'kody:runtime'
import * as s from 'remix/data-schema'
import {
	applyConfigPatch,
	configFieldsShape,
	diffConfig,
	type ConfigPatch,
} from './domain/config.ts'
import { parseInput, strictObject } from './domain/validation.ts'
import {
	ingestPhotos,
	photoInputSchema,
	type PhotoInput,
} from './images/ingest.ts'
import { nowIso, siteLinks } from './site-links.ts'
import { readDraft, writeDraft } from './store/site-store.ts'

export type UpdateConfigInput = Omit<ConfigPatch, 'headshot'> & {
	/** New headshot photo, or null to remove it. */
	headshot?: PhotoInput | null
	/** Report changes without saving. */
	dryRun?: boolean
}

const updateConfigInput = strictObject({
	...configFieldsShape,
	headshot: s.optional(s.nullable(photoInputSchema)),
	dryRun: s.optional(s.boolean()),
})

/**
 * Change site settings in the draft. Pass only the fields to change; `null`
 * clears an optional field (phone, location, bookingUrl, siteUrl, headshot, a
 * social link). Unknown keys are rejected. Changes go live with publish-changes.
 *
 * Fields: siteName, tagline, photographerName, bio (markdown), headshot
 * (`{ url | base64, alt? }`), contactEmail, phone, location, social
 * (`{ instagram?, facebook?, tiktok?, youtube?, vimeo?, x?, threads?, pinterest?, linkedin?, behance?, website? }`),
 * bookingUrl, bookingLabel, accentColor (`#rrggbb`), theme (`auto|light|dark`),
 * seoDescription, siteUrl (canonical origin), notifyOnInquiry, embedCopyright,
 * showCameraDetails.
 *
 * @param raw - partial config plus `dryRun?: boolean` (report changes, save nothing)
 * @returns `{ dryRun, changes: [{ field, from, to }], previewUrl, liveUrl }`
 *
 * @example
 * import updateConfig from 'kody:@kentcdodds/photo-portfolio/update-config'
 * await updateConfig({
 *   photographerName: 'Jane Doe',
 *   siteName: 'Jane Doe Photography',
 *   contactEmail: 'jane@example.com',
 *   social: { instagram: 'https://instagram.com/janedoe' },
 *   bookingUrl: 'https://cal.com/janedoe/session',
 *   dryRun: true,
 * })
 */
export default async function updateConfig(raw: UpdateConfigInput) {
	const {
		dryRun = false,
		headshot,
		...fields
	} = parseInput<UpdateConfigInput>(updateConfigInput, raw, 'update-config')
	const storage = packageStorage()
	const draft = await readDraft(storage)
	const now = nowIso()

	const patch: ConfigPatch = { ...fields }
	if (headshot === null) patch.headshot = null
	if (headshot && !dryRun) {
		const [photo] = await ingestPhotos(storage, [headshot], {
			config: applyConfigPatch(draft.config, fields),
			fallbackAlt: `Portrait of ${fields.photographerName ?? draft.config.photographerName}`,
			now,
		})
		patch.headshot = photo
	}
	const config = applyConfigPatch(draft.config, patch)
	const changes = diffConfig(draft.config, config)
	if (headshot && dryRun)
		changes.push({
			field: 'headshot',
			from: draft.config.headshot
				? `photo ${draft.config.headshot.id}`
				: undefined,
			to: 'new photo',
		})

	if (!dryRun && changes.length > 0)
		await writeDraft(storage, { ...draft, config, updatedAt: now })
	return {
		dryRun,
		changes,
		...(await siteLinks(storage, config)),
		next:
			changes.length === 0
				? 'Nothing changed.'
				: dryRun
					? 'Call again without dryRun to save these to the draft.'
					: 'Saved to the draft. Open previewUrl, then run publish-changes.',
	}
}