Keep every picked card on the finished Rule, and wait for its words before Continue.

This commit is contained in:
adilallo committed 2026-11-22 17:56:38 -07:00
1 parent a846169960
commit a2815ae05f
16 files changed
+417 -114

No files matched your search

+13 -21
View File
@@ -8,11 +8,10 @@ import type {
import type { CommunityRuleSection } from "../../app/components/type/CommunityRule/CommunityRule.types";
import { resolveMethodPresetIdFromLabel } from "./buildFinalReviewCategories";
import {
communicationPresetFor,
conflictManagementPresetFor,
decisionApproachPresetFor,
membershipPresetFor,
mergeCoreValueDetailWithPresets,
emptyCommunicationDetails,
emptyConflictManagementDetails,
emptyDecisionApproachDetails,
emptyMembershipDetails,
publishedMethodDisplayLabel,
} from "./finalReviewChipPresets";
import { isDocumentEntry } from "./documentEntryGuards";
@@ -61,12 +60,12 @@ export function buildCoreValuesForDocument(state: CreateFlowState): Array<{
return snap
.filter((r) => selected.has(r.id))
.map((r) => {
const merged = mergeCoreValueDetailWithPresets(r.id, r.label, details[r.id]);
const saved = details[r.id];
return {
chipId: r.id,
label: r.label,
meaning: merged.meaning,
signals: merged.signals,
meaning: typeof saved?.meaning === "string" ? saved.meaning : "",
signals: typeof saved?.signals === "string" ? saved.signals : "",
};
});
}
@@ -278,10 +277,8 @@ function publishedRowSupportText(
/**
* Merge `selected*MethodIds` with any saved `{group}MethodDetailsById`
* overrides authored on the final-review screen. Preset defaults from the
* shipped `messages/en/create/customRule/*.json` seed any sub-fields the
* user didn't edit so consumers of `document.methodSelections` always see
* a complete payload per method.
* overrides authored on the final-review screen. Blank parts stay blank —
* library presets are not filled in for text the pen holder never wrote.
*/
export function buildMethodSelectionsForDocument(
state: CreateFlowState,
@@ -297,7 +294,6 @@ export function buildMethodSelectionsForDocument(
);
if (commIds.length > 0) {
out.communication = commIds.map((id) => {
const preset = communicationPresetFor(id);
const override = state.communicationMethodDetailsById?.[id];
const supportText = publishedRowSupportText(
id,
@@ -310,7 +306,7 @@ export function buildMethodSelectionsForDocument(
id,
state.customMethodCardMetaById,
),
sections: override ? { ...preset, ...override } : preset,
sections: override ?? emptyCommunicationDetails(),
...(supportText ? { supportText } : {}),
};
});
@@ -322,7 +318,6 @@ export function buildMethodSelectionsForDocument(
);
if (memIds.length > 0) {
out.membership = memIds.map((id) => {
const preset = membershipPresetFor(id);
const override = state.membershipMethodDetailsById?.[id];
const supportText = publishedRowSupportText(
id,
@@ -335,7 +330,7 @@ export function buildMethodSelectionsForDocument(
id,
state.customMethodCardMetaById,
),
sections: override ? { ...preset, ...override } : preset,
sections: override ?? emptyMembershipDetails(),
...(supportText ? { supportText } : {}),
};
});
@@ -347,13 +342,11 @@ export function buildMethodSelectionsForDocument(
);
if (daIds.length > 0) {
out.decisionApproaches = daIds.map((id) => {
const preset = decisionApproachPresetFor(id);
const override = state.decisionApproachDetailsById?.[id];
const supportText = publishedRowSupportText(
id,
state.customMethodCardMetaById,
);
const merged = override ? { ...preset, ...override } : preset;
return {
id,
label: publishedMethodDisplayLabel(
@@ -362,7 +355,7 @@ export function buildMethodSelectionsForDocument(
state.customMethodCardMetaById,
),
sections: withoutUnpublishedDecisionConsensus(
{ ...merged },
{ ...(override ?? emptyDecisionApproachDetails()) },
state.customMethodCardFieldBlocksById?.[id],
) as DecisionApproachDetailEntry,
...(supportText ? { supportText } : {}),
@@ -376,7 +369,6 @@ export function buildMethodSelectionsForDocument(
);
if (cmIds.length > 0) {
out.conflictManagement = cmIds.map((id) => {
const preset = conflictManagementPresetFor(id);
const override = state.conflictManagementDetailsById?.[id];
const supportText = publishedRowSupportText(
id,
@@ -389,7 +381,7 @@ export function buildMethodSelectionsForDocument(
id,
state.customMethodCardMetaById,
),
sections: override ? { ...preset, ...override } : preset,
sections: override ?? emptyConflictManagementDetails(),
...(supportText ? { supportText } : {}),
};
});
+14 -5
View File
@@ -15,9 +15,8 @@ import type {
/**
* Catalog copy for method/value sections. Generate draft inserts these into
* empty creation-modal fields. Publish still merges them onto saved overrides
* so every method carries a complete section payload when the author only
* edited a subset of fields.
* empty creation-modal fields. Publish keeps blank parts blank — it does not
* fill library text the pen holder never wrote.
*/
type CustomRuleMethodRow = {
id: string;
@@ -145,6 +144,16 @@ export function emptyDecisionApproachTextFields(
};
}
export function emptyDecisionApproachDetails(): DecisionApproachDetailEntry {
return {
corePrinciple: "",
applicableScope: [],
selectedApplicableScope: [],
stepByStepInstructions: "",
objectionsDeadlocks: "",
};
}
export function conflictManagementPresetFor(
id: string,
): ConflictManagementDetailEntry {
@@ -233,8 +242,8 @@ export function coreValuePresetForLabel(label: string): CoreValueDetailEntry {
}
/**
* Published / display copy: saved draft wins when non-empty; otherwise preset
* by chip id (numeric presets), then by label match in `coreValues.json`.
* Generate draft and legacy placeholder replacement: saved copy wins when
* non-empty; otherwise preset by chip id, then by label match.
*/
export function mergeCoreValueDetailWithPresets(
chipId: string,
+124 -12
View File
@@ -5,12 +5,34 @@ import type {
DecisionApproachDetailEntry,
MembershipMethodDetailEntry,
} from "../../app/(app)/create/types";
import {
buildFinalReviewCategoryRowsDetailed,
type FinalReviewCategoryNames,
} from "./buildFinalReviewCategories";
import type { CustomMethodCardFieldBlock } from "./customMethodCardFieldBlocks";
import { facetBlockHasContent } from "./methodCardWizardPrefill";
import type { TemplateFacetGroupKey } from "./templateReviewMapping";
import { usesWizardFieldBlocksModalBody } from "./usesWizardFieldBlocksModalBody";
const LABEL_NAMES: FinalReviewCategoryNames = {
values: "values",
communication: "communication",
membership: "membership",
decisions: "decisions",
conflict: "conflict",
};
function missingText(...values: readonly (string | undefined)[]): boolean {
return values.some((value) => (value ?? "").trim().length === 0);
}
function customFieldBlocksHaveWords(
blocks: readonly CustomMethodCardFieldBlock[] | undefined,
): boolean {
if (!blocks || blocks.length === 0) return false;
return blocks.every(facetBlockHasContent);
}
function communicationMissingWords(
saved: CommunicationMethodDetailEntry | undefined,
): boolean {
@@ -55,21 +77,30 @@ function conflictMissingWords(
);
}
/**
* A review chip shows a warning when its value is missing meaning or signals,
* or a card is missing its words.
*/
export function finalReviewChipHasWarning(
function methodUsesCustomFieldBlocks(
state: CreateFlowState,
groupKey: TemplateFacetGroupKey | null,
overrideKey: string | null,
overrideKey: string,
): boolean {
if (!groupKey || !overrideKey) return true;
return usesWizardFieldBlocksModalBody({
methodId: overrideKey,
meta: state.customMethodCardMetaById,
fieldBlocksById: state.customMethodCardFieldBlocksById,
modalEditUnlocked: true,
draftFieldBlocks: null,
});
}
function methodCardMissingWords(
state: CreateFlowState,
groupKey: Exclude<TemplateFacetGroupKey, "coreValues">,
overrideKey: string,
): boolean {
if (methodUsesCustomFieldBlocks(state, overrideKey)) {
return !customFieldBlocksHaveWords(
state.customMethodCardFieldBlocksById?.[overrideKey],
);
}
switch (groupKey) {
case "coreValues": {
const saved = state.coreValueDetailsByChipId?.[overrideKey];
return missingText(saved?.meaning, saved?.signals);
}
case "communication":
return communicationMissingWords(
state.communicationMethodDetailsById?.[overrideKey],
@@ -88,3 +119,84 @@ export function finalReviewChipHasWarning(
);
}
}
function valueHasMeaning(
state: CreateFlowState,
overrideKey: string,
): boolean {
const saved = state.coreValueDetailsByChipId?.[overrideKey];
if ((saved?.meaning ?? "").trim().length > 0) return true;
const blocks = state.customMethodCardFieldBlocksById?.[overrideKey];
if (!blocks || blocks.length === 0) return false;
return blocks.some(
(block) =>
block.kind === "text" &&
block.id !== "facet-signals" &&
facetBlockHasContent(block),
);
}
function valueHasSignals(
state: CreateFlowState,
overrideKey: string,
): boolean {
const saved = state.coreValueDetailsByChipId?.[overrideKey];
if ((saved?.signals ?? "").trim().length > 0) return true;
const blocks = state.customMethodCardFieldBlocksById?.[overrideKey];
if (!blocks || blocks.length === 0) return false;
const signalsBlock = blocks.find(
(block) => block.kind === "text" && block.id === "facet-signals",
);
if (!signalsBlock) return true;
return facetBlockHasContent(signalsBlock);
}
/**
* A review chip shows a warning when its value is missing meaning or signals,
* or a card is missing its words. Custom field text counts as those words.
*/
export function finalReviewChipHasWarning(
state: CreateFlowState,
groupKey: TemplateFacetGroupKey | null,
overrideKey: string | null,
): boolean {
if (!groupKey || !overrideKey) return true;
if (groupKey === "coreValues") {
return !valueHasMeaning(state, overrideKey) || !valueHasSignals(state, overrideKey);
}
return methodCardMissingWords(state, groupKey, overrideKey);
}
/**
* Continue waits for every picked card's words and every picked value's
* meaning. Missing signals stay a warning only.
*/
export function finalReviewChipBlocksContinue(
state: CreateFlowState,
groupKey: TemplateFacetGroupKey | null,
overrideKey: string | null,
): boolean {
if (!groupKey || !overrideKey) return false;
if (groupKey === "coreValues") {
return !valueHasMeaning(state, overrideKey);
}
return methodCardMissingWords(state, groupKey, overrideKey);
}
/** Chip labels that still need words before Continue. */
export function finalReviewMissingWordsLabels(
state: CreateFlowState,
): string[] {
const rows = buildFinalReviewCategoryRowsDetailed(state, LABEL_NAMES);
const labels: string[] = [];
for (const row of rows) {
for (const entry of row.entries) {
if (
finalReviewChipBlocksContinue(state, entry.groupKey, entry.overrideKey)
) {
labels.push(entry.label);
}
}
}
return labels;
}
+1 -1
View File
@@ -233,7 +233,7 @@ export function facetDetailsToWizardFieldBlocks(
return mapFacetPrefillToWizardFieldBlocks(prefill);
}
function facetBlockHasContent(block: CustomMethodCardFieldBlock): boolean {
export function facetBlockHasContent(block: CustomMethodCardFieldBlock): boolean {
switch (block.kind) {
case "text":
return hasTrimmedText(block.placeholderText);
@@ -4,13 +4,6 @@ import type { PublishedMethodSelections } from "./buildPublishPayload";
import { parseDocumentSectionsForDisplay } from "./buildPublishPayload";
import { resolveMethodPresetIdFromLabel } from "./buildFinalReviewCategories";
import { resolveCoreValueChipIdFromLabel } from "./finalReviewChipPresets";
import {
communicationPresetFor,
conflictManagementPresetFor,
decisionApproachPresetFor,
membershipPresetFor,
mergeCoreValueDetailWithPresets,
} from "./finalReviewChipPresets";
import { templateCategoryToGroupKey } from "./templateReviewMapping";
import type { TemplateFacetGroupKey } from "./templateReviewMapping";
import { RULE_SECTION_CATEGORY } from "./ruleSectionsFromMethodSelections";
@@ -110,26 +103,9 @@ function sectionsRecordFromBlocks(
return out;
}
function presetForMethod(
groupKey: Exclude<TemplateFacetGroupKey, "coreValues">,
id: string,
): Record<string, unknown> {
switch (groupKey) {
case "communication":
return { ...communicationPresetFor(id) } as Record<string, unknown>;
case "membership":
return { ...membershipPresetFor(id) } as Record<string, unknown>;
case "decisionApproaches":
return { ...decisionApproachPresetFor(id) } as Record<string, unknown>;
case "conflictManagement":
return { ...conflictManagementPresetFor(id) } as Record<string, unknown>;
}
}
function sectionsRecordFromEntry(
entry: CommunityRuleSection["entries"][number],
groupKey: Exclude<TemplateFacetGroupKey, "coreValues">,
presetId: string,
): Record<string, unknown> {
const labelByKey = LABELS_BY_GROUP[groupKey];
const consensusKey =
@@ -140,16 +116,16 @@ function sectionsRecordFromEntry(
consensusLevelKey: consensusKey,
});
if (Object.keys(fromBlocks).length > 0) {
return { ...presetForMethod(groupKey, presetId), ...fromBlocks };
return fromBlocks;
}
}
const body = (entry.body ?? "").trim();
if (body.length === 0) {
return presetForMethod(groupKey, presetId);
return {};
}
return { ...presetForMethod(groupKey, presetId), corePrinciple: body };
return { corePrinciple: body };
}
function coreValuesFromValuesSection(
@@ -169,18 +145,14 @@ function coreValuesFromValuesSection(
const parts = body.length > 0 ? body.split(/\n\n+/) : [];
const meaning = (parts[0] ?? "").trim();
const signals = parts.slice(1).join("\n\n").trim();
const merged = mergeCoreValueDetailWithPresets("", label, {
meaning,
signals,
});
const chipId =
resolveCoreValueChipIdFromLabel(label) ??
`hydrated-${label.toLowerCase()}`;
out.push({
chipId,
label,
meaning: merged.meaning,
signals: merged.signals,
meaning,
signals,
});
}
@@ -213,7 +185,7 @@ function methodSelectionsFromDisplaySections(
rows.push({
id,
label,
sections: sectionsRecordFromEntry(entry, groupKey, id),
sections: sectionsRecordFromEntry(entry, groupKey),
});
}
if (rows.length > 0) {
@@ -80,7 +80,6 @@ const CM_LABELS: Record<string, string> = {
function needsPlaceholderPresetEnrichment(entry: CommunityRuleEntry): boolean {
if (entry.blocks && entry.blocks.length > 0) return false;
const b = (entry.body ?? "").trim();
if (b.length === 0) return true;
return b === TEMPLATE_COMPOSITION_SUGGESTED_BODY;
}
@@ -196,15 +195,12 @@ function sectionFromStoredCoreValues(
for (const row of raw) {
if (!row || typeof row !== "object") continue;
const o = row as Record<string, unknown>;
const chipId = typeof o.chipId === "string" ? o.chipId : "";
const label = nonEmptyTrimmed(o.label);
if (!label) continue;
const merged = mergeCoreValueDetailWithPresets(chipId, label, {
meaning: typeof o.meaning === "string" ? o.meaning : "",
signals: typeof o.signals === "string" ? o.signals : "",
});
const meaning = (merged.meaning ?? "").trim();
const signals = (merged.signals ?? "").trim();
const meaning =
typeof o.meaning === "string" ? o.meaning.trim() : "";
const signals =
typeof o.signals === "string" ? o.signals.trim() : "";
const bodyParts: string[] = [];
if (meaning.length > 0) bodyParts.push(meaning);
if (signals.length > 0) bodyParts.push(signals);
@@ -221,8 +221,7 @@ export function communityRuleEntryFromMethodChip(
const blocks = [...presetBlocks, ...wizardBlocks];
const description = nonEmptyTrimmed(options?.supportText);
if (blocks.length === 0) {
if (!description) return null;
return { title, body: description };
return { title, body: description ?? "" };
}
return {
title,
@@ -387,8 +386,8 @@ export function sectionFromConflict(
}
/**
* Swap template `sections` method rows for fully-resolved entries built from
* `methodSelections` (preset + overrides).
* Swap template `sections` method rows for entries built from
* `methodSelections`.
*/
export function replaceMethodSectionsWithMethodSelections(
sections: CommunityRuleSection[],