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

config.ts

173 lines · 5.8 KB · TypeScript
import * as s from 'remix/data-schema'
import type { Photo, SiteConfig, SocialPlatform } from './types.ts'
import { socialPlatforms } from './types.ts'
import {
	httpUrl,
	isEmail,
	isHexColor,
	longText,
	strictObject,
	text,
} from './validation.ts'

/** What a fresh fork shows until the owner runs update-config. */
export const defaultConfig: SiteConfig = {
	siteName: 'Your Name Photography',
	tagline: 'Portraits, places, and the light in between.',
	photographerName: 'Your Name',
	bio: [
		"Hi, I'm **Your Name**, a photographer who loves honest moments, good light, and the quiet details most people walk past.",
		'',
		'I photograph portraits, families, and the places that make people feel at home. Every session is relaxed and unhurried, so you can be yourself while I do the work of noticing.',
		'',
		"Based in Your City and happy to travel. If you have an idea, a date, or just a question, [get in touch](contact) and let's make something you'll keep.",
	].join('\n'),
	contactEmail: 'hello@example.com',
	location: 'Your City',
	social: {},
	bookingLabel: 'Book a session',
	accentColor: '#b8763a',
	theme: 'auto',
	seoDescription:
		'Portfolio of Your Name, a portrait and travel photographer. Browse recent galleries and book a session.',
	notifyOnInquiry: true,
	embedCopyright: true,
	showCameraDetails: false,
}

/** Fields `update-config` accepts. `null` clears an optional field. */
export type ConfigPatch = {
	siteName?: string
	tagline?: string
	photographerName?: string
	bio?: string
	headshot?: Photo | null
	contactEmail?: string
	phone?: string | null
	location?: string | null
	social?: Partial<Record<SocialPlatform, string | null>>
	bookingUrl?: string | null
	bookingLabel?: string
	accentColor?: string
	theme?: SiteConfig['theme']
	seoDescription?: string
	siteUrl?: string | null
	notifyOnInquiry?: boolean
	embedCopyright?: boolean
	showCameraDetails?: boolean
}

const socialShape = Object.fromEntries(
	socialPlatforms.map((platform) => [
		platform,
		s.optional(s.nullable(httpUrl())),
	]),
) as Record<SocialPlatform, s.Schema<unknown, string | null | undefined>>

/** Scalar config fields (everything except the headshot image). */
export const configFieldsShape = {
	siteName: s.optional(text(80)),
	tagline: s.optional(longText(160)),
	photographerName: s.optional(text(80)),
	bio: s.optional(longText(8000)),
	contactEmail: s.optional(
		s.string().refine(isEmail, 'must be an email address'),
	),
	phone: s.optional(s.nullable(longText(40))),
	location: s.optional(s.nullable(longText(80))),
	social: s.optional(strictObject(socialShape)),
	bookingUrl: s.optional(s.nullable(httpUrl())),
	bookingLabel: s.optional(text(40)),
	accentColor: s.optional(
		s.string().refine(isHexColor, 'must look like #b8763a'),
	),
	theme: s.optional(s.enum_(['auto', 'light', 'dark'] as const)),
	seoDescription: s.optional(longText(300)),
	siteUrl: s.optional(
		s.nullable(
			httpUrl().refine(
				(value: string) => !new URL(value).search && !new URL(value).hash,
				'must be a plain origin like https://janedoe.photo',
			),
		),
	),
	notifyOnInquiry: s.optional(s.boolean()),
	embedCopyright: s.optional(s.boolean()),
	showCameraDetails: s.optional(s.boolean()),
}

function trimOrUndefined(value: string | null | undefined) {
	const trimmed = value?.trim()
	return trimmed ? trimmed : undefined
}

/** Apply a validated patch. Pure: returns a new config. */
export function applyConfigPatch(
	config: SiteConfig,
	patch: ConfigPatch,
): SiteConfig {
	const next: SiteConfig = { ...config, social: { ...config.social } }
	const setRequired = <key extends keyof SiteConfig>(
		key: key,
		value: SiteConfig[key] | undefined,
	) => {
		if (value !== undefined) next[key] = value
	}
	setRequired('siteName', patch.siteName?.trim())
	setRequired('tagline', patch.tagline?.trim())
	setRequired('photographerName', patch.photographerName?.trim())
	setRequired('bio', patch.bio?.trim())
	setRequired('contactEmail', patch.contactEmail?.trim())
	setRequired('bookingLabel', patch.bookingLabel?.trim())
	setRequired('accentColor', patch.accentColor?.toLowerCase())
	setRequired('theme', patch.theme)
	setRequired('seoDescription', patch.seoDescription?.trim())
	setRequired('notifyOnInquiry', patch.notifyOnInquiry)
	setRequired('embedCopyright', patch.embedCopyright)
	setRequired('showCameraDetails', patch.showCameraDetails)

	const optionalText = ['phone', 'location', 'bookingUrl'] as const
	for (const key of optionalText) {
		if (patch[key] === undefined) continue
		const value = trimOrUndefined(patch[key])
		if (value) next[key] = value
		else delete next[key]
	}
	if (patch.siteUrl !== undefined) {
		const value = trimOrUndefined(patch.siteUrl)
		if (value) next.siteUrl = value.replace(/\/+$/, '')
		else delete next.siteUrl
	}
	if (patch.headshot !== undefined) {
		if (patch.headshot) next.headshot = patch.headshot
		else delete next.headshot
	}
	for (const [platform, url] of Object.entries(patch.social ?? {}) as Array<
		[SocialPlatform, string | null | undefined]
	>) {
		if (url === undefined) continue
		if (url) next.social[platform] = url.trim()
		else delete next.social[platform]
	}
	return next
}

export type ConfigChange = { field: string; from: unknown; to: unknown }

/** Field-level differences, with images summarized by id so previews stay small. */
export function diffConfig(
	before: SiteConfig,
	after: SiteConfig,
): Array<ConfigChange> {
	const summarize = (key: string, value: unknown) =>
		key === 'headshot' && value ? `photo ${(value as Photo).id}` : value
	const keys = new Set([...Object.keys(before), ...Object.keys(after)])
	const changes: Array<ConfigChange> = []
	for (const key of keys) {
		const from = summarize(key, (before as Record<string, unknown>)[key])
		const to = summarize(key, (after as Record<string, unknown>)[key])
		if (JSON.stringify(from) !== JSON.stringify(to))
			changes.push({ field: key, from, to })
	}
	return changes
}