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

gallery.tsx

120 lines · 3.5 KB · TypeScript
/** @jsxRuntime automatic */
/** @jsxImportSource remix/component */
import { unsafeHTML } from 'remix/component'
import type { Action } from 'remix/router'
import { coverOf, findGallery } from '../../src/domain/content.ts'
import { buildSrcset, pickVariant } from '../../src/domain/image-plan.ts'
import type { Photo } from '../../src/domain/types.ts'
import { canonicalUrl, loadSite, variantUrl, type Site } from '../data/site.ts'
import { routes } from '../routes.ts'
import { icon } from '../ui/icons.tsx'
import { notFound } from '../ui/not-found.tsx'
import { ogImage } from '../ui/og-image.ts'
import { cameraDetails } from '../ui/photo-details.ts'
import { picture } from '../ui/picture.tsx'
import { render } from '../ui/render.tsx'

/** Shape the lightbox client reads from `#lightbox-data` (see app/assets/entry.ts). */
export type LightboxPhoto = {
	href: string
	alt: string
	caption?: string
	details?: string
	width: number
	height: number
	lqip: string
	src: string
	srcset: string
	webpSrcset: string
}

function lightboxData(
	site: Site,
	slug: string,
	photos: Array<Photo>,
): Array<LightboxPhoto> {
	const url = (photo: Photo, variant: Parameters<typeof variantUrl>[2]) =>
		variantUrl(site, photo, variant)
	return photos.map((photo) => {
		const details = cameraDetails(site.content.config, photo)
		return {
			href: routes.photo.href({ slug, photoId: photo.id }),
			alt: photo.alt,
			...(photo.caption ? { caption: photo.caption } : {}),
			...(details ? { details } : {}),
			width: photo.width,
			height: photo.height,
			lqip: photo.lqip,
			src: url(photo, pickVariant(photo, 'jpeg', 1600)!),
			srcset: buildSrcset(photo, 'jpeg', url),
			webpSrcset: buildSrcset(photo, 'webp', url),
		}
	})
}

/** JSON for an inline <script>, with `<` escaped so captions can't close the tag. */
function scriptJson(value: unknown) {
	return unsafeHTML(JSON.stringify(value).replace(/</g, '\\u003c'))
}

export default {
	async handler(context) {
		const site = await loadSite(context)
		const gallery = findGallery(site.content, context.params.slug ?? '')
		if (!gallery || gallery.slug !== context.params.slug)
			return notFound(site, 'That gallery')
		const cover = coverOf(gallery)
		return render(
			site,
			{
				title: gallery.title,
				description:
					gallery.description ??
					`${gallery.title}, a gallery by ${site.content.config.photographerName}.`,
				canonical: canonicalUrl(
					site,
					routes.gallery.href({ slug: gallery.slug }),
				),
				nav: 'galleries',
				...ogImage(site, cover),
			},
			<section class="wrap-wide section">
				<header class="page-head">
					<a class="back-link" href={routes.galleries.href()}>
						{icon('back')} All galleries
					</a>
					<h1>{gallery.title}</h1>
					{gallery.description ? (
						<p class="lede">{gallery.description}</p>
					) : null}
				</header>
				<ul class="photo-grid" data-lightbox="lightbox-data">
					{gallery.photos.map((photo, index) => (
						<li style={`--ratio:${(photo.width / photo.height).toFixed(4)}`}>
							<a
								href={routes.photo.href({
									slug: gallery.slug,
									photoId: photo.id,
								})}
								data-index={String(index)}
							>
								{picture(site, photo, {
									sizes:
										'(max-width: 640px) 100vw, (max-width: 1200px) 50vw, 33vw',
									priority: index < 3,
								})}
							</a>
						</li>
					))}
				</ul>
				<script
					type="application/json"
					id="lightbox-data"
					innerHTML={scriptJson(
						lightboxData(site, gallery.slug, gallery.photos),
					)}
				/>
			</section>,
		)
	},
} satisfies Action<typeof routes.gallery>