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
@@ -336,6 +336,8 @@ function CreateFlowLayoutContent({
|
||||
replaceState,
|
||||
router,
|
||||
fromCreateWizard,
|
||||
signedIn: Boolean(sessionUser),
|
||||
requestConfirm,
|
||||
markCreateFlowInteraction,
|
||||
});
|
||||
const isCreateTemplatesPickerRoute =
|
||||
@@ -901,15 +903,25 @@ function CreateFlowLayoutContent({
|
||||
templateTitle={customizeDialog?.templateTitle ?? ""}
|
||||
valuesCount={customizeDialog?.valuesCount ?? 0}
|
||||
cardsCount={customizeDialog?.cardsCount ?? 0}
|
||||
initialTitle={typeof state.title === "string" ? state.title : ""}
|
||||
initialTitle={
|
||||
fromCreateWizard && state.templateReviewEntryFromCreateFlow === true
|
||||
? typeof state.title === "string"
|
||||
? state.title
|
||||
: ""
|
||||
: ""
|
||||
}
|
||||
initialDescription={
|
||||
typeof state.communityContext === "string"
|
||||
? state.communityContext
|
||||
fromCreateWizard && state.templateReviewEntryFromCreateFlow === true
|
||||
? typeof state.communityContext === "string"
|
||||
? state.communityContext
|
||||
: ""
|
||||
: ""
|
||||
}
|
||||
initialLocation={
|
||||
typeof state.communityLocation === "string"
|
||||
? state.communityLocation
|
||||
fromCreateWizard && state.templateReviewEntryFromCreateFlow === true
|
||||
? typeof state.communityLocation === "string"
|
||||
? state.communityLocation
|
||||
: ""
|
||||
: ""
|
||||
}
|
||||
onClose={closeCustomizeDialog}
|
||||
|
||||
@@ -21,7 +21,11 @@ import {
|
||||
isCreateFlowTemplatesPickerPath,
|
||||
parseCreateFlowScreenFromPathname,
|
||||
} from "./utils/flowSteps";
|
||||
import { hasFreshEntryPending } from "./utils/prepareFreshCreateFlowEntry";
|
||||
import {
|
||||
hasFreshEntryPending,
|
||||
hasTemplateApplyLatch,
|
||||
clearTemplateApplyLatch,
|
||||
} from "./utils/prepareFreshCreateFlowEntry";
|
||||
|
||||
import { isBackendSyncEnabled } from "../../../lib/create/backendSyncEnabled";
|
||||
|
||||
@@ -94,6 +98,12 @@ export function SignedInDraftHydration({
|
||||
return;
|
||||
}
|
||||
|
||||
if (hasTemplateApplyLatch()) {
|
||||
finishedUserIdRef.current = userId;
|
||||
clearTemplateApplyLatch();
|
||||
return;
|
||||
}
|
||||
|
||||
// Local draft wins over server: no fetch, no replaceState. The provider
|
||||
// already hydrated from localStorage at mount, so the user sees their
|
||||
// unsaved keystrokes immediately.
|
||||
|
||||
@@ -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,
|
||||
]);
|
||||
|
||||
|
||||
@@ -11,6 +11,9 @@ import { isBackendSyncEnabled } from "../../../../lib/create/backendSyncEnabled"
|
||||
*/
|
||||
export const FRESH_ENTRY_PENDING_KEY = "create:fresh-entry-pending";
|
||||
|
||||
/** Set when a template is applied so server-draft hydration cannot replace it. */
|
||||
export const TEMPLATE_APPLY_LATCH_KEY = "create:template-apply-latch";
|
||||
|
||||
export type PrepareFreshCreateFlowEntryOptions = {
|
||||
/**
|
||||
* When `true`, and backend sync is on, also `DELETE /api/drafts/me`.
|
||||
@@ -28,6 +31,33 @@ export function hasFreshEntryPending(): boolean {
|
||||
}
|
||||
}
|
||||
|
||||
export function latchTemplateApply(): void {
|
||||
if (typeof window === "undefined") return;
|
||||
try {
|
||||
window.sessionStorage.setItem(TEMPLATE_APPLY_LATCH_KEY, "1");
|
||||
} catch {
|
||||
/* ignore — sessionStorage may be unavailable */
|
||||
}
|
||||
}
|
||||
|
||||
export function hasTemplateApplyLatch(): boolean {
|
||||
if (typeof window === "undefined") return false;
|
||||
try {
|
||||
return window.sessionStorage.getItem(TEMPLATE_APPLY_LATCH_KEY) === "1";
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function clearTemplateApplyLatch(): void {
|
||||
if (typeof window === "undefined") return;
|
||||
try {
|
||||
window.sessionStorage.removeItem(TEMPLATE_APPLY_LATCH_KEY);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
function setFreshEntryPending(): void {
|
||||
if (typeof window === "undefined") return;
|
||||
try {
|
||||
|
||||
@@ -258,7 +258,7 @@ export function ProfilePageView({
|
||||
id: "create-template",
|
||||
title: t("optionCreateTemplate"),
|
||||
description: "",
|
||||
href: "/templates?fromFlow=1",
|
||||
href: "/templates",
|
||||
leadingIcon: "content_copy",
|
||||
showDescription: false,
|
||||
},
|
||||
@@ -356,7 +356,7 @@ export function ProfilePageView({
|
||||
createLabel={t("emptyCardCreate")}
|
||||
browseLabel={t("emptyCardBrowse")}
|
||||
onCreate={onStartNewCustomRule}
|
||||
browseHref="/templates?fromFlow=1"
|
||||
browseHref="/templates"
|
||||
/>
|
||||
) : (
|
||||
<div className="flex flex-col gap-3">
|
||||
|
||||
Reference in new issue
Block a user