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.
287 lines
9.3 KiB
TypeScript
287 lines
9.3 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useMemo, useState } from "react";
|
|
import {
|
|
buildTemplateCustomizePrefill,
|
|
templatePresetDetailsFromPrefill,
|
|
} from "../../../../lib/create/applyTemplatePrefill";
|
|
import { loadTemplateReviewBySlug } from "../../../../lib/create/loadTemplateReviewBySlug";
|
|
import { methodSectionsPinsForHydratedSelections } from "../../../../lib/create/publishedDocumentToCreateFlowState";
|
|
import { stripCustomRuleSelectionFields } from "../../../../lib/create/stripCustomRuleSelectionFields";
|
|
import { templateChipCounts } from "../../../../lib/create/templatePath";
|
|
import messages from "../../../../messages/en/index";
|
|
import type {
|
|
CreateFlowContextValue,
|
|
CreateFlowState,
|
|
} from "../types";
|
|
import { createFlowStepPath } from "../utils/createFlowPaths";
|
|
|
|
type AppRouterLike = { push: (_href: string) => void };
|
|
type UpdateState = CreateFlowContextValue["updateState"];
|
|
type ReplaceStateFn = CreateFlowContextValue["replaceState"];
|
|
|
|
export type TemplateCustomizeDialogModel = {
|
|
slug: string;
|
|
templateTitle: string;
|
|
body: unknown;
|
|
valuesCount: number;
|
|
cardsCount: number;
|
|
};
|
|
|
|
export type TemplateCustomizeFields = {
|
|
title: string;
|
|
communityContext: string;
|
|
communityLocation: string;
|
|
};
|
|
|
|
export type UseTemplateReviewActionsResult = {
|
|
/** True iff the current pathname is a template-review route (locale/basePath tolerant). */
|
|
isTemplateReviewRoute: boolean;
|
|
/** Decoded slug parsed out of the template-review pathname, or null. */
|
|
templateReviewSlug: string | null;
|
|
/** True between the fetch start and resolution for either action. */
|
|
isApplyingTemplate: boolean;
|
|
/** Set when the template fetch failed or the body was malformed. Cleared at the start of each action. */
|
|
templateReviewApplyError: string | null;
|
|
setTemplateReviewApplyError: (_message: string | null) => void;
|
|
/**
|
|
* Customize: open the name / photo / description dialog. Continue applies
|
|
* the template's cards and routes to `/create/core-values`. Setup questions
|
|
* are skipped.
|
|
*/
|
|
handleCustomize: () => Promise<void>;
|
|
/**
|
|
* Use without changes: seed the template's cards and go to final review.
|
|
* Direct entry clears leftover identity so the Rule card can collect name,
|
|
* photo, and description. In-flow keeps the community answers already given.
|
|
*/
|
|
handleUseWithoutChanges: () => Promise<void>;
|
|
customizeDialog: TemplateCustomizeDialogModel | null;
|
|
closeCustomizeDialog: () => void;
|
|
confirmCustomizeDialog: (_fields: TemplateCustomizeFields) => void;
|
|
};
|
|
|
|
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>[];
|
|
}
|
|
|
|
/**
|
|
* Encapsulates the two template-review footer actions (Customize / Use
|
|
* without changes) plus the Customize dialog they share.
|
|
*
|
|
* @example
|
|
* const {
|
|
* handleCustomize,
|
|
* handleUseWithoutChanges,
|
|
* customizeDialog,
|
|
* closeCustomizeDialog,
|
|
* confirmCustomizeDialog,
|
|
* } = useTemplateReviewActions({ ... });
|
|
*/
|
|
export function useTemplateReviewActions({
|
|
pathname,
|
|
state,
|
|
updateState,
|
|
replaceState,
|
|
router,
|
|
fromCreateWizard = false,
|
|
markCreateFlowInteraction,
|
|
}: {
|
|
pathname: string | null | undefined;
|
|
state: CreateFlowState;
|
|
updateState: UpdateState;
|
|
replaceState: ReplaceStateFn;
|
|
router: AppRouterLike;
|
|
/**
|
|
* True when this template-review URL was opened from the create wizard
|
|
* (`?fromFlow=1`). Direct catalog / marketing entry is false — a leftover
|
|
* draft title must not count as “community steps already done”.
|
|
*/
|
|
fromCreateWizard?: boolean;
|
|
markCreateFlowInteraction: () => void;
|
|
}): UseTemplateReviewActionsResult {
|
|
const [isApplyingTemplate, setIsApplyingTemplate] = useState(false);
|
|
const [templateReviewApplyError, setTemplateReviewApplyError] = useState<
|
|
string | null
|
|
>(null);
|
|
const [customizeDialog, setCustomizeDialog] =
|
|
useState<TemplateCustomizeDialogModel | null>(null);
|
|
|
|
const templateReviewSlug = useMemo(() => {
|
|
const m = pathname?.match(/\/create\/review-template\/([^/?#]+)/);
|
|
return m?.[1] ? decodeURIComponent(m[1]) : null;
|
|
}, [pathname]);
|
|
|
|
const isTemplateReviewRoute = Boolean(
|
|
pathname?.includes("/create/review-template/"),
|
|
);
|
|
|
|
const handleCustomize = useCallback(async () => {
|
|
if (!templateReviewSlug) return;
|
|
markCreateFlowInteraction();
|
|
setTemplateReviewApplyError(null);
|
|
setIsApplyingTemplate(true);
|
|
const loaded = await loadTemplateReviewBySlug(templateReviewSlug);
|
|
setIsApplyingTemplate(false);
|
|
if (loaded.ok === false) {
|
|
setTemplateReviewApplyError(loaded.message);
|
|
return;
|
|
}
|
|
const counts = templateChipCounts(templateReviewSlug);
|
|
setCustomizeDialog({
|
|
slug: templateReviewSlug,
|
|
templateTitle: loaded.template.title,
|
|
body: loaded.template.body,
|
|
valuesCount: counts?.values ?? 0,
|
|
cardsCount: counts?.cards ?? 0,
|
|
});
|
|
}, [markCreateFlowInteraction, templateReviewSlug]);
|
|
|
|
const closeCustomizeDialog = useCallback(() => {
|
|
setCustomizeDialog(null);
|
|
}, []);
|
|
|
|
const confirmCustomizeDialog = useCallback(
|
|
(fields: TemplateCustomizeFields) => {
|
|
if (!customizeDialog) return;
|
|
markCreateFlowInteraction();
|
|
const prefill = buildTemplateCustomizePrefill(
|
|
customizeDialog.body,
|
|
customizeDialog.slug,
|
|
);
|
|
const details = templatePresetDetailsFromPrefill(prefill);
|
|
const pinPatch = methodSectionsPinsForHydratedSelections(prefill);
|
|
const title = fields.title.trim();
|
|
const communityContext = fields.communityContext.trim();
|
|
const communityLocation = fields.communityLocation.trim();
|
|
updateState({
|
|
...prefill,
|
|
...details,
|
|
title: title.length > 0 ? title : undefined,
|
|
communityContext:
|
|
communityContext.length > 0 ? communityContext : undefined,
|
|
summary: communityContext.length > 0 ? communityContext : undefined,
|
|
communityLocation:
|
|
communityLocation.length > 0 ? communityLocation : undefined,
|
|
methodSectionsPinCommitted: {
|
|
...state.methodSectionsPinCommitted,
|
|
...pinPatch,
|
|
},
|
|
templateReviewBackSlug: customizeDialog.slug,
|
|
setupTraits: undefined,
|
|
pendingTemplateAction: {
|
|
slug: customizeDialog.slug,
|
|
mode: "customize",
|
|
},
|
|
});
|
|
setCustomizeDialog(null);
|
|
router.push(createFlowStepPath("core-values"));
|
|
},
|
|
[
|
|
customizeDialog,
|
|
markCreateFlowInteraction,
|
|
router,
|
|
state.methodSectionsPinCommitted,
|
|
updateState,
|
|
],
|
|
);
|
|
|
|
const handleUseWithoutChanges = useCallback(async () => {
|
|
if (!templateReviewSlug) return;
|
|
markCreateFlowInteraction();
|
|
setTemplateReviewApplyError(null);
|
|
setIsApplyingTemplate(true);
|
|
const loaded = await loadTemplateReviewBySlug(templateReviewSlug);
|
|
setIsApplyingTemplate(false);
|
|
if (loaded.ok === false) {
|
|
setTemplateReviewApplyError(loaded.message);
|
|
return;
|
|
}
|
|
const { template } = loaded;
|
|
const sections = sectionsFromTemplateBody(template.body);
|
|
if (!sections) {
|
|
setTemplateReviewApplyError(
|
|
messages.create.templateReview.errors.applyFailed,
|
|
);
|
|
return;
|
|
}
|
|
|
|
replaceState((prev) => {
|
|
const base = stripCustomRuleSelectionFields(prev);
|
|
const customizePrefill = buildTemplateCustomizePrefill(
|
|
template.body,
|
|
templateReviewSlug ?? undefined,
|
|
);
|
|
const details = templatePresetDetailsFromPrefill(customizePrefill);
|
|
const hasValuesSeed =
|
|
customizePrefill.selectedCoreValueIds !== undefined;
|
|
|
|
const sectionsWithoutValues = hasValuesSeed
|
|
? sections.filter((s) => {
|
|
const name = (s as { categoryName?: unknown }).categoryName;
|
|
if (typeof name !== "string") return true;
|
|
const key = name.toLowerCase().replace(/[^a-z]+/g, "");
|
|
return key !== "values" && key !== "corevalues";
|
|
})
|
|
: sections;
|
|
|
|
const pinPatch =
|
|
methodSectionsPinsForHydratedSelections(customizePrefill);
|
|
|
|
const seeded: CreateFlowState = {
|
|
...base,
|
|
...customizePrefill,
|
|
...details,
|
|
sections: sectionsWithoutValues,
|
|
methodSectionsPinCommitted: pinPatch,
|
|
templateReviewBackSlug: templateReviewSlug,
|
|
setupTraits: undefined,
|
|
pendingTemplateAction: {
|
|
slug: templateReviewSlug,
|
|
mode: "useWithoutChanges",
|
|
},
|
|
};
|
|
|
|
if (fromCreateWizard) {
|
|
return seeded;
|
|
}
|
|
|
|
return {
|
|
...seeded,
|
|
title: undefined,
|
|
communityContext: undefined,
|
|
communityLocation: undefined,
|
|
communityAvatarUrl: undefined,
|
|
communityAvatarFileName: undefined,
|
|
currentStep: undefined,
|
|
templateReviewEntryFromCreateFlow: undefined,
|
|
};
|
|
});
|
|
router.push(createFlowStepPath("final-review"));
|
|
}, [
|
|
fromCreateWizard,
|
|
markCreateFlowInteraction,
|
|
replaceState,
|
|
router,
|
|
templateReviewSlug,
|
|
]);
|
|
|
|
return {
|
|
isTemplateReviewRoute,
|
|
templateReviewSlug,
|
|
isApplyingTemplate,
|
|
templateReviewApplyError,
|
|
setTemplateReviewApplyError,
|
|
handleCustomize,
|
|
handleUseWithoutChanges,
|
|
customizeDialog,
|
|
closeCustomizeDialog,
|
|
confirmCustomizeDialog,
|
|
};
|
|
}
|