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

get-config.ts

59 lines · 2.1 KB · TypeScript
import { packageStorage } from 'kody:runtime'
import * as s from 'remix/data-schema'
import { defaultConfig } from './domain/config.ts'
import type { SiteConfig } from './domain/types.ts'
import { parseInput, strictObject } from './domain/validation.ts'
import { siteLinks } from './site-links.ts'
import { pendingChanges, readDraft, readLive } from './store/site-store.ts'

export type GetConfigInput = { from?: 'draft' | 'live' }

const getConfigInput = strictObject({
	from: s.optional(s.enum_(['draft', 'live'] as const)),
})

export type ConfigView = Omit<SiteConfig, 'headshot'> & {
	headshot: { id: string; alt: string; width: number; height: number } | null
}

/**
 * Read the photographer's site settings (name, bio, contact, socials, booking,
 * theme, SEO, canonical URL). This is the only place her details live: never
 * keep them in agent memory, read them here.
 *
 * @param raw - `{ from?: 'draft' | 'live' }`, default `'draft'` (what the next publish will show)
 * @returns `{ from, config, defaults, pendingConfigFields, liveUrl, previewUrl }`.
 *   `defaults` shows each field's value in a fresh fork; `pendingConfigFields`
 *   lists fields edited since the last publish.
 *
 * @example
 * import getConfig from 'kody:@kentcdodds/photo-portfolio/get-config'
 * const { config } = await getConfig({})
 * // config.photographerName → 'Your Name'
 */
export default async function getConfig(raw: GetConfigInput = {}) {
	const input = parseInput<GetConfigInput>(getConfigInput, raw, 'get-config')
	const storage = packageStorage()
	const content =
		input.from === 'live' ? await readLive(storage) : await readDraft(storage)
	const pending = await pendingChanges(storage)
	const { headshot, ...rest } = content.config
	const config: ConfigView = {
		...rest,
		headshot: headshot
			? {
					id: headshot.id,
					alt: headshot.alt,
					width: headshot.width,
					height: headshot.height,
				}
			: null,
	}
	return {
		from: input.from ?? 'draft',
		config,
		defaults: defaultConfig,
		pendingConfigFields: pending.configFields,
		...(await siteLinks(storage, content.config)),
	}
}