NGU-Web/src/lib/organizations.ts
Zaldimmar fd7e1b6fc1 Organization cards: list partners, ministries or a hand-picked set (migration 067)
The Partners section's preset gets "Cards: list" (partners by default,
ministries, or both) and an optional hand-picked list
(page_section_orgs, partners or ministries only, enforced by trigger)
that replaces it in its own order. /organizations takes kind=ministry
and ids=; the plain list still leaves ministries out. Ministry cards
link to their website only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 22:40:24 -05:00

91 lines
3.7 KiB
TypeScript

/* ═══════════════════════════════════════════════════════════════
ORGANIZATIONS
Regions, groups, partners and NGU itself are one table and one
endpoint. Anything that lists organizations reads them the same
way and filters by kind:
const { organizations } = useOrganizations("region");
const { organizations } = useOrganizations("partner");
const { organizations } = useOrganizations(); // all
Every organization has the same card surface, name, color,
logo, description, links. What differs by kind sits under
`details`, so a list component can render the common parts
without knowing what it's holding:
region { scope, map_note, areas[], groups[] }
group { region_id, region_name, region_color, meets, started }
partner {}
Each kind is a separate request path, so the cache in api.ts
keys them apart and two sections asking for regions share one
fetch.
═══════════════════════════════════════════════════════════════ */
import { isBadId, orgHref } from "./hrefs.ts";
import { useResource } from "./useResource.ts";
/* `ids` (a hand-picked list) wins over `kind`. */
export function useOrganizations(kind, ids?: string[]) {
const path = ids?.length
? `/organizations?ids=${ids.map(encodeURIComponent).join(",")}`
: kind
? `/organizations?kind=${encodeURIComponent(kind)}`
: "/organizations";
const { data, error, loading } = useResource(path);
return {
organizations: data?.organizations ?? [],
loading,
error,
};
}
/* ── Where an organization's page lives ────────────────────────
The route is hrefs.ts's (orgHref). A list links only the kinds
with a page of their own, and renders no link rather than a dead
one for the rest (NGU itself) or a bad id.
───────────────────────────────────────────────────────────── */
const WITH_PAGE = new Set(["region", "group", "partner"]);
export function orgPath(org) {
return WITH_PAGE.has(org?.kind) && !isBadId(org.id) ? orgHref(org.id, org.kind) : null;
}
/* ── "AK, ID, MT, OR, UT (Salt Lake City area), WA" ────────────
The areas a region covers, annotated where it holds only part
of one. Canada is a band on the map rather than somewhere you'd
list, so it's left out of the sentence.
The note comes straight off the row. The old statesLabel had to
work out whether this region was the primary or the secondary of
a split before it knew which note applied; there's no such thing
any more.
───────────────────────────────────────────────────────────── */
export function areasSentence(areas: any[] = []) {
return areas
.filter((area) => area.area_code !== "CANADA")
.map((area) => (area.note ? `${area.area_code} (${area.note})` : area.area_code))
.sort()
.join(", ");
}
// Alphabetical by name, for sorting organizations.
export const byName = (a, b) => a.name.localeCompare(b.name);
/* Initials for an organization with no logo. "NGU Lynnwood" → "NL",
dropping the org prefix so every card doesn't read "NG". */
export function initialsFor(name = "") {
return name
.replace(/^NGU\s+/i, "")
.split(/\s+/)
.filter(Boolean)
.slice(0, 2)
.map((word) => word[0].toUpperCase())
.join("");
}