Open a template from its cards even when a saved draft exists.

This commit is contained in:
adilallo committed 2026-11-22 18:08:34 -07:00
1 parent 851133e56d
commit 55b071aa83
11 files changed
+326 -140

No files matched your search

+70
View File
@@ -12,6 +12,7 @@ import {
decisionApproachPresetFor,
membershipPresetFor,
} from "./finalReviewChipPresets";
import { methodSectionsPinsForHydratedSelections } from "./publishedDocumentToCreateFlowState";
import {
templateChipsForSlug,
type TemplateChipSet,
@@ -223,3 +224,72 @@ export function templatePresetDetailsFromPrefill(
coreValueDetailsByChipId,
};
}
export function sectionsFromTemplateBody(
body: unknown,
): Record<string, unknown>[] | null {
if (!body || typeof body !== "object" || Array.isArray(body)) return null;
const sectionsRaw = (body as { sections?: unknown }).sections;
if (!Array.isArray(sectionsRaw) || sectionsRaw.length === 0) return null;
return sectionsRaw as Record<string, unknown>[];
}
export type TemplateApplyIdentity = {
title?: string;
communityContext?: string;
communityLocation?: string;
};
/**
* Fresh wizard state for a template. Does not spread a previous draft, so a
* leftover logo, setup answers, or another community's cards cannot ride along.
*/
export function buildAppliedTemplateCreateFlowState(args: {
slug: string;
body: unknown;
mode: "customize" | "useWithoutChanges";
identity?: TemplateApplyIdentity;
}): CreateFlowState {
const prefill = buildTemplateCustomizePrefill(args.body, args.slug);
const details = templatePresetDetailsFromPrefill(prefill);
const pinPatch = methodSectionsPinsForHydratedSelections(prefill);
const title = args.identity?.title?.trim();
const communityContext = args.identity?.communityContext?.trim();
const communityLocation = args.identity?.communityLocation?.trim();
const seeded: CreateFlowState = {
...prefill,
...details,
methodSectionsPinCommitted: pinPatch,
templateReviewBackSlug: args.slug,
pendingTemplateAction: {
slug: args.slug,
mode: args.mode,
},
};
if (title) seeded.title = title;
if (communityContext) {
seeded.communityContext = communityContext;
seeded.summary = communityContext;
}
if (communityLocation) seeded.communityLocation = communityLocation;
if (args.identity) seeded.templateReviewEntryFromCreateFlow = true;
if (args.mode === "useWithoutChanges") {
const sections = sectionsFromTemplateBody(args.body);
if (sections) {
const hasValuesSeed = seeded.selectedCoreValueIds !== undefined;
seeded.sections = hasValuesSeed
? sections.filter((section) => {
const name = (section as { categoryName?: unknown }).categoryName;
if (typeof name !== "string") return true;
const key = name.toLowerCase().replace(/[^a-z]+/g, "");
return key !== "values" && key !== "corevalues";
})
: sections;
}
}
return seeded;
}