Template remove add, read-only, chips open modals

This commit is contained in:
adilallo
2026-04-20 13:14:56 -06:00
parent 45bbbb8a35
commit d3bb8cdd0f
9 changed files with 632 additions and 43 deletions
+97 -13
View File
@@ -21,27 +21,111 @@ function isDocumentSection(
}
/**
* Maps API template `body` (published-rule document shape) to RuleCard category rows.
* Known facet groups that template sections map to. Matches the five modals on
* the custom-rule create flow (`m.create.customRule.*`).
*/
export function templateBodyToCategories(body: unknown): Category[] {
if (!body || typeof body !== "object") return [];
const sections = (body as Record<string, unknown>).sections;
if (!Array.isArray(sections)) return [];
export type TemplateFacetGroupKey =
| "coreValues"
| "communication"
| "membership"
| "decisionApproaches"
| "conflictManagement";
const out: Category[] = [];
/**
* Normalize a section `categoryName` (as it appears in a template's `body`)
* to the custom-rule facet-group key. Returns `null` for unknown categories.
* Keys are matched case- and punctuation-insensitively so variations like
* "Decision making" / "Decision-making" resolve to the same group.
*/
export function templateCategoryToGroupKey(
categoryName: string,
): TemplateFacetGroupKey | null {
const key = categoryName.toLowerCase().replace(/[^a-z]+/g, "");
switch (key) {
case "values":
case "corevalues":
return "coreValues";
case "communication":
case "communications":
return "communication";
case "membership":
case "memberships":
return "membership";
case "decisionmaking":
case "decisionapproaches":
case "decisions":
return "decisionApproaches";
case "conflictmanagement":
case "conflict":
case "conflictresolution":
return "conflictManagement";
default:
return null;
}
}
/**
* Detail for a single chip rendered on a template review — includes the raw
* entry fields plus the facet-group key so a click can open the matching
* read-only modal (chip `label` is used to look up the preset method inside
* the group).
*/
export interface TemplateChipDetail {
chipId: string;
chipLabel: string;
categoryName: string;
groupKey: TemplateFacetGroupKey | null;
body: string;
}
/**
* Maps API template `body` (published-rule document shape) to RuleCard category
* rows **plus** a chipId → detail lookup for wiring chip clicks to the
* read-only detail modal.
*/
export function templateBodyToReviewData(body: unknown): {
categories: Category[];
chipDetailsByChipId: Record<string, TemplateChipDetail>;
} {
const empty = { categories: [] as Category[], chipDetailsByChipId: {} };
if (!body || typeof body !== "object") return empty;
const sections = (body as Record<string, unknown>).sections;
if (!Array.isArray(sections)) return empty;
const categories: Category[] = [];
const chipDetailsByChipId: Record<string, TemplateChipDetail> = {};
for (const raw of sections) {
if (!isDocumentSection(raw)) continue;
const chipOptions: ChipOption[] = raw.entries.map((e, i) => ({
id: `${raw.categoryName}-${i}`,
label: e.title,
state: "unselected",
}));
out.push({
const groupKey = templateCategoryToGroupKey(raw.categoryName);
const chipOptions: ChipOption[] = raw.entries.map((e, i) => {
const chipId = `${raw.categoryName}-${i}`;
chipDetailsByChipId[chipId] = {
chipId,
chipLabel: e.title,
categoryName: raw.categoryName,
groupKey,
body: e.body,
};
return {
id: chipId,
label: e.title,
state: "unselected",
};
});
categories.push({
name: raw.categoryName,
chipOptions,
});
}
return out;
return { categories, chipDetailsByChipId };
}
/**
* Backwards-compatible wrapper kept so existing consumers can still grab just
* the rows when they don't need chip-click wiring.
*/
export function templateBodyToCategories(body: unknown): Category[] {
return templateBodyToReviewData(body).categories;
}
/**