Files
community-rule/lib/create/templatePath.ts
T
adilallo 6498d52541 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.
2026-11-09 17:34:33 -07:00

93 lines
2.7 KiB
TypeScript

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))];
}