Skip setup questions on the template path and collect name, photo, and description without recommendations.
Customize opens a dialog then walks the template decks; Use without changes lands on final review with Has Name Field. Template cards stay first, the engine is not called, and the catalog no longer shows a recommended tag.
This commit is contained in:
1 parent
53d236d0d5
commit
6498d52541
37 files changed
+1450
-733
No files matched your search
@@ -3,10 +3,19 @@ import type {
|
||||
CreateFlowState,
|
||||
} from "../../app/(app)/create/types";
|
||||
import coreValuesMessages from "../../messages/en/create/customRule/coreValues.json";
|
||||
import templateChipsFile from "../../data/templates/templateChips.json";
|
||||
import { assignTemplateMethodSlugsToPrefill } from "./customRuleFacets";
|
||||
import { methodSlugFromTitle } from "./methodSlugFromTitle";
|
||||
import type { DeckId } from "./deckTypes";
|
||||
import {
|
||||
communicationPresetFor,
|
||||
conflictManagementPresetFor,
|
||||
coreValuePresetFor,
|
||||
decisionApproachPresetFor,
|
||||
membershipPresetFor,
|
||||
} from "./finalReviewChipPresets";
|
||||
import {
|
||||
templateChipsForSlug,
|
||||
type TemplateChipSet,
|
||||
} from "./templatePath";
|
||||
|
||||
type TemplateEntry = { title: unknown };
|
||||
type TemplateSection = { categoryName: unknown; entries: unknown };
|
||||
@@ -101,13 +110,8 @@ function buildCoreValuePrefill(
|
||||
*
|
||||
* Returns an empty object for malformed bodies (no sections array).
|
||||
*/
|
||||
type TemplateChipSet = Record<DeckId | "coreValues", string[]>;
|
||||
|
||||
function prefillFromTemplateChips(slug: string): Partial<CreateFlowState> | null {
|
||||
const templates = (
|
||||
templateChipsFile as { templates: Record<string, TemplateChipSet> }
|
||||
).templates;
|
||||
const chips = templates[slug];
|
||||
const chips: TemplateChipSet | null = templateChipsForSlug(slug);
|
||||
if (!chips) return null;
|
||||
const selected = new Set(chips.coreValues);
|
||||
return {
|
||||
@@ -173,3 +177,49 @@ export function buildCoreValuesPrefillFromTemplateBody(
|
||||
coreValuesChipsSnapshot: full.coreValuesChipsSnapshot,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Catalog copy for each prefilled chip, so a template copy keeps the card
|
||||
* text instead of opening empty Generate-draft fields.
|
||||
*/
|
||||
export function templatePresetDetailsFromPrefill(
|
||||
prefill: Partial<CreateFlowState>,
|
||||
): Partial<CreateFlowState> {
|
||||
const communicationMethodDetailsById: NonNullable<
|
||||
CreateFlowState["communicationMethodDetailsById"]
|
||||
> = {};
|
||||
for (const id of prefill.selectedCommunicationMethodIds ?? []) {
|
||||
communicationMethodDetailsById[id] = communicationPresetFor(id);
|
||||
}
|
||||
const membershipMethodDetailsById: NonNullable<
|
||||
CreateFlowState["membershipMethodDetailsById"]
|
||||
> = {};
|
||||
for (const id of prefill.selectedMembershipMethodIds ?? []) {
|
||||
membershipMethodDetailsById[id] = membershipPresetFor(id);
|
||||
}
|
||||
const decisionApproachDetailsById: NonNullable<
|
||||
CreateFlowState["decisionApproachDetailsById"]
|
||||
> = {};
|
||||
for (const id of prefill.selectedDecisionApproachIds ?? []) {
|
||||
decisionApproachDetailsById[id] = decisionApproachPresetFor(id);
|
||||
}
|
||||
const conflictManagementDetailsById: NonNullable<
|
||||
CreateFlowState["conflictManagementDetailsById"]
|
||||
> = {};
|
||||
for (const id of prefill.selectedConflictManagementIds ?? []) {
|
||||
conflictManagementDetailsById[id] = conflictManagementPresetFor(id);
|
||||
}
|
||||
const coreValueDetailsByChipId: NonNullable<
|
||||
CreateFlowState["coreValueDetailsByChipId"]
|
||||
> = {};
|
||||
for (const id of prefill.selectedCoreValueIds ?? []) {
|
||||
coreValueDetailsByChipId[id] = coreValuePresetFor(id);
|
||||
}
|
||||
return {
|
||||
communicationMethodDetailsById,
|
||||
membershipMethodDetailsById,
|
||||
decisionApproachDetailsById,
|
||||
conflictManagementDetailsById,
|
||||
coreValueDetailsByChipId,
|
||||
};
|
||||
}
|
||||
@@ -15,18 +15,8 @@ export type RuleTemplateDto = {
|
||||
|
||||
type TemplatesResponse = {
|
||||
templates?: RuleTemplateDto[];
|
||||
scores?: Record<string, TemplateFacetScoreDto>;
|
||||
};
|
||||
|
||||
function parseScoresPayload(
|
||||
raw: unknown,
|
||||
): Record<string, TemplateFacetScoreDto> {
|
||||
if (raw && typeof raw === "object" && !Array.isArray(raw)) {
|
||||
return raw as Record<string, TemplateFacetScoreDto>;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
async function getTemplatesJson(
|
||||
queryString: string,
|
||||
signal: AbortSignal | undefined,
|
||||
@@ -51,17 +41,6 @@ async function getTemplatesJson(
|
||||
}
|
||||
}
|
||||
|
||||
/** Matches `listRankedRuleTemplatesFromDb` / GET `/api/templates` with facet params. */
|
||||
export type TemplateFacetScoreDto = {
|
||||
score: number;
|
||||
matchedFacets: string[];
|
||||
};
|
||||
|
||||
export type RankedTemplatesFetchResult = {
|
||||
templates: RuleTemplateDto[];
|
||||
scores: Record<string, TemplateFacetScoreDto>;
|
||||
};
|
||||
|
||||
export type FetchTemplatesOptions = {
|
||||
signal?: AbortSignal;
|
||||
};
|
||||
@@ -100,37 +79,6 @@ export async function fetchTemplates(
|
||||
return Array.isArray(data.templates) ? data.templates : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Facet-ranked list + per-template scores (CR-88). Query must be non-empty
|
||||
* `facet.size=…&…` from {@link buildFacetQueryString}.
|
||||
*/
|
||||
export async function fetchRankedTemplatesByFacets(options: {
|
||||
facetQuery: string;
|
||||
signal?: AbortSignal;
|
||||
}): Promise<RankedTemplatesFetchResult | { error: string }> {
|
||||
if (options.facetQuery.length === 0) {
|
||||
return { error: "Could not load templates" };
|
||||
}
|
||||
const got = await getTemplatesJson(options.facetQuery, options.signal);
|
||||
if (got.ok === false) {
|
||||
if (got.error === "aborted") {
|
||||
throw new DOMException("Aborted", "AbortError");
|
||||
}
|
||||
return { error: "Could not load templates" };
|
||||
}
|
||||
const { data, statusOk } = got;
|
||||
if (!statusOk) {
|
||||
return {
|
||||
error:
|
||||
typeof data.error === "string"
|
||||
? data.error
|
||||
: "Could not load templates",
|
||||
};
|
||||
}
|
||||
const templates = Array.isArray(data.templates) ? data.templates : [];
|
||||
return { templates, scores: parseScoresPayload(data.scores) };
|
||||
}
|
||||
|
||||
export type TemplateDetailDto = {
|
||||
template: RuleTemplateDto;
|
||||
methods: Array<{ section: string; slug: string }>;
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
import type { CreateFlowState } from "../../app/(app)/create/types";
|
||||
import type { DeckId } from "./deckTypes";
|
||||
import templateChipsFile from "../../data/templates/templateChips.json";
|
||||
|
||||
export type TemplatePathMode = "customize" | "useWithoutChanges";
|
||||
|
||||
export type TemplatePath = {
|
||||
slug: string;
|
||||
mode: TemplatePathMode;
|
||||
};
|
||||
|
||||
export type TemplateChipSet = Record<DeckId | "coreValues", string[]>;
|
||||
|
||||
/**
|
||||
* Persistent template-path pin. Kept for the rest of the wizard so decks skip
|
||||
* the engine, final review shows identity fields, and Back knows whether to
|
||||
* return to the template or to the conflict deck.
|
||||
*/
|
||||
export function templatePathFromState(
|
||||
state: Pick<CreateFlowState, "pendingTemplateAction">,
|
||||
): TemplatePath | null {
|
||||
const pending = state.pendingTemplateAction;
|
||||
if (!pending) return null;
|
||||
const slug = pending.slug.trim();
|
||||
if (!slug) return null;
|
||||
if (pending.mode !== "customize" && pending.mode !== "useWithoutChanges") {
|
||||
return null;
|
||||
}
|
||||
return { slug, mode: pending.mode };
|
||||
}
|
||||
|
||||
export function isTemplateCreatePath(
|
||||
state: Pick<CreateFlowState, "pendingTemplateAction">,
|
||||
): boolean {
|
||||
return templatePathFromState(state) !== null;
|
||||
}
|
||||
|
||||
export function templateChipsForSlug(slug: string): TemplateChipSet | null {
|
||||
const templates = (
|
||||
templateChipsFile as { templates: Record<string, TemplateChipSet> }
|
||||
).templates;
|
||||
const chips = templates[slug];
|
||||
return chips ?? null;
|
||||
}
|
||||
|
||||
export function templateCardIdsForDeck(
|
||||
slug: string,
|
||||
deck: DeckId,
|
||||
): string[] {
|
||||
const chips = templateChipsForSlug(slug);
|
||||
if (!chips) return [];
|
||||
return [...chips[deck]];
|
||||
}
|
||||
|
||||
export function templateValueIdsForSlug(slug: string): string[] {
|
||||
const chips = templateChipsForSlug(slug);
|
||||
if (!chips) return [];
|
||||
return [...chips.coreValues];
|
||||
}
|
||||
|
||||
export function templateChipCounts(slug: string): {
|
||||
values: number;
|
||||
cards: number;
|
||||
} | null {
|
||||
const chips = templateChipsForSlug(slug);
|
||||
if (!chips) return null;
|
||||
return {
|
||||
values: chips.coreValues.length,
|
||||
cards:
|
||||
chips.communication.length +
|
||||
chips.membership.length +
|
||||
chips.decisionApproaches.length +
|
||||
chips.conflictManagement.length,
|
||||
};
|
||||
}
|
||||
|
||||
/** Put `firstIds` at the head (stable), then the remaining items in their original order. */
|
||||
export function orderIdsFirst<T extends { id: string }>(
|
||||
items: readonly T[],
|
||||
firstIds: readonly string[],
|
||||
): T[] {
|
||||
const byId = new Map(items.map((item) => [item.id, item] as const));
|
||||
const seen = new Set<string>();
|
||||
const head: T[] = [];
|
||||
for (const id of firstIds) {
|
||||
const item = byId.get(id);
|
||||
if (!item || seen.has(id)) continue;
|
||||
seen.add(id);
|
||||
head.push(item);
|
||||
}
|
||||
return [...head, ...items.filter((item) => !seen.has(item.id))];
|
||||
}
|
||||
Reference in new issue
Block a user