From 54a4871ad105f623b55493c73bfe941a5d9be588 Mon Sep 17 00:00:00 2001 From: adilallo <39313955+adilallo@users.noreply.github.com> Date: Sun, 22 Nov 2026 18:10:44 -0700 Subject: [PATCH] Show the remade card names on template detail, matching final review, and say one size fits all. --- .../TemplateReviewCard/TemplateReviewCard.tsx | 12 +- lib/create/templateReviewMapping.ts | 135 +++++++++++++++++- messages/en/create/templateReview.json | 2 +- tests/unit/templateReviewMapping.test.ts | 115 +++++++++++++++ 4 files changed, 256 insertions(+), 8 deletions(-) diff --git a/app/components/cards/TemplateReviewCard/TemplateReviewCard.tsx b/app/components/cards/TemplateReviewCard/TemplateReviewCard.tsx index 63871e9..78390f6 100644 --- a/app/components/cards/TemplateReviewCard/TemplateReviewCard.tsx +++ b/app/components/cards/TemplateReviewCard/TemplateReviewCard.tsx @@ -7,7 +7,7 @@ import type { Category, RuleProps } from "../Rule"; import { getAssetPath } from "../../../../lib/assetUtils"; import type { RuleTemplateDto } from "../../../../lib/create/fetchTemplates"; import { - templateBodyToReviewData, + templateReviewDataForSlug, templateSummaryFromBody, } from "../../../../lib/create/templateReviewMapping"; import { @@ -26,9 +26,9 @@ export interface TemplateReviewCardProps { /** * Expanded Rule for template review: surfaces + icon from Figma catalog (21764-16435); - * tag rows from API `body`. Chip clicks open a read-only detail modal per - * facet group (values / communication / membership / decision-making / conflict - * management) so reviewers can see what each chip means without editing. + * tag rows from the remade catalog chips. Chip clicks open a read-only detail + * modal per facet group (values / communication / membership / decision-making / + * conflict management) so reviewers can see what each chip means without editing. */ export function TemplateReviewCard({ template, @@ -38,8 +38,8 @@ export function TemplateReviewCard({ const catalog = getGovernanceTemplateCatalogEntry(template.slug); const pres = catalog ?? TEMPLATE_GRID_FALLBACK_PRESENTATION; const { categories: rawCategories, chipDetailsByChipId } = useMemo( - () => templateBodyToReviewData(template.body), - [template.body], + () => templateReviewDataForSlug(template.slug, template.body), + [template.slug, template.body], ); const summary = templateSummaryFromBody(template.description, template.body); diff --git a/lib/create/templateReviewMapping.ts b/lib/create/templateReviewMapping.ts index 8448606..c471b45 100644 --- a/lib/create/templateReviewMapping.ts +++ b/lib/create/templateReviewMapping.ts @@ -1,9 +1,14 @@ import type { Category } from "../../app/components/cards/Rule"; import type { ChipOption } from "../../app/components/controls/MultiSelect/MultiSelect.types"; import type { CommunityRuleSection } from "../../app/components/type/CommunityRule/CommunityRule.types"; +import coreValuesMessages from "../../messages/en/create/customRule/coreValues.json"; import type { TemplateFacetGroupKey } from "./customRuleFacets"; -import { templateCategoryToFacetGroupKey } from "./customRuleFacets"; +import { + readMethodPresetsForFacetGroup, + templateCategoryToFacetGroupKey, +} from "./customRuleFacets"; import { isDocumentEntry } from "./documentEntryGuards"; +import { templateChipsForSlug, type TemplateChipSet } from "./templatePath"; export type { TemplateFacetGroupKey } from "./customRuleFacets"; @@ -91,6 +96,134 @@ export function templateBodyToCategories(body: unknown): Category[] { return templateBodyToReviewData(body).categories; } +type ReviewData = ReturnType; + +const CHIP_REVIEW_GROUPS: readonly { + groupKey: TemplateFacetGroupKey; + categoryName: string; + idsOf: (_chips: TemplateChipSet) => readonly string[]; +}[] = [ + { + groupKey: "coreValues", + categoryName: "Values", + idsOf: (chips) => chips.coreValues, + }, + { + groupKey: "communication", + categoryName: "Communication", + idsOf: (chips) => chips.communication, + }, + { + groupKey: "membership", + categoryName: "Membership", + idsOf: (chips) => chips.membership, + }, + { + groupKey: "decisionApproaches", + categoryName: "Decision-making", + idsOf: (chips) => chips.decisionApproaches, + }, + { + groupKey: "conflictManagement", + categoryName: "Conflict management", + idsOf: (chips) => chips.conflictManagement, + }, +]; + +function coreValueLabelById(id: string): string | null { + const values = (coreValuesMessages as { values?: unknown }).values; + if (!Array.isArray(values)) return null; + for (const raw of values) { + if (!raw || typeof raw !== "object") continue; + const row = raw as { id?: unknown; label?: unknown }; + if (row.id === id && typeof row.label === "string" && row.label.trim()) { + return row.label; + } + } + return null; +} + +/** Same order as `coreValuesChipsSnapshot` / final review: catalog list, then leftovers. */ +function orderedCoreValueIds(ids: readonly string[]): string[] { + const wanted = new Set(ids); + const out: string[] = []; + const values = (coreValuesMessages as { values?: unknown }).values; + if (Array.isArray(values)) { + for (const raw of values) { + if (!raw || typeof raw !== "object") continue; + const id = (raw as { id?: unknown }).id; + if (typeof id === "string" && wanted.delete(id)) out.push(id); + } + } + for (const id of ids) { + if (wanted.has(id)) out.push(id); + } + return out; +} + +function methodLabelById( + groupKey: Exclude, + id: string, +): string | null { + const row = readMethodPresetsForFacetGroup(groupKey).find( + (method) => method.id === id, + ); + return row?.label ?? null; +} + +function templateChipsToReviewData(slug: string): ReviewData | null { + const chips = templateChipsForSlug(slug); + if (!chips) return null; + + const categories: Category[] = []; + const chipDetailsByChipId: ReviewData["chipDetailsByChipId"] = {}; + + for (const group of CHIP_REVIEW_GROUPS) { + const chipOptions: ChipOption[] = []; + const ids = + group.groupKey === "coreValues" + ? orderedCoreValueIds(group.idsOf(chips)) + : group.idsOf(chips); + for (const id of ids) { + const chipLabel = + group.groupKey === "coreValues" + ? coreValueLabelById(id) + : methodLabelById(group.groupKey, id); + if (!chipLabel) continue; + chipDetailsByChipId[id] = { + chipId: id, + chipLabel, + categoryName: group.categoryName, + groupKey: group.groupKey, + body: "", + }; + chipOptions.push({ + id, + label: chipLabel, + state: "unselected", + }); + } + if (chipOptions.length === 0) continue; + categories.push({ + name: group.categoryName, + chipOptions, + }); + } + + return { categories, chipDetailsByChipId }; +} + +/** + * Detail-page chips. Prefer the remade catalog ids in templateChips.json so + * the list matches final review; fall back to API body titles for unknown slugs. + */ +export function templateReviewDataForSlug( + slug: string, + body: unknown, +): ReviewData { + return templateChipsToReviewData(slug) ?? templateBodyToReviewData(body); +} + /** * Summary line under tag rows: prefer API description; else first entry bodies (short). */ diff --git a/messages/en/create/templateReview.json b/messages/en/create/templateReview.json index 1b729f3..94d5448 100644 --- a/messages/en/create/templateReview.json +++ b/messages/en/create/templateReview.json @@ -1,7 +1,7 @@ { "intro": { "title": "CommunityRule Template", - "description": "These templates can get you started with a proven approach to governance and decision-making. As useful as these templates are, they are not one size fit all solutions though. We recommend that you use them as a starting place for your community to adapt and customize according to your group's specific needs." + "description": "These templates can get you started with a proven approach to governance and decision-making. As useful as these templates are, they are not one size fits all solutions though. We recommend that you use them as a starting place for your community to adapt and customize according to your group's specific needs." }, "footer": { "useWithoutChanges": "Use without changes", diff --git a/tests/unit/templateReviewMapping.test.ts b/tests/unit/templateReviewMapping.test.ts index a0a0f66..1987171 100644 --- a/tests/unit/templateReviewMapping.test.ts +++ b/tests/unit/templateReviewMapping.test.ts @@ -1,10 +1,15 @@ import { describe, expect, it } from "vitest"; +import { buildAppliedTemplateCreateFlowState } from "../../lib/create/applyTemplatePrefill"; +import { buildFinalReviewCategoriesFromState } from "../../lib/create/buildFinalReviewCategories"; import { templateBodyToCategories, templateBodyToReviewData, templateCategoryToGroupKey, + templateReviewDataForSlug, templateSummaryFromBody, } from "../../lib/create/templateReviewMapping"; +import templateChipsFile from "../../data/templates/templateChips.json"; +import templateReviewMessages from "../../messages/en/create/templateReview.json"; describe("templateReviewMapping", () => { it("maps body sections to Rule categories", () => { @@ -117,3 +122,113 @@ describe("templateReviewMapping", () => { }); }); }); + +const FINAL_REVIEW_NAMES = { + values: "Values", + communication: "Communication", + membership: "Membership", + decisions: "Decision-making", + conflict: "Conflict management", +}; + +const STALE_CONSENSUS_BODY = { + sections: [ + { + categoryName: "Values", + entries: [{ title: "Community Care", body: "" }], + }, + { + categoryName: "Communication", + entries: [ + { title: "In-Person Meetings", body: "" }, + { title: "Loomio", body: "" }, + ], + }, + { + categoryName: "Membership", + entries: [{ title: "Peer Sponsorship", body: "" }], + }, + { + categoryName: "Decision-making", + entries: [ + { title: "Consensus or Vote-Based Approval", body: "" }, + { title: "Consensus Decision-Making", body: "" }, + ], + }, + { + categoryName: "Conflict management", + entries: [{ title: "Facilitated Negotiation", body: "" }], + }, + ], +}; + +describe("templateReviewDataForSlug", () => { + it("lists remade catalog names for consensus, not API body titles", () => { + const { categories } = templateReviewDataForSlug( + "consensus", + STALE_CONSENSUS_BODY, + ); + const labels = categories.flatMap((row) => + row.chipOptions.map((chip) => chip.label), + ); + expect(labels).toContain("How we run meetings"); + expect(labels).toContain("In-person meetings"); + expect(labels).toContain("Conflict ladder"); + expect(labels).toContain("Community care"); + expect(labels).toContain("Decision speeds"); + expect(labels).not.toContain("In-Person Meetings"); + expect(labels).not.toContain("Community Care"); + expect(labels).not.toContain("Peer Sponsorship"); + expect(labels).not.toContain("Facilitated Negotiation"); + }); + + it("matches Use without changes final-review labels", () => { + const slugs = Object.keys( + (templateChipsFile as { templates: Record }).templates, + ); + for (const slug of slugs) { + const detail = templateReviewDataForSlug(slug, STALE_CONSENSUS_BODY); + const seeded = buildAppliedTemplateCreateFlowState({ + slug, + body: STALE_CONSENSUS_BODY, + mode: "useWithoutChanges", + }); + const review = buildFinalReviewCategoriesFromState( + seeded, + FINAL_REVIEW_NAMES, + ); + const detailByName = new Map( + detail.categories.map((row) => [ + row.name, + row.chipOptions.map((chip) => chip.label), + ]), + ); + for (const row of review) { + if (row.chips.length === 0) continue; + expect(detailByName.get(row.name), slug).toEqual(row.chips); + } + } + }); + + it("falls back to body titles when the slug is not in the catalog", () => { + const cats = templateReviewDataForSlug("unknown-template", { + sections: [ + { + categoryName: "Values", + entries: [{ title: "Solidarity", body: "Long body" }], + }, + ], + }).categories; + expect(cats[0].chipOptions.map((chip) => chip.label)).toEqual([ + "Solidarity", + ]); + }); +}); + +describe("template review intro", () => { + it("reads one size fits all", () => { + expect(templateReviewMessages.intro.description).toContain( + "one size fits all", + ); + }); +});