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

site.ts

109 lines · 3.3 KB · TypeScript
import { KodyRuntime } from 'kody:runtime'
import type { RequestContext } from 'remix/router'
import type {
	Photo,
	PhotoVariant,
	SiteContent,
} from '../../src/domain/types.ts'
import { joinUrl, publicBaseUrl } from '../../src/domain/urls.ts'
import { variantFileName } from '../../src/domain/image-plan.ts'
import {
	readDraft,
	readLive,
	recordHostedUrl,
	storageKeys,
	type SiteStorage,
} from '../../src/store/site-store.ts'

export const previewCookie = 'pp_preview'

export type Site = {
	content: SiteContent
	/** Showing the draft through a preview link. */
	preview: boolean
	storage: SiteStorage
	appBase: string
	assetBase: string
	clientModuleUrl: string | null
	/** Canonical public base (siteUrl or hosted URL). */
	publicBase: string | null
	requestUrl: URL
}

export function readCookie(request: Request, name: string) {
	const header = request.headers.get('cookie') ?? ''
	for (const part of header.split(';')) {
		const [key, ...value] = part.trim().split('=')
		if (key === name) return decodeURIComponent(value.join('='))
	}
	return null
}

export function getStorage(context: RequestContext): SiteStorage {
	return context.get(KodyRuntime).packageStorage()
}

export async function isPreviewToken(
	storage: SiteStorage,
	token: string | null,
) {
	if (!token) return false
	const stored = await storage.get(storageKeys.previewToken)
	return typeof stored === 'string' && stored === token
}

/** Hosted URL already written to storage by this isolate. */
let recordedHostedUrl: string | undefined

export async function loadSite(context: RequestContext): Promise<Site> {
	const { packageContext } = context.get(KodyRuntime)
	const storage = getStorage(context)
	const hostedUrl = packageContext?.hostedUrl
	if (hostedUrl && hostedUrl !== recordedHostedUrl) {
		await recordHostedUrl(storage, hostedUrl)
		recordedHostedUrl = hostedUrl
	}
	const preview = await isPreviewToken(
		storage,
		readCookie(context.request, previewCookie),
	)
	const content = preview ? await readDraft(storage) : await readLive(storage)
	return {
		content,
		preview,
		storage,
		appBase: packageContext?.appBasePath ?? '',
		assetBase: packageContext?.assetBasePath ?? '',
		clientModuleUrl: packageContext?.clientModuleUrl ?? null,
		publicBase: publicBaseUrl(content.config, packageContext?.hostedUrl),
		requestUrl: context.url,
	}
}

/** Origin-relative URL for one encoded variant. */
export function variantUrl(
	site: Pick<Site, 'appBase' | 'assetBase'>,
	photo: Photo,
	variant: Pick<PhotoVariant, 'width' | 'format'>,
) {
	if (photo.source === 'bundled') {
		return `${site.assetBase}/samples/${photo.id}-${variant.width}.${variant.format === 'jpeg' ? 'jpg' : 'webp'}`
	}
	return `${site.appBase}/media/${photo.id}/${variantFileName(variant)}`
}

/** Path inside the site ("/galleries/x") for a full app path. */
export function sitePath(site: Pick<Site, 'appBase'>, appPath: string) {
	return appPath.slice(site.appBase.length) || '/'
}

/** Canonical absolute URL for an app path, using the configured siteUrl when set. */
export function canonicalUrl(site: Site, appPath: string) {
	const base = site.publicBase ?? site.requestUrl.origin + site.appBase
	return joinUrl(base, sitePath(site, appPath))
}

/** Absolute URL for an asset or media path on the serving origin (Open Graph images). */
export function absoluteUrl(site: Site, path: string) {
	return new URL(path, site.requestUrl).href
}