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

photo.tsx

85 lines · 2.5 KB · TypeScript
/** @jsxRuntime automatic */
/** @jsxImportSource remix/component */
import type { Action } from 'remix/router'
import { findGallery } from '../../src/domain/content.ts'
import { canonicalUrl, loadSite } 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'

/** One photo on its own page: the shareable URL the lightbox shows, and the no-JavaScript view. */
export default {
	async handler(context) {
		const site = await loadSite(context)
		const gallery = findGallery(site.content, context.params.slug ?? '')
		const index =
			gallery?.photos.findIndex((p) => p.id === context.params.photoId) ?? -1
		if (!gallery || index < 0) return notFound(site, 'That photo')
		const photo = gallery.photos[index]!
		const previous = gallery.photos[index - 1]
		const next = gallery.photos[index + 1]
		const details = cameraDetails(site.content.config, photo)
		return render(
			site,
			{
				title:
					photo.caption ??
					`${gallery.title} (${index + 1} of ${gallery.photos.length})`,
				description: photo.alt,
				canonical: canonicalUrl(
					site,
					routes.photo.href({ slug: gallery.slug, photoId: photo.id }),
				),
				nav: 'galleries',
				...ogImage(site, photo),
			},
			<section class="wrap-wide section photo-page">
				<a class="back-link" href={routes.gallery.href({ slug: gallery.slug })}>
					{icon('back')} {gallery.title}
				</a>
				<figure class="photo-figure">
					{picture(site, photo, {
						sizes: '(max-width: 1400px) 100vw, 1400px',
						priority: true,
					})}
					<figcaption>
						{photo.caption ? (
							<span class="caption">{photo.caption}</span>
						) : null}
						{details ? <span class="details">{details}</span> : null}
						<span class="counter">
							{index + 1} / {gallery.photos.length}
						</span>
					</figcaption>
				</figure>
				<nav class="pager" aria-label="Photos">
					{previous ? (
						<a
							href={routes.photo.href({
								slug: gallery.slug,
								photoId: previous.id,
							})}
							rel="prev"
						>
							← Previous
						</a>
					) : (
						<span />
					)}
					{next ? (
						<a
							href={routes.photo.href({ slug: gallery.slug, photoId: next.id })}
							rel="next"
						>
							Next →
						</a>
					) : null}
				</nav>
			</section>,
		)
	},
} satisfies Action<typeof routes.photo>