NGU-Web/src/components/MapBlock.tsx
Zaldimmar 56a6e11655 Map block: sections of its own, shown one at a time
Migration 094 adds content_block_map_sections: a map block groups its
venue's locations for its page, eg. Main spaces, without changing the
venue. With sections the API cuts the block's venue to the locations
they hold, in their order; the list groups under their names, and
buttons (All, then each section) show one section's marks and rows.
A schedule's place links only when a map on the page shows it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 03:42:50 -06:00

372 lines
14 KiB
TypeScript

/* ═══════════════════════════════════════════════════════════════
MAP BLOCK
The 'map' content block (088): a venue's map picture with its
locations marked on it, each a numbered pin or a box, and the
locations listed beside it (under it on a phone).
One picture, the marks drawn over it in percent of its size, so
they stay on their places at any width. A location is lit up
(in the accent, the rest dimmed) while its row or mark is hovered
or focused, and when the address points at it: #map-<venue>-<key>,
the link a schedule entry's place makes (mapAnchor). Those ids
sit at the block's top, so Layout's jump to the anchor brings the
whole map into view, not the mark at the top of the screen.
The picture zooms (the buttons over it), scrolling inside its box
when it's bigger than it; the marks are in percent, so they keep
their places. A location's Google and Apple Maps links (091) are
buttons at the right of its row.
MapPicture is the picture and its marks alone; the event's
presentation (EventPresent.tsx) draws it too, with no links.
Reading mode (readingMode.ts) gets the list alone, no picture.
A block's own sections (094), eg. Main spaces, group the list
under their names; the API has already cut the venue to the
locations they hold. Buttons over the list (All, then one per
section) show one section alone, its marks on the map and its
rows in the list. A link to a location outside the section in
view goes back to All.
PageMaps holds the locations mapped by the blocks drawn together
(their anchors, ContentBlocks), so a schedule's places link only
when there's a map on the page showing them.
═══════════════════════════════════════════════════════════════ */
import { createContext, useEffect, useState, type CSSProperties, type ReactNode } from 'react'
import { AppleLogo, ArrowsIn, GoogleLogo, MagnifyingGlassMinus, MagnifyingGlassPlus } from '@phosphor-icons/react'
import { Link, useLocation, type To } from 'react-router-dom'
import { mediaUrl } from '../lib/media.ts'
import type { MapSection, Venue, VenueLocation } from '../lib/useContent.ts'
import { accentText, readableOn, withAlpha } from '../lib/color.ts'
import { useReadingMode } from '../lib/readingMode.ts'
export const PageMaps = createContext<Set<string>>(new Set())
export const mapAnchor = (venueId: string, key: string) => `map-${venueId}-${key}`
/* The picture's zoom steps, 1 the column's width. */
const ZOOMS = [1, 1.5, 2, 3, 4]
const isBox = (location: VenueLocation) => location.w != null && location.h != null
export const isMarked = (location: VenueLocation) => location.x != null && location.y != null
/* A location's number on the map and in the list: its place in the
venue's order. */
export const locationNumber = (venue: Venue, location: VenueLocation) => venue.locations.indexOf(location) + 1
/* A location's numbered dot, in the accent. */
export function LocationNumber({
n,
accent,
className = 'h-6 w-6 text-xs',
style,
}: {
n: number
accent: string
className?: string
style?: CSSProperties
}) {
return (
<span
aria-hidden="true"
className={`flex shrink-0 items-center justify-center rounded-full font-bold ${className}`}
style={{ background: accent, color: readableOn(accent.startsWith('#') ? accent : '#000000'), ...style }}
>
{n}
</span>
)
}
/* The venue's picture with its marks, `active` lit up. With `link`
each mark is a link (and `light` its hover and focus); without,
the marks are only drawn. `className` sizes the picture's box. */
export function MapPicture({
venue,
accent,
active,
link,
light,
className = '',
big = false,
only,
}: {
venue: Venue
accent: string
active: string | null
/** Marks only these locations' keys; all when unset. */
only?: Set<string>
link?: (key: string) => To
light?: (key: string) => object
className?: string
/** Bigger marks, for a screen across a room. */
big?: boolean
}) {
const image = mediaUrl(venue.map_image)
if (!image) return null
return (
<div className={`relative ${className}`}>
<img src={image} alt={venue.map_alt || `Map of ${venue.name}`} className="block w-full" />
{venue.locations.filter((location) => isMarked(location) && (!only || only.has(location.key))).map((location) => {
const on = active === location.key
const dim = active != null && !on
const style: CSSProperties = isBox(location)
? {
left: `${location.x}%`,
top: `${location.y}%`,
width: `${location.w}%`,
height: `${location.h}%`,
border: `${big ? 4 : 2}px solid ${accent}`,
background: withAlpha(accent, on ? 35 : 12),
opacity: dim ? 0.35 : 1,
}
: {
left: `${location.x}%`,
top: `${location.y}%`,
transform: 'translate(-50%, -50%)',
opacity: dim ? 0.35 : 1,
zIndex: on ? 1 : undefined,
}
const dot = big
? isBox(location)
? 'absolute top-[0.5vw] left-[0.5vw] h-[2.6vw] w-[2.6vw] text-[1.3vw]'
: on
? 'h-[4vw] w-[4vw] text-[2vw]'
: 'h-[2.8vw] w-[2.8vw] text-[1.4vw]'
: isBox(location)
? 'absolute top-1 left-1 h-6 w-6 text-xs'
: on
? 'h-9 w-9 text-xs'
: 'h-7 w-7 text-xs'
const inside = (
<>
<LocationNumber
n={locationNumber(venue, location)}
accent={accent}
className={`shadow ${dot}`}
style={on ? { outline: `${big ? 4 : 3}px solid white` } : undefined}
/>
{on && !big && (
<span
aria-hidden="true"
className="absolute top-full left-1/2 mt-1 -translate-x-1/2 whitespace-nowrap bg-raised px-2 py-0.5 text-xs font-semibold text-ink shadow"
>
{location.name}
</span>
)}
</>
)
return link ? (
<Link
key={location.key}
to={link(location.key)}
aria-label={location.name}
{...light?.(location.key)}
className="absolute transition-opacity"
style={style}
>
{inside}
</Link>
) : (
<span key={location.key} aria-hidden="true" className="absolute transition-opacity duration-500" style={style}>
{inside}
</span>
)
})}
</div>
)
}
export default function MapBlock({
venue,
sections = [],
accent,
}: {
venue: Venue
sections?: MapSection[]
accent: string
}) {
const { hash, search } = useLocation()
const [hovered, setHovered] = useState<string | null>(null)
const [zoom, setZoom] = useState(0)
// The section in view, by index; null is All.
const [view, setView] = useState<number | null>(null)
const fromHash = venue.locations.find((location) => hash === `#${mapAnchor(venue.id, location.key)}`)?.key ?? null
const viewed = view != null ? sections[view] : undefined
// A link to a place outside the section in view shows All again.
useEffect(() => {
if (fromHash && viewed && !viewed.keys.includes(fromHash)) setView(null)
}, [fromHash, viewed])
const byKey = new Map(venue.locations.map((location) => [location.key, location]))
const groups: { name?: string; locations: VenueLocation[] }[] = sections.length
? (viewed ? [viewed] : sections).map((section) => ({
name: section.name,
locations: section.keys.flatMap((key) => byKey.get(key) ?? []),
}))
: [{ locations: venue.locations }]
const only = viewed ? new Set(viewed.keys) : undefined
const active = hovered ?? fromHash
const picture = !useReadingMode() && Boolean(venue.map_image)
const to = (key: string) => ({ search, hash: mapAnchor(venue.id, key) })
const light = (key: string) => ({
onMouseEnter: () => setHovered(key),
onMouseLeave: () => setHovered(null),
onFocus: () => setHovered(key),
onBlur: () => setHovered(null),
})
return (
<figure className="relative grid gap-6 md:grid-cols-[minmax(0,2fr)_minmax(0,1fr)]">
{/* Where a link to a location lands: the map's top. */}
{venue.locations.map((location) => (
<span key={location.key} id={mapAnchor(venue.id, location.key)} className="absolute top-0 left-0" />
))}
{picture && (
<div className="relative self-start">
<div className="max-h-[80vh] overflow-auto border border-brand/15">
<div style={{ width: `${ZOOMS[zoom] * 100}%` }}>
<MapPicture venue={venue} accent={accent} active={active} link={to} light={light} only={only} />
</div>
</div>
<div className="absolute top-2 right-2 flex gap-1">
<ZoomButton label="Zoom in" disabled={zoom === ZOOMS.length - 1} onClick={() => setZoom(zoom + 1)}>
<MagnifyingGlassPlus weight="bold" className="h-5 w-5" />
</ZoomButton>
<ZoomButton label="Zoom out" disabled={zoom === 0} onClick={() => setZoom(zoom - 1)}>
<MagnifyingGlassMinus weight="bold" className="h-5 w-5" />
</ZoomButton>
{zoom > 0 && (
<ZoomButton label="Fit the map" onClick={() => setZoom(0)}>
<ArrowsIn weight="bold" className="h-5 w-5" />
</ZoomButton>
)}
</div>
</div>
)}
<figcaption className={picture ? '' : 'md:col-span-2'}>
<p className="font-display font-bold text-ink">{venue.name}</p>
{venue.address && <p className="text-sm text-muted">{venue.address}</p>}
{sections.length > 1 && (
<div role="group" aria-label="Show" className="mt-3 flex flex-wrap gap-2">
{[null, ...sections.map((_, i) => i)].map((i) => {
const selected = view === i
return (
<button
key={i ?? 'all'}
type="button"
aria-pressed={selected}
onClick={() => setView(i)}
className="btn border px-3 py-1 text-xs transition-colors"
style={
selected
? { background: accent, borderColor: accent, color: readableOn(accent) }
: { borderColor: withAlpha(accent, 33), color: accentText(accent) }
}
>
{i == null ? 'All' : sections[i].name}
</button>
)
})}
</div>
)}
{groups.map((group, g) => (
<div key={group.name ?? g} className="mt-3">
{group.name && <p className="px-2 text-sm font-bold text-ink">{group.name}</p>}
<ol className="mt-1 space-y-1">
{group.locations.map((location) => {
const on = active === location.key
const row = (
<>
<LocationNumber n={locationNumber(venue, location)} accent={accent} />
<span className="min-w-0">
<span className="block font-semibold" style={on ? { color: accentText(accent) } : undefined}>
{location.name}
</span>
{location.detail && <span className="block text-sm text-muted">{location.detail}</span>}
</span>
</>
)
const rowClass = 'flex items-start gap-3 px-2 py-1.5 text-body'
const rowStyle = on ? { background: withAlpha(accent, 12) } : undefined
return (
<li key={location.key} className="flex items-start gap-2">
{isMarked(location) ? (
<Link to={to(location.key)} {...light(location.key)} className={`${rowClass} flex-1`} style={rowStyle}>
{row}
</Link>
) : (
<div className={`${rowClass} flex-1`}>{row}</div>
)}
<MapLinks location={location} accent={accent} />
</li>
)
})}
</ol>
</div>
))}
</figcaption>
</figure>
)
}
/* A zoom button over the map picture. */
function ZoomButton({
label,
disabled,
onClick,
children,
}: {
label: string
disabled?: boolean
onClick: () => void
children: ReactNode
}) {
return (
<button
type="button"
aria-label={label}
title={label}
disabled={disabled}
onClick={onClick}
className="btn flex h-9 w-9 items-center justify-center bg-raised text-ink shadow transition-colors hover:text-link disabled:opacity-40"
>
{children}
</button>
)
}
/* A location's Google and Apple Maps links (091), as buttons that
open them in a new tab. */
function MapLinks({ location, accent }: { location: VenueLocation; accent: string }) {
const links = [
{ href: location.google_maps_url, label: 'Google Maps', Icon: GoogleLogo },
{ href: location.apple_maps_url, label: 'Apple Maps', Icon: AppleLogo },
].filter((link) => link.href)
if (links.length === 0) return null
return (
<span className="flex shrink-0 gap-1 py-1.5">
{links.map(({ href, label, Icon }) => (
<a
key={label}
href={href!}
target="_blank"
rel="noreferrer"
aria-label={`${location.name} in ${label} (opens in a new tab)`}
title={label}
className="btn flex h-8 w-8 items-center justify-center border transition-colors hover:bg-tint"
style={{ borderColor: withAlpha(accent, 33), color: accentText(accent) }}
>
<Icon weight="bold" className="h-4 w-4" aria-hidden="true" />
</a>
))}
</span>
)
}