Files
community-rule/lib/create/finalReviewChipWarnings.ts
T

203 lines
5.7 KiB
TypeScript

import type {
CommunicationMethodDetailEntry,
ConflictManagementDetailEntry,
CreateFlowState,
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 {
if (!saved) return true;
return missingText(
saved.corePrinciple,
saved.logisticsAdmin,
saved.codeOfConduct,
);
}
function membershipMissingWords(
saved: MembershipMethodDetailEntry | undefined,
): boolean {
if (!saved) return true;
return missingText(
saved.eligibility,
saved.joiningProcess,
saved.expectations,
);
}
function decisionMissingWords(
saved: DecisionApproachDetailEntry | undefined,
): boolean {
if (!saved) return true;
return missingText(
saved.corePrinciple,
saved.stepByStepInstructions,
saved.objectionsDeadlocks,
);
}
function conflictMissingWords(
saved: ConflictManagementDetailEntry | undefined,
): boolean {
if (!saved) return true;
return missingText(
saved.corePrinciple,
saved.processProtocol,
saved.restorationFallbacks,
);
}
function methodUsesCustomFieldBlocks(
state: CreateFlowState,
overrideKey: string,
): boolean {
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 "communication":
return communicationMissingWords(
state.communicationMethodDetailsById?.[overrideKey],
);
case "membership":
return membershipMissingWords(
state.membershipMethodDetailsById?.[overrideKey],
);
case "decisionApproaches":
return decisionMissingWords(
state.decisionApproachDetailsById?.[overrideKey],
);
case "conflictManagement":
return conflictMissingWords(
state.conflictManagementDetailsById?.[overrideKey],
);
}
}
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;
}