NGU-Web/src/pages/OrganizationDetail.tsx
Zaldimmar 8d6bb8c2b8 Content blocks: Shown in the page, the card only, or both (migration 071)
A card block also showed on the record's page (the pages printed the
card's paragraphs above the body), which the editor didn't say. Shown
in now offers The page, The card only, and The card and the page.
Every existing card block becomes both, so nothing on the site moves;
the pages no longer print card paragraphs, and a both block comes
with the page's blocks, in its place in the list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 05:27:33 -05:00

297 lines
10 KiB
TypeScript

/* ═══════════════════════════════════════════════════════════════
ORGANIZATION DETAIL
/regions/:id /groups/:id /partners/:id /organizations/:id
One component behind four routes, because organizations are one
table. The kind decides which extras render, not which file runs.
The URL kind is decoration, the slug is what identifies the
record, so a request for /groups/great-lakes when that slug is
a region redirects to the canonical path rather than rendering a
correct page at a wrong address.
Its leadership, team by team, is OrgTeams (shared with the
Organization page); its contact buttons are OrgContact.
═══════════════════════════════════════════════════════════════ */
import type { ReactNode } from 'react'
import { Link, Navigate, useLocation, useParams } from 'react-router-dom'
import PageShell from '../components/PageShell.tsx'
import PageState from '../components/PageState.tsx'
import ContentBlocks from '../components/ContentBlocks.tsx'
import OrgTeams, { hasTeams } from '../components/OrgTeams.tsx'
import OrgContact from '../components/OrgContact.tsx'
import Tags from '../components/Tags.tsx'
import EventListCards from './sections/EventList-Cards.tsx'
import {
useOrganization,
type OrganizationRecord,
} from '../lib/useContent.ts'
import { orgHref } from '../lib/hrefs.ts'
import { mediaUrl } from '../lib/media.ts'
import { initialsFor } from '../lib/organizations.ts'
import Logo from '../components/Logo.tsx'
import { withAlpha, accentText, readableOn, BRAND } from '../lib/color.ts'
import { useReadingMode } from '../lib/readingMode.ts'
const BODY = 'var(--color-muted)'
export default function OrganizationDetail() {
const { id } = useParams()
const { pathname } = useLocation()
const { data: org, loading, error, notFound, reload } = useOrganization(id)
const reading = useReadingMode()
if (!org) {
return (
<PageState
loading={loading}
error={error}
notFound={notFound}
onRetry={reload}
noun="organization"
backTo="/community"
backLabel="Community"
/>
)
}
// /groups/x when x is a region: same record, wrong address.
//
// useLocation, not window.location: the latter is outside the
// router's awareness, always "/" under HashRouter, and free to
// be stale mid-navigation, either of which turns this into a
// redirect loop rather than a one-shot correction.
//
// Guarded on org.id because a record with no id would redirect to
// /groups/undefined, which is a worse page than the one we're
// already on. All four organization routes must be registered or
// this redirects somewhere nothing matches.
const canonical = org.id ? orgHref(org.id, org.kind) : null
if (canonical && decodeURIComponent(pathname) !== canonical) {
return <Navigate to={canonical} replace />
}
const accent = org.color || BRAND
const sections: any[] = [
{
id: 'about',
title: 'About',
accent,
content: (
<div className="max-w-6xl mx-auto px-6 space-y-8">
<Facts org={org} accent={accent} />
<div className="max-w-3xl">
<ContentBlocks blocks={org.blocks} accent={accent} />
</div>
<OrgContact org={org} accent={accent} />
</div>
),
},
]
if (hasTeams(org)) {
sections.push({
id: 'leadership',
title: 'Who runs it',
accent,
content: (
<div className="max-w-6xl mx-auto px-6">
<OrgTeams org={org} accent={accent} />
</div>
),
})
}
if (org.kind === 'region' && (org.details.groups?.length ?? 0) > 0) {
sections.push({
id: 'groups',
title: 'Groups',
blurb: org.details.map_note ?? undefined,
accent,
content: (
<div className="max-w-6xl mx-auto px-6 grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
{(org.details.groups ?? []).map((group) => (
<Link
key={group.id}
to={orgHref(group.id, 'group')}
className="flex items-center gap-4 border p-4 transition-colors hover:bg-tint"
style={{ borderColor: `${withAlpha(accent, 25)}` }}
>
<GroupMark file={group.logo} name={group.name} accent={accent} />
<span>
<span className="block font-semibold" style={{ color: accentText(accent) }}>
{group.name}
</span>
{group.location_label && (
<span className="block text-sm" style={{ color: BODY }}>
{group.location_label}
</span>
)}
</span>
</Link>
))}
</div>
),
})
}
if (org.awards.length > 0) {
const recipients = (count: number) =>
count === 0 ? 'No recipients yet' : `${count} recipient${count === 1 ? '' : 's'}`
sections.push({
id: 'awards',
title: 'Awards',
blurb: `Given by ${org.short_name || org.name}.`,
accent,
// Reading mode: a card reads as one long link, so each award is
// a line with its name linked.
content: reading ? (
<ul>
{org.awards.map((award) => (
<li key={award.id}>
<Link to={`/awards/${award.id}`}>{award.name}</Link>
{award.description && `: ${award.description}`} ({recipients(award.recipient_count)})
</li>
))}
</ul>
) : (
<div className="max-w-6xl mx-auto px-6 grid gap-4 sm:grid-cols-2">
{org.awards.map((award) => (
<Link
key={award.id}
to={`/awards/${award.id}`}
className="border bg-surface p-5 transition-colors hover:bg-tint-soft"
style={{ borderColor: `${withAlpha(accent, 25)}` }}
>
<span className="block font-semibold" style={{ color: accentText(accent) }}>
{award.name}
</span>
{award.description && (
<span className="mt-1 block text-sm" style={{ color: BODY }}>
{award.description}
</span>
)}
<span className="mt-3 block text-xs uppercase tracking-wide opacity-70" style={{ color: BODY }}>
{recipients(award.recipient_count)}
</span>
</Link>
))}
</div>
),
})
}
// EventList-Cards fetches and renders this itself, its own
// docstring offers `host` for exactly this case, and a card there
// already knows about gradients, logos, past-event collapsing and
// the carousel. A second grid here was the same component written
// worse.
//
// `org.events` is still what decides whether the section exists at
// all: an organization that has never hosted anything shouldn't
// get a heading followed by "events coming soon".
if (org.events.length > 0) {
sections.push({
id: 'events',
title: 'Gatherings',
accent,
content: (
<EventListCards
host={org.id}
view="grid"
accent={accent}
empty="· Nothing on the calendar right now ·"
/>
),
})
}
return <PageShell title={org.name} intro={org.tagline ?? undefined} sections={sections} />
}
/* ── Pieces ──────────────────────────────────────────────────── */
/* "Mid-Atlantic" reads as "Mid-Atlantic Region"; a name that already
ends in Region keeps it. */
const regionName = (name?: string | null) => (name && !/region$/i.test(name.trim()) ? `${name} Region` : name)
/* The facts at the top, framed: one labeled cell each (city, region,
ministry, meeting time, since) and the record's tags. */
function Facts({
org,
accent,
}: {
org: OrganizationRecord
accent: string
}) {
const where =
org.location_label || [org.locality, org.state_code].filter(Boolean).join(', ')
const group = org.kind === 'group' ? org.details : null
const link = 'font-semibold hover:underline'
const facts = ([
['City', where],
[
'Part of',
group?.region_id && (
<Link to={orgHref(group.region_id, 'region')} className={link} style={{ color: accentText(group.region_color || accent) }}>
{regionName(group.region_name)}
</Link>
),
],
[
'Ministry',
group?.ministry_name &&
(group.ministry_url ? (
<a href={group.ministry_url} target="_blank" rel="noreferrer" className={link} style={{ color: accentText(accent) }}>
{group.ministry_name}
</a>
) : (
group.ministry_name
)),
],
['Meets', group?.meets],
['Since', group?.started],
] as [string, ReactNode][]).filter(([, value]) => Boolean(value))
if (facts.length === 0 && org.tags.length === 0) return null
return (
<div className="max-w-3xl border-t-4 bg-raised p-5 shadow-sm" style={{ borderColor: accent }}>
{facts.length > 0 && (
<dl className="grid grid-cols-1 gap-x-8 gap-y-4 sm:grid-cols-2">
{facts.map(([label, value]) => (
<div key={label}>
<dt className="text-xs font-semibold uppercase tracking-wide text-muted">{label}</dt>
<dd className="mt-0.5 font-semibold text-ink">{value}</dd>
</div>
))}
</dl>
)}
<Tags tags={org.tags} accent={accent} className={facts.length > 0 ? 'mt-4' : ''} />
</div>
)
}
function GroupMark({ file, name, accent }: { file?: string | null; name: string; accent: string }) {
return (
<Logo
src={mediaUrl(file)}
className="h-10 w-10 shrink-0 object-contain"
fallback={
<span
aria-hidden="true"
className="flex h-10 w-10 shrink-0 items-center justify-center text-xs font-bold"
style={{ background: accent, color: readableOn(accent) }}
>
{initialsFor(name)}
</span>
}
/>
)
}