Open a template from its cards even when a saved draft exists.
This commit is contained in:
1 parent
851133e56d
commit
55b071aa83
11 files changed
+326
-140
No files matched your search
@@ -2,19 +2,25 @@
|
||||
|
||||
import { useCallback, useMemo, useState } from "react";
|
||||
import {
|
||||
buildTemplateCustomizePrefill,
|
||||
templatePresetDetailsFromPrefill,
|
||||
buildAppliedTemplateCreateFlowState,
|
||||
sectionsFromTemplateBody,
|
||||
} from "../../../../lib/create/applyTemplatePrefill";
|
||||
import { fetchDraftFromServer } from "../../../../lib/create/api";
|
||||
import { createFlowStateHasKeys } from "../../../../lib/create/draftHydrationUtils";
|
||||
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 { AsyncConfirmOptions } from "../../../hooks/useAsyncConfirm";
|
||||
import type {
|
||||
CreateFlowContextValue,
|
||||
CreateFlowState,
|
||||
} from "../types";
|
||||
import { hasBrowserCreateFlowDraft } from "../utils/createRuleEntry";
|
||||
import { createFlowStepPath } from "../utils/createFlowPaths";
|
||||
import {
|
||||
latchTemplateApply,
|
||||
prepareFreshCreateFlowEntrySync,
|
||||
} from "../utils/prepareFreshCreateFlowEntry";
|
||||
|
||||
type AppRouterLike = { push: (_href: string) => void };
|
||||
type UpdateState = CreateFlowContextValue["updateState"];
|
||||
@@ -61,13 +67,27 @@ export type UseTemplateReviewActionsResult = {
|
||||
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>[];
|
||||
const TEMPLATE_NAV_KEYS = new Set([
|
||||
"templateReviewEntryFromCreateFlow",
|
||||
"templateReviewBackSlug",
|
||||
"pendingTemplateAction",
|
||||
"currentStep",
|
||||
"methodSectionsPinCommitted",
|
||||
]);
|
||||
|
||||
function createFlowStateLooksLikeProgress(state: CreateFlowState): boolean {
|
||||
return Object.keys(state).some((key) => !TEMPLATE_NAV_KEYS.has(key));
|
||||
}
|
||||
|
||||
async function hasDraftToReplace(
|
||||
state: CreateFlowState,
|
||||
signedIn: boolean,
|
||||
): Promise<boolean> {
|
||||
if (createFlowStateLooksLikeProgress(state)) return true;
|
||||
if (hasBrowserCreateFlowDraft()) return true;
|
||||
if (!signedIn) return false;
|
||||
const server = await fetchDraftFromServer();
|
||||
return server != null && createFlowStateHasKeys(server);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,10 +106,11 @@ function sectionsFromTemplateBody(
|
||||
export function useTemplateReviewActions({
|
||||
pathname,
|
||||
state,
|
||||
updateState,
|
||||
replaceState,
|
||||
router,
|
||||
fromCreateWizard = false,
|
||||
signedIn = false,
|
||||
requestConfirm,
|
||||
markCreateFlowInteraction,
|
||||
}: {
|
||||
pathname: string | null | undefined;
|
||||
@@ -103,6 +124,8 @@ export function useTemplateReviewActions({
|
||||
* draft title must not count as “community steps already done”.
|
||||
*/
|
||||
fromCreateWizard?: boolean;
|
||||
signedIn?: boolean;
|
||||
requestConfirm?: (_opts: AsyncConfirmOptions) => Promise<boolean>;
|
||||
markCreateFlowInteraction: () => void;
|
||||
}): UseTemplateReviewActionsResult {
|
||||
const [isApplyingTemplate, setIsApplyingTemplate] = useState(false);
|
||||
@@ -121,10 +144,26 @@ export function useTemplateReviewActions({
|
||||
pathname?.includes("/create/review-template/"),
|
||||
);
|
||||
|
||||
const keepInFlowIdentity =
|
||||
fromCreateWizard && state.templateReviewEntryFromCreateFlow === true;
|
||||
|
||||
const confirmReplaceDraft = useCallback(async (): Promise<boolean> => {
|
||||
if (!requestConfirm) return true;
|
||||
if (!(await hasDraftToReplace(state, signedIn))) return true;
|
||||
const copy = messages.create.templateReview.replaceDraft;
|
||||
return requestConfirm({
|
||||
title: copy.title,
|
||||
description: copy.description,
|
||||
proceedText: copy.proceed,
|
||||
cancelText: copy.cancel,
|
||||
});
|
||||
}, [requestConfirm, signedIn, state]);
|
||||
|
||||
const handleCustomize = useCallback(async () => {
|
||||
if (!templateReviewSlug) return;
|
||||
markCreateFlowInteraction();
|
||||
setTemplateReviewApplyError(null);
|
||||
if (!(await confirmReplaceDraft())) return;
|
||||
setIsApplyingTemplate(true);
|
||||
const loaded = await loadTemplateReviewBySlug(templateReviewSlug);
|
||||
setIsApplyingTemplate(false);
|
||||
@@ -140,7 +179,11 @@ export function useTemplateReviewActions({
|
||||
valuesCount: counts?.values ?? 0,
|
||||
cardsCount: counts?.cards ?? 0,
|
||||
});
|
||||
}, [markCreateFlowInteraction, templateReviewSlug]);
|
||||
}, [
|
||||
confirmReplaceDraft,
|
||||
markCreateFlowInteraction,
|
||||
templateReviewSlug,
|
||||
]);
|
||||
|
||||
const closeCustomizeDialog = useCallback(() => {
|
||||
setCustomizeDialog(null);
|
||||
@@ -150,44 +193,29 @@ export function useTemplateReviewActions({
|
||||
(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: {
|
||||
prepareFreshCreateFlowEntrySync({ signedIn });
|
||||
latchTemplateApply();
|
||||
replaceState(() =>
|
||||
buildAppliedTemplateCreateFlowState({
|
||||
slug: customizeDialog.slug,
|
||||
body: customizeDialog.body,
|
||||
mode: "customize",
|
||||
},
|
||||
});
|
||||
identity: {
|
||||
title: fields.title,
|
||||
communityContext: fields.communityContext,
|
||||
communityLocation: fields.communityLocation,
|
||||
},
|
||||
}),
|
||||
);
|
||||
setCustomizeDialog(null);
|
||||
router.push(createFlowStepPath("core-values"));
|
||||
},
|
||||
[
|
||||
customizeDialog,
|
||||
markCreateFlowInteraction,
|
||||
replaceState,
|
||||
router,
|
||||
state.methodSectionsPinCommitted,
|
||||
updateState,
|
||||
signedIn,
|
||||
],
|
||||
);
|
||||
|
||||
@@ -195,6 +223,7 @@ export function useTemplateReviewActions({
|
||||
if (!templateReviewSlug) return;
|
||||
markCreateFlowInteraction();
|
||||
setTemplateReviewApplyError(null);
|
||||
if (!(await confirmReplaceDraft())) return;
|
||||
setIsApplyingTemplate(true);
|
||||
const loaded = await loadTemplateReviewBySlug(templateReviewSlug);
|
||||
setIsApplyingTemplate(false);
|
||||
@@ -211,63 +240,33 @@ export function useTemplateReviewActions({
|
||||
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,
|
||||
};
|
||||
});
|
||||
prepareFreshCreateFlowEntrySync({ signedIn });
|
||||
latchTemplateApply();
|
||||
replaceState(() =>
|
||||
buildAppliedTemplateCreateFlowState({
|
||||
slug: templateReviewSlug,
|
||||
body: template.body,
|
||||
mode: "useWithoutChanges",
|
||||
identity: keepInFlowIdentity
|
||||
? {
|
||||
title: state.title,
|
||||
communityContext: state.communityContext,
|
||||
communityLocation: state.communityLocation,
|
||||
}
|
||||
: undefined,
|
||||
}),
|
||||
);
|
||||
router.push(createFlowStepPath("final-review"));
|
||||
}, [
|
||||
fromCreateWizard,
|
||||
confirmReplaceDraft,
|
||||
keepInFlowIdentity,
|
||||
markCreateFlowInteraction,
|
||||
replaceState,
|
||||
router,
|
||||
state.communityContext,
|
||||
state.communityLocation,
|
||||
signedIn,
|
||||
state.title,
|
||||
templateReviewSlug,
|
||||
]);
|
||||
|
||||
|
||||
Reference in new issue
Block a user