NGU-Web/src/pages/sections/OrgList-Map.tsx
Zaldimmar 2ad65cdc15 Write the site in US English
color, center, canceled, labeled, recognize and the rest, in UI
text, help text, comments, docs and names (ColorInput, QrCenter).
Migration 053 renames the two Site settings color CHECKs, whose
names are the messages the admin shows. Kept: events.status's
stored value 'cancelled' (shown as canceled), aria-labelledby,
schema.org's EventCancelled, and old migrations as they ran.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 13:24:11 -05:00

894 lines
29 KiB
TypeScript

import { CaretRight, X } from '@phosphor-icons/react';
import { useEffect, useRef, useState } from "react";
import { useCommunity } from "../../lib/useCommunity.ts";
import { Link } from "react-router-dom";
import ArrowLink from "../../components/ArrowLink.tsx";
import OrgLogo from "../../components/OrgLogo.tsx";
import { orgPath } from "../../lib/organizations.ts";
import { AREAS, AREA_NAMES } from "../../data/mapGrid.ts";
import { withAlpha, accentText, readableOn } from '../../lib/color.ts'
import { scrollBehavior } from "../../lib/motion.ts";
/* ═══════════════════════════════════════════════════════════════
ORGANIZATION LIST: MAP
Organizations placed on a tile grid: the map on the left, the
list on the right, selecting on either side filtering both.
Filtered to groups, and unlike the other sections that isn't
a prop. A map only makes sense for organizations that sit
somewhere, and it reads regions off the same data to color the
tiles, so "plot partners instead" isn't a filter change, it's a
different component. Named for the view rather than the filter
so that stays visible.
Why a tile grid rather than a geographic map: every state reads
at the same size (so Rhode Island is as clickable as Texas), it
stays legible on a phone, it needs no map library or GeoJSON,
and a state split between two regions is just a tile painted in
two colors. If you later want true geography, the swap point is
<RegionMap>, everything else works off the data.
The tile layout comes from mapGrid.ts; who paints what comes
from the API. A state and the Canada band are the same shape
now, a tile with a span, so the map is one loop.
═══════════════════════════════════════════════════════════════ */
const TILE = 100;
const PAD = 5;
const COLS = 13;
const ROWS = 7;
// How wide the map + list block runs. The section heading above it
// stays at max-w-6xl, so this deliberately breaks out past it.
const CONTENT_MAX = "88rem";
const MUTED = "var(--color-muted)";
const RULE = "color-mix(in srgb, var(--color-muted) 25%, transparent)";
const INK = "var(--color-body)";
const FALLBACK_COLOR = "#4a6b72";
/* organizations.color is nullable; an SVG fill left undefined paints
black, so an uncolored region takes the same fallback as a card. */
const colorOf = (item) => item.color || FALLBACK_COLOR;
const US_TITLE = "US Unity Regions";
const INTL_TITLE = "International Unity Regions";
/* ── Body content ──────────────────────────────────────────────
A group's description is ordered blocks rather than one
string, so a heading or a list added later renders instead of
vanishing. When organization and person pages arrive this should
move to a shared component; small enough to live here until then.
───────────────────────────────────────────────────────────── */
function Blocks({ blocks = [] as any[], color }) {
if (blocks.length === 0) return null;
return (
<div className="mt-4 flex flex-col gap-3" style={{ color: INK }}>
{blocks.map((block, i) => {
switch (block.type) {
case "heading":
return (
<h4 key={i} className="text-lg font-extrabold">
{block.text}
</h4>
);
case "subheading":
return (
<h5 key={i} className="font-bold">
{block.text}
</h5>
);
case "quote":
return (
<blockquote
key={i}
className="pl-3 italic"
style={{ borderLeft: `2px solid ${color}` }}
>
{block.text}
</blockquote>
);
case "list":
case "links":
return (
<ul key={i} className="list-disc ml-5 flex flex-col gap-1">
{block.items.map((item, j) => (
<li key={j}>
{item.url ? (
<a
href={item.url}
target="_blank"
rel="noopener noreferrer"
className="font-bold underline"
style={{ textDecorationColor: color }}
>
{item.text}
</a>
) : (
item.text
)}
{item.detail && (
<span className="text-sm" style={{ color: MUTED }}>
{" "}
({item.detail})
</span>
)}
</li>
))}
</ul>
);
case "divider":
return <hr key={i} style={{ borderColor: RULE }} />;
default:
return (
<p key={i} className="leading-relaxed">
{block.text}
</p>
);
}
})}
</div>
);
}
/* ── Tile ──────────────────────────────────────────────────────
Every region painting this tile gets its own rect. A whole tile
is one slice with no edge; a shared tile is two, each declaring
which edge it sits on and how much it takes. Nothing here
subtracts, and the order the slices arrive in doesn't change
what's drawn.
───────────────────────────────────────────────────────────── */
function Tile({
code,
x,
y,
size,
width = size,
label,
slices = [] as any[],
count = 0,
selected,
hovered,
setSelected,
setHovered,
onPick,
fontSize = 34,
}) {
if (slices.length === 0) return null;
const primary = slices[0];
const ids = slices.map(s => s.regionId);
const active = ids.includes(selected) || ids.includes(hovered);
const dimmed = selected && !ids.includes(selected);
const clipId = `clip-${code}`;
const opacity = count ? 1 : active ? 0.5 : 0.28;
// Clicking a shared tile cycles through its regions and then
// clears, so every slice is reachable without a second control.
const cycle = () => {
onPick?.(code);
const at = ids.indexOf(selected);
setSelected(at === ids.length - 1 ? null : ids[at + 1]);
};
return (
<g
onClick={cycle}
onMouseEnter={() => setHovered(primary.regionId)}
onMouseLeave={() => setHovered(null)}
style={{ cursor: "pointer" }}
opacity={dimmed ? 0.25 : 1}
className="transition-opacity duration-200"
>
<title>
{AREA_NAMES[code] || code}: {slices.map(s => s.name).join(" / ")}
{count ? ` · ${count} group${count > 1 ? "s" : ""}` : ""}
</title>
<clipPath id={clipId}>
<rect x={x} y={y} width={width} height={size} rx={14} />
</clipPath>
<g clipPath={`url(#${clipId})`}>
{slices.map(slice => {
const h = slice.edge ? size * slice.share : size;
const top = slice.edge === "bottom" ? y + size - h : y;
return (
<rect
key={slice.regionId}
x={x}
y={top}
width={width}
height={h}
fill={colorOf(slice)}
fillOpacity={opacity}
className="transition-all duration-200"
/>
);
})}
</g>
<rect
x={x}
y={y}
width={width}
height={size}
rx={14}
fill="none"
stroke={active ? colorOf(primary) : "var(--color-surface)"}
strokeOpacity={active ? 1 : 0.55}
strokeWidth={active ? 4 : 2}
className="transition-all duration-200"
/>
<text
x={x + width / 2}
y={y + size / 2 + 2}
textAnchor="middle"
dominantBaseline="middle"
fontSize={fontSize}
fontWeight="800"
fill={count ? "var(--color-surface)" : colorOf(primary)}
style={{ pointerEvents: "none" }}
>
{label || code}
</text>
{count > 0 && (
<circle
cx={x + width - 14}
cy={y + 14}
r={7}
fill="#ffffff"
fillOpacity={0.9}
style={{ pointerEvents: "none" }}
/>
)}
</g>
);
}
function RegionMap({ slices, groupCounts, ...props }: any) {
const size = TILE - PAD * 2;
return (
<svg
viewBox={`0 0 ${COLS * TILE} ${ROWS * TILE}`}
className="w-full h-auto"
role="group"
aria-label="Groups by Unity region"
>
{/* States and bands are the same shape, a tile with a span,
so this is one loop rather than two. */}
{AREAS.map(area => (
<Tile
key={area.code}
code={area.code}
label={area.isState ? area.code : area.name}
x={(area.col - 1) * TILE + PAD}
y={(area.row - 1) * TILE + PAD}
size={size}
width={area.span * TILE - PAD * 2}
fontSize={area.isState ? 34 : 38}
slices={slices[area.code] ?? []}
count={groupCounts[area.code] ?? 0}
{...props}
/>
))}
</svg>
);
}
function LegendButton({ region, selected, setSelected, hovered, setHovered }) {
const on = selected === region.id || hovered === region.id;
return (
<button
onClick={() => setSelected(selected === region.id ? null : region.id)}
onMouseEnter={() => setHovered(region.id)}
onMouseLeave={() => setHovered(null)}
onFocus={() => setHovered(region.id)}
onBlur={() => setHovered(null)}
aria-pressed={selected === region.id}
className="flex items-center gap-2 py-1.5 px-3 rounded-lg text-sm font-bold transition-all duration-200"
style={{
border: `1px solid ${colorOf(region)}`,
background: on ? colorOf(region) : "transparent",
color: on ? readableOn(colorOf(region)) : INK,
opacity: selected && selected !== region.id ? 0.45 : 1,
}}
>
<span
className="h-2.5 w-2.5 rounded-full"
style={{ background: on ? "#ffffff" : colorOf(region) }}
/>
{region.name}
</button>
);
}
function Legend({ domestic, international, onMapIds, ...props }: any) {
const { selected, setSelected } = props;
// A region is on the map if it paints a tile. West Central used
// to need a hardcoded exception here because its states arrived
// only through SPLITS; it has ordinary rows now, so the exception
// is gone.
const onMap = region => onMapIds.has(region.id);
const us = domestic.filter(onMap);
const intl = international.filter(onMap);
return (
<div className="mt-6">
<p className="text-xs uppercase tracking-wide mb-2" style={{ color: MUTED }}>
{US_TITLE}
</p>
<div className="flex flex-wrap gap-2">
{us.map(r => (
<LegendButton key={r.id} region={r} {...props} />
))}
</div>
<div className="h-px my-4" style={{ background: RULE }} />
<p className="text-xs uppercase tracking-wide mb-2" style={{ color: MUTED }}>
{INTL_TITLE}
</p>
<div className="flex flex-wrap gap-2 items-center">
{intl.map(r => (
<LegendButton key={r.id} region={r} {...props} />
))}
{selected && (
<button
onClick={() => setSelected(null)}
className="py-1.5 px-3 rounded-lg text-sm font-bold underline"
style={{ color: FALLBACK_COLOR }}
>
Show all
</button>
)}
</div>
</div>
);
}
function RegionBlock({
region,
groups,
subtext,
selected,
setSelected,
setHovered,
indent,
regionRefs,
groupRefs,
}) {
const on = selected === region.id;
return (
<div
ref={el => regionRefs && (regionRefs.current[region.id] = el)}
className="transition-opacity duration-200"
style={{ opacity: selected && !on ? 0.35 : 1, marginLeft: indent ? "0.75rem" : 0 }}
>
{/* The heading holds the button, not the other way round: a
heading inside a button stops being a heading. */}
<h4 className={`font-extrabold ${indent ? "text-lg" : "text-xl"}`} style={{ color: INK }}>
<button
onClick={() => setSelected(on ? null : region.id)}
onMouseEnter={() => setHovered(region.id)}
onMouseLeave={() => setHovered(null)}
aria-expanded={on}
className="w-full flex items-baseline gap-2 text-left py-2"
>
<span
className="h-3 w-3 rounded-full shrink-0"
style={{ background: colorOf(region) }}
/>
{region.name}
<span className="text-sm font-normal ml-auto" style={{ color: MUTED }}>
{groups.length || "-"}
</span>
</button>
</h4>
{subtext && (
<p className="text-sm mb-2 ml-5 leading-snug" style={{ color: MUTED }}>
{subtext}
</p>
)}
{groups.length === 0 ? (
<p className="text-sm ml-5 mb-4" style={{ color: MUTED }}>
No groups yet. Interested in starting one?
</p>
) : (
<ul className="ml-5 mb-4 flex flex-col gap-3">
{groups.map(c => (
<li
key={c.id}
ref={el => groupRefs && (groupRefs.current[c.id] = el)}
className="pl-3 flex items-start gap-3"
style={{ borderLeft: `2px solid ${colorOf(region)}` }}
>
<div className="min-w-0 flex-1">
<p className="font-bold">{c.name}</p>
<p className="text-sm" style={{ color: FALLBACK_COLOR }}>
{c.location_label}
{c.meets ? ` · ${c.meets}` : ""}
</p>
{(c.website || c.email) && (
<p className="text-sm mt-1 flex gap-4">
{c.website && (
<a
href={c.website}
target="_blank"
rel="noopener noreferrer"
className="font-bold underline"
style={{ color: colorOf(region) }}
>
Details
</a>
)}
{c.email && (
<a
href={`mailto:${c.email}`}
className="font-bold underline"
style={{ color: colorOf(region) }}
>
Contact
</a>
)}
</p>
)}
</div>
{orgPath(c) && (
<ArrowLink
to={orgPath(c)}
label={`${c.name}: group page`}
color={colorOf(region)}
size="h-8 w-8"
/>
)}
</li>
))}
</ul>
)}
</div>
);
}
/* ═══════════════════════════════════════════════════════════════
GRID VIEW
One grid per region. Clicking a card's arrow opens a detail
panel directly above that region's grid, framed in the region
color. Selecting another card swaps the panel's contents.
═══════════════════════════════════════════════════════════════ */
function GroupCard({ group, color, open, onOpen }) {
return (
<div
className="rounded-2xl p-4 flex items-start gap-4 transition-all duration-200"
style={{
border: `1px solid ${color}`,
background: open ? `${withAlpha(color, 8)}` : "transparent",
}}
>
<OrgLogo org={group} color={color} />
<div className="min-w-0 flex-1">
<p className="font-bold leading-tight">{group.name}</p>
<p className="text-sm" style={{ color: FALLBACK_COLOR }}>
{group.location_label}
</p>
{group.meets && (
<p className="text-sm" style={{ color: MUTED }}>
{group.meets}
</p>
)}
</div>
{/* Two controls, two destinations: the chevron opens the
detail panel in place, the arrow leaves for the group's
own page. Keeping them distinct beats one control that
does different things depending on where you click. */}
<div className="shrink-0 flex items-center gap-2">
<button
onClick={onOpen}
aria-expanded={open}
aria-label={`${open ? "Hide" : "View"} details for ${group.name}`}
className="h-9 w-9 rounded-full flex items-center justify-center transition-transform duration-200 hover:scale-110"
style={{
border: `1px solid ${color}`,
color,
transform: open ? "rotate(90deg)" : "none",
}}
>
<CaretRight className="h-4 w-4" weight="bold" aria-hidden="true" />
</button>
{orgPath(group) && (
<ArrowLink
to={orgPath(group)}
label={`${group.name}: group page`}
color={color}
/>
)}
</div>
</div>
);
}
function GroupDetail({ group, region, onClose }) {
/* "Led by" comes from affiliations rather than a text field, so
it lists real people and stays empty until they exist. */
const leads = (group.leadership ?? [])
.map(person =>
person.title ? `${person.display_name} (${person.title})` : person.display_name
)
.join(", ");
const rows = [
["Region", region.name],
["Where", group.venue],
["Meets", group.meets],
["Led by", leads],
["Since", group.started],
].filter(([, v]) => v);
return (
<div
className="rounded-2xl p-6 mb-6"
style={{ border: `2px solid ${colorOf(region)}`, background: `${withAlpha(colorOf(region), 6)}` }}
>
<div className="flex items-start gap-4">
<OrgLogo org={group} color={colorOf(region)} size="h-20 w-20" />
<div className="min-w-0 flex-1">
<p className="text-2xl font-extrabold leading-tight">{group.name}</p>
<p style={{ color: FALLBACK_COLOR }}>{group.location_label}</p>
</div>
<button
onClick={onClose}
aria-label="Close details"
className="shrink-0 h-8 w-8 rounded-full flex items-center justify-center text-lg font-bold transition-transform duration-200 hover:scale-110"
style={{ border: `1px solid ${colorOf(region)}`, color: colorOf(region) }}
>
<X size={20} weight="bold" aria-hidden="true" />
</button>
</div>
<Blocks blocks={group.blocks} color={colorOf(region)} />
{rows.length > 0 && (
<dl className="mt-4 grid grid-cols-1 sm:grid-cols-2 gap-x-8 gap-y-1 text-sm">
{rows.map(([label, value]) => (
<div key={label} className="flex gap-2">
<dt className="shrink-0" style={{ color: MUTED }}>
{label}
</dt>
<dd className="font-bold" style={{ color: INK }}>
{value}
</dd>
</div>
))}
</dl>
)}
<div className="mt-5 flex flex-wrap gap-3">
{orgPath(group) && (
<Link
to={orgPath(group)!}
className="py-2 px-5 rounded-xl font-bold text-sm transition-transform duration-200 hover:scale-105"
style={{ background: colorOf(region), color: readableOn(colorOf(region)) }}
>
Group page
</Link>
)}
{group.website && (
<a
href={group.website}
target="_blank"
rel="noopener noreferrer"
className="py-2 px-5 rounded-xl font-bold text-sm transition-transform duration-200 hover:scale-105"
style={{ border: `1px solid ${colorOf(region)}`, color: colorOf(region) }}
>
Visit site
</a>
)}
{group.email && (
<a
href={`mailto:${group.email}`}
className="py-2 px-5 rounded-xl font-bold text-sm transition-transform duration-200 hover:scale-105"
style={{ border: `1px solid ${colorOf(region)}`, color: colorOf(region) }}
>
Get in touch
</a>
)}
</div>
</div>
);
}
function GroupGrid({ regions, groups, groupsIn, subtextFor, openId, setOpenId }) {
// Only regions that actually have groups get a grid.
const populated = regions
.map(region => ({ region, list: groupsIn(region.id) }))
.filter(({ list }) => list.length > 0);
const empty = regions.filter(region => groupsIn(region.id).length === 0);
const openGroup = groups.find(c => c.id === openId) || null;
return (
<div className="mx-auto px-8 md:px-12" style={{ maxWidth: CONTENT_MAX }}>
{populated.map(({ region, list }) => {
const subtext = subtextFor(region);
return (
<section key={region.id} className="mb-12">
<div className="flex items-baseline gap-3 mb-1">
<span
className="h-3 w-3 rounded-full shrink-0"
style={{ background: colorOf(region) }}
/>
<h3 className="text-2xl font-extrabold" style={{ color: colorOf(region) }}>
{region.name}
</h3>
<span className="text-sm" style={{ color: MUTED }}>
{list.length}
</span>
</div>
{subtext && (
<p className="text-sm mb-5 ml-6" style={{ color: MUTED }}>
{subtext}
</p>
)}
{/* Detail panel sits above this region's grid, and only
when the open group belongs to this region. */}
{openGroup && openGroup.region_id === region.id && (
<GroupDetail
group={openGroup}
region={region}
onClose={() => setOpenId(null)}
/>
)}
<div
className="grid gap-4 items-start"
style={{
gridTemplateColumns: "repeat(auto-fill, minmax(min(22rem, 100%), 1fr))",
}}
>
{list.map(c => (
<GroupCard
key={c.id}
group={c}
color={colorOf(region)}
open={openId === c.id}
onOpen={() => setOpenId(openId === c.id ? null : c.id)}
/>
))}
</div>
</section>
);
})}
{empty.length > 0 && (
<p className="text-sm" style={{ color: MUTED }}>
No groups yet in {empty.map(r => r.name).join(", ")}. Interested in
starting one?
</p>
)}
</div>
);
}
/* The control for the section heading's action bar. */
export function OrgMapToggle({ view, setView, accent }) {
const btn = active => ({
background: active ? accent : "transparent",
color: active ? readableOn(accent) : accentText(accent),
});
return (
<div
className="inline-flex rounded-xl overflow-hidden shrink-0"
style={{ border: `1px solid ${accent}` }}
role="group"
aria-label="Change how groups are displayed"
>
{[
["map", "Map"],
["grid", "Grid"],
].map(([id, label]) => (
<button
key={id}
onClick={() => setView(id)}
aria-pressed={view === id}
className="py-2 px-4 font-bold text-sm transition-colors duration-200"
style={btn(view === id)}
>
{label}
</button>
))}
</div>
);
}
export default function OrgListMap({ view = "map", accent = FALLBACK_COLOR }) {
const {
loading,
error,
regions,
regionAreas,
domestic,
international,
virtual,
groups,
groupsIn,
slices,
groupCounts,
regionsForArea,
subtextFor,
} = useCommunity();
const [selected, setSelected] = useState<any>(null);
const [hovered, setHovered] = useState<any>(null);
const [openId, setOpenId] = useState<any>(null); // no card open on arrival
// The list scrolls itself to whatever the map or legend points at.
const listRef = useRef<any>(null);
const regionRefs = useRef<any>({});
const groupRefs = useRef<any>({});
// Where the latest hover came from. The list follows the map and
// the legend, but hovering the list itself must not move it: the
// row would slide out from under the pointer.
const hoverFromList = useRef(false);
const hoverOnMap = id => {
hoverFromList.current = false;
setHovered(id);
};
const hoverInList = id => {
hoverFromList.current = true;
setHovered(id);
};
const scrollListTo = el => {
const box = listRef.current;
if (!box || !el) return;
// Only when the list is its own scroll area (lg and up). Below
// that it's stacked under the map and scrolling it would fight
// the page.
if (box.scrollHeight <= box.clientHeight) return;
box.scrollTo({ top: el.offsetTop - 8, behavior: scrollBehavior() });
};
// Hovering or selecting a region on the map or legend brings its
// block into view. After a hover in the list, nothing scrolls: not
// on the way in, and not back to the selection on the way out.
useEffect(() => {
if (hoverFromList.current) return;
const id = hovered || selected;
if (id) scrollListTo(regionRefs.current[id]);
}, [hovered, selected]);
// Clicking a tile jumps to its first group when it has one,
// otherwise to the region it belongs to.
const pickArea = code => {
const group = groups.find(c => c.area_code === code);
if (group && groupRefs.current[group.id]) {
return scrollListTo(groupRefs.current[group.id]);
}
const region = regionsForArea(code)[0];
if (region) scrollListTo(regionRefs.current[region.id]);
};
const shell = children => (
<div className="mx-auto px-8 md:px-12" style={{ maxWidth: CONTENT_MAX }}>
{children}
</div>
);
if (loading && regions.length === 0) {
return shell(
<div className="skeleton" role="status" aria-label="Loading groups" />
);
}
if (error && regions.length === 0) {
return shell(
<p className="font-semibold" style={{ color: accentText(accent) }}>
· Groups couldn't be loaded just now, please try again shortly ·
</p>
);
}
if (view === "grid") {
return (
<GroupGrid
regions={regions}
groups={groups}
groupsIn={groupsIn}
subtextFor={subtextFor}
openId={openId}
setOpenId={setOpenId}
/>
);
}
const shared = { selected, setSelected, hovered, setHovered: hoverOnMap };
const onMapIds = new Set(regionAreas.map(a => a.region_id));
const block = (region, indent) => (
<RegionBlock
key={region.id}
region={region}
groups={groupsIn(region.id)}
subtext={subtextFor(region)}
indent={indent}
regionRefs={regionRefs}
groupRefs={groupRefs}
{...shared}
setHovered={hoverInList}
/>
);
return (
/* Wider than the section heading above it, the map needs the
room. CONTENT_MAX is the knob; drop it toward 72rem to pull
the whole block back in line with the heading. */
shell(
<div className="grid grid-cols-1 lg:grid-cols-[1.45fr_1fr] gap-10 items-stretch">
{/* Map */}
<div>
<RegionMap
slices={slices}
groupCounts={groupCounts}
onPick={pickArea}
{...shared}
/>
<Legend
domestic={domestic}
international={international}
onMapIds={onMapIds}
{...shared}
/>
<p className="text-sm mt-4" style={{ color: MUTED }}>
A filled tile means a group meets there; a two-tone tile is a
state shared by two Unity regions. Select a region to filter the
list.
</p>
</div>
{/* List, h-0 + min-h-full makes this column take its height
from the map column rather than the other way round, so a
long list scrolls instead of stretching the section. */}
<div
ref={listRef}
className="relative lg:h-0 lg:min-h-full overflow-y-auto pr-2"
>
<h3 className="text-xl font-extrabold mb-1" style={{ color: FALLBACK_COLOR }}>
{US_TITLE}
</h3>
{domestic.map(r => block(r, true))}
<h3
className="text-xl font-extrabold mt-6 mb-1 pt-4"
style={{ color: FALLBACK_COLOR, borderTop: `1px solid ${RULE}` }}
>
{INTL_TITLE}
</h3>
{international.map(r => block(r, true))}
<div className="mt-6 pt-4" style={{ borderTop: `1px solid ${RULE}` }}>
{virtual.map(r => block(r, false))}
</div>
</div>
</div>
)
);
}