NGU-Web/src/lib/useCommunity.ts
Zaldimmar 38aff34c97 Call local chapters groups, everywhere
The organization kind is 'group', its side table is groups, pages
live at /groups/<id> and the Community page's section is #groups.
Migration 052 converts existing databases: the kind and the tables
whose CHECKs name chapters are rebuilt, the front page count and
request category follow, and the site's own /chapters/ and
#chapters links in records are rewritten. Request files now go in
Requests/Groups on the shared drive.

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

139 lines
4.7 KiB
TypeScript

/* ═══════════════════════════════════════════════════════════════
useCommunity
The group map's view of the organization list (OrgList-Map).
Regions and groups are the same table and the same endpoint;
this hook reshapes them into what a map needs, slices per tile,
counts per tile, groups grouped by region.
Two things live elsewhere:
the tile grid src/data/mapGrid.ts. Where a state sits never
changes, so it isn't worth a round trip.
the fetch src/lib/organizations.ts. One endpoint for
every kind, so a section listing regions and
a section listing partners read alike.
═══════════════════════════════════════════════════════════════ */
import { useMemo } from "react";
import {
areasSentence,
byName,
useOrganizations,
} from "./organizations.ts";
import {
areaForGroup,
buildAreaSlices,
countGroupsByArea,
} from "../data/mapGrid.ts";
export function useCommunity() {
const regionsQuery = useOrganizations("region");
const groupsQuery = useOrganizations("group");
const rawRegions = regionsQuery.organizations;
const rawGroups = groupsQuery.organizations;
return useMemo(() => {
/* ── Regions ───────────────────────────────────────────────
scope and map_note are lifted out of `details` so the rest
of the app doesn't have to know they're kind-specific. */
const regions = rawRegions.map((org) => ({
...org,
scope: org.details?.scope ?? null,
map_note: org.details?.map_note ?? null,
areas: org.details?.areas ?? [],
}));
// Flat, because buildAreaSlices takes it that way.
const regionAreas = regions.flatMap((region) =>
region.areas.map((area) => ({ ...area, region_id: region.id })),
);
/* ── Groups, each tagged with the tile it lights up ──────
The database stores a real address; which square that maps
to is a rendering question, answered here once rather than
at every call site. */
const groups = rawGroups.map((org) => ({
...org,
region_id: org.details?.region_id ?? null,
region_name: org.details?.region_name ?? null,
region_color: org.details?.region_color ?? null,
meets: org.details?.meets ?? null,
started: org.details?.started ?? null,
area_code: areaForGroup(org),
}));
const regionById = Object.fromEntries(regions.map((r) => [r.id, r]));
/* { WA: [{ regionId, name, color, share, edge, note }], ... }
California comes back as two half slices rather than a
primary and a remainder, so the SVG never does arithmetic. */
const slices = buildAreaSlices(regionAreas, regions);
const groupCounts = countGroupsByArea(groups);
const groupsByRegion = new Map();
for (const group of groups) {
if (!group.region_id) continue;
const list = groupsByRegion.get(group.region_id);
if (list) list.push(group);
else groupsByRegion.set(group.region_id, [group]);
}
const groupsIn = (regionId) => groupsByRegion.get(regionId) ?? [];
/* Sorted by name to match how the page has always shown them,
rather than by the sort_order the API returns. */
const domestic = regions.filter((r) => r.scope === "domestic").sort(byName);
const international = regions
.filter((r) => r.scope === "international")
.sort(byName);
const virtual = regions.filter((r) => r.scope === "virtual");
const areasLabelFor = (regionId) =>
areasSentence(regionById[regionId]?.areas ?? []);
const subtextFor = (region) => {
const label = areasLabelFor(region.id);
if (label && region.map_note) return `${label} · ${region.map_note}`;
return label || region.map_note || "";
};
const regionsForArea = (areaCode) =>
(slices[areaCode] ?? [])
.map((slice) => regionById[slice.regionId])
.filter(Boolean);
return {
loading: regionsQuery.loading || groupsQuery.loading,
error: regionsQuery.error ?? groupsQuery.error,
regions,
regionAreas,
regionById,
domestic,
international,
virtual,
groups,
groupsIn,
slices,
groupCounts,
regionsForArea,
areasLabelFor,
subtextFor,
};
}, [
rawRegions,
rawGroups,
regionsQuery.loading,
regionsQuery.error,
groupsQuery.loading,
groupsQuery.error,
]);
}