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))];
|
||||
}
|
||||
@@ -14,8 +14,8 @@ export type GovernanceTemplateCatalogEntry = {
|
||||
/** Path under public/ for getAssetPath() — Figma Asset / Template Mark */
|
||||
iconPath: string;
|
||||
/**
|
||||
* When true, static catalog rows show the “RECOMMENDED” tag. Facet-ranked
|
||||
* `/templates` sets this in `gridEntriesWithFacetScores` instead.
|
||||
* Templates carry no recommendation. Kept so grid card rows stay a
|
||||
* stable shape; always treat as false.
|
||||
*/
|
||||
recommended?: boolean;
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { RuleTemplateDto, TemplateFacetScoreDto } from "../create/fetchTemplates";
|
||||
import type { RuleTemplateDto } from "../create/fetchTemplates";
|
||||
import { templateSummaryFromBody } from "../create/templateReviewMapping";
|
||||
import type { GovernanceTemplateCatalogEntry } from "./governanceTemplateCatalog";
|
||||
import {
|
||||
@@ -25,7 +25,7 @@ function presentationForSlug(slug: string): Pick<
|
||||
return {
|
||||
iconPath: catalog.iconPath,
|
||||
backgroundColor: catalog.backgroundColor,
|
||||
recommended: catalog.recommended === true,
|
||||
recommended: false,
|
||||
};
|
||||
}
|
||||
return TEMPLATE_GRID_FALLBACK_PRESENTATION;
|
||||
@@ -43,74 +43,10 @@ export function ruleTemplateToGridEntry(template: RuleTemplateDto): TemplateGrid
|
||||
description,
|
||||
iconPath: pres.iconPath,
|
||||
backgroundColor: pres.backgroundColor,
|
||||
recommended: pres.recommended,
|
||||
recommended: false,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Max templates that show the “RECOMMENDED” tag when facet-ranked. Within the
|
||||
* **top score tier** only: we do not pad with lower-scoring templates (e.g. two
|
||||
* at score 4 and three at 3 → recommend the two 4s only), but if the top tier
|
||||
* exceeds this cap we still take the first `limit` in API order.
|
||||
*/
|
||||
export const TEMPLATE_GRID_COMPACT_RECOMMENDED_LIMIT = 5;
|
||||
|
||||
/**
|
||||
* Among templates in **API rank order** (score desc) with `score > 0`, mark
|
||||
* only those in the **maximum-score tier** (no lower tiers), at most `limit`
|
||||
* (API order is the tie-break when many tie for first place).
|
||||
*/
|
||||
export function deriveRecommendedTemplateSlugs(
|
||||
templatesInRankOrder: ReadonlyArray<{ slug: string }>,
|
||||
scores: Record<string, { score?: number } | undefined>,
|
||||
limit: number,
|
||||
): Set<string> {
|
||||
if (limit <= 0) return new Set();
|
||||
const matched = templatesInRankOrder.filter(
|
||||
(t) => (scores[t.slug]?.score ?? 0) > 0,
|
||||
);
|
||||
if (matched.length === 0) return new Set();
|
||||
let maxScore = 0;
|
||||
for (const t of matched) {
|
||||
const s = scores[t.slug]?.score ?? 0;
|
||||
if (s > maxScore) maxScore = s;
|
||||
}
|
||||
const topTier = matched.filter(
|
||||
(t) => (scores[t.slug]?.score ?? 0) === maxScore,
|
||||
);
|
||||
return new Set(topTier.slice(0, limit).map((t) => t.slug));
|
||||
}
|
||||
|
||||
export type GridEntriesWithFacetScoresOptions = {
|
||||
/** Default {@link TEMPLATE_GRID_COMPACT_RECOMMENDED_LIMIT}. */
|
||||
compactRecommendedLimit?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* After `GET /api/templates?facet.*` with `scores`, mark `recommended` only
|
||||
* for the top facet matches (see {@link deriveRecommendedTemplateSlugs}).
|
||||
*/
|
||||
export function gridEntriesWithFacetScores(
|
||||
templates: RuleTemplateDto[],
|
||||
scores: Record<string, TemplateFacetScoreDto>,
|
||||
options?: GridEntriesWithFacetScoresOptions,
|
||||
): TemplateGridCardEntry[] {
|
||||
const cap =
|
||||
options?.compactRecommendedLimit ?? TEMPLATE_GRID_COMPACT_RECOMMENDED_LIMIT;
|
||||
const recommendedSlugs = deriveRecommendedTemplateSlugs(
|
||||
templates,
|
||||
scores,
|
||||
cap,
|
||||
);
|
||||
return templates.map((t) => {
|
||||
const base = ruleTemplateToGridEntry(t);
|
||||
return {
|
||||
...base,
|
||||
recommended: recommendedSlugs.has(t.slug),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
const bySlug = (templates: RuleTemplateDto[]) =>
|
||||
new Map(templates.map((t) => [t.slug, t] as const));
|
||||
|
||||
|
||||
Reference in new issue
Block a user