Show the remade card names on template detail, matching final review, and say one size fits all.
This commit is contained in:
1 parent
f634a91bb2
commit
54a4871ad1
4 files changed
+256
-8
No files matched your search
@@ -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<typeof templateBodyToReviewData>;
|
||||
|
||||
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<TemplateFacetGroupKey, "coreValues">,
|
||||
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).
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user