Signed in create rule clear

This commit is contained in:
adilallo
2026-04-29 07:34:40 -06:00
parent 048dceced9
commit 815de2fdfd
10 changed files with 95 additions and 46 deletions
+4 -2
View File
@@ -27,8 +27,10 @@ const SYNC_ENABLED = process.env.NEXT_PUBLIC_ENABLE_BACKEND_SYNC === "true";
* server draft on top would clobber unsaved keystrokes with a stale snapshot.
*
* Server draft becomes authoritative only when localStorage is empty — i.e.
* fresh device, after explicit Save & Exit (which clears localStorage), or
* after Exit-from-completed clears local state.
* fresh device, after explicit Save & Exit (which clears localStorage),
* after Exit-from-completed clears local state, or after
* {@link prepareFreshCreateFlowEntry} (Create rule / new template entry) clears
* local + deletes the server draft when sync is on.
*
* Skips when `?syncDraft=1` or transfer-pending — {@link PostLoginDraftTransfer}
* owns that path.
@@ -4,17 +4,13 @@ import { clearCoreValueDetailsLocalStorage } from "./coreValueDetailsLocalStorag
/**
* Wipe the anonymous in-progress create-flow draft from `localStorage` (both
* the main `create-flow-anonymous` blob and the separate core-value details
* key). Intended for call sites that navigate **into** the create flow from
* outside and want a fresh slate — today that's the marketing "Popular
* templates" click handler on the home page and the `/templates` index page
* (when not in-flow). `CreateFlowProvider` reads `localStorage` during its
* `useState` initializer, so clearing *before* pushing the next route means
* the provider mounts empty and the Create Community stage starts clean.
* key). Clearing *before* `router.push` means `CreateFlowProvider` can read
* empty storage on mount.
*
* Note: this only touches localStorage. It does **not** delete the
* authenticated user's server draft (`/api/drafts/me`). Server drafts are
* loaded deliberately from the profile page, not re-hydrated into the flow
* on every entry, so there's nothing to wipe here for signed-in users.
* For marketing/profile “new rule” entry that should also remove the signed-in
* server draft when backend sync is on, use {@link prepareFreshCreateFlowEntry}.
*
* This helper only touches `localStorage`; it does **not** `DELETE /api/drafts/me`.
*/
export function clearCreateFlowPersistedDrafts(): void {
clearAnonymousCreateFlowStorage();
@@ -0,0 +1,23 @@
import { deleteServerDraft } from "../../../../lib/create/api";
import { clearAnonymousCreateFlowStorage } from "./anonymousDraftStorage";
import { clearCoreValueDetailsLocalStorage } from "./coreValueDetailsLocalStorage";
const SYNC_ENABLED =
process.env.NEXT_PUBLIC_ENABLE_BACKEND_SYNC === "true";
/**
* Call **before** navigating into `/create` from marketing or profile “new rule”
* entry points so signed-in + sync matches an anonymous fresh start: wipe
* `localStorage` draft keys and, when sync is on, `DELETE /api/drafts/me`.
* Anonymous `DELETE` is harmless (401). Await ensures the server draft is gone
* before mount so {@link SignedInDraftHydration} does not rehydrate stale work.
*
* Do **not** use for “Continue draft” — that path should load the server draft.
*/
export async function prepareFreshCreateFlowEntry(): Promise<void> {
clearAnonymousCreateFlowStorage();
clearCoreValueDetailsLocalStorage();
if (SYNC_ENABLED) {
await deleteServerDraft();
}
}
+12
View File
@@ -22,6 +22,8 @@ import {
} from "../create/utils/flowSteps";
import type { CreateFlowStep } from "../create/types";
import { clearAnonymousCreateFlowStorage } from "../create/utils/anonymousDraftStorage";
import { clearCoreValueDetailsLocalStorage } from "../create/utils/coreValueDetailsLocalStorage";
import { prepareFreshCreateFlowEntry } from "../create/utils/prepareFreshCreateFlowEntry";
import { useMediaQuery } from "../../hooks/useMediaQuery";
import {
ProfilePageSignedOutView,
@@ -245,9 +247,18 @@ export default function ProfilePageClient() {
const handleContinueDraft = useCallback(() => {
if (draft == null || !draft.hasDraft) return;
const step = resolveContinueStepState(draft.state);
clearAnonymousCreateFlowStorage();
clearCoreValueDetailsLocalStorage();
router.push(`/create/${step}`);
}, [draft, router]);
const handleStartNewCustomRule = useCallback(() => {
void (async () => {
await prepareFreshCreateFlowEntry();
router.push("/create");
})();
}, [router]);
const handleRequestDeleteDraft = useCallback(() => {
setActionError(null);
setDraftDeleteOpen(true);
@@ -360,6 +371,7 @@ export default function ProfilePageClient() {
}}
onCloseDeleteAccount={() => setAccountDeleteOpen(false)}
onConfirmDeleteAccount={handleConfirmDeleteAccount}
onStartNewCustomRule={handleStartNewCustomRule}
/>
);
}
@@ -72,6 +72,8 @@ export type ProfilePageViewProps = {
onDismissProfileSuccess: () => void;
onDismissActionError: () => void;
onDismissRulesError: () => void;
/** Clears local + server draft (when sync) then routes to `/create` — same fresh start as marketing “Create rule”. */
onStartNewCustomRule: () => void;
};
/**
@@ -199,6 +201,7 @@ export function ProfilePageView({
onDismissProfileSuccess,
onDismissActionError,
onDismissRulesError,
onStartNewCustomRule,
}: ProfilePageViewProps) {
const t = useTranslation("pages.profile");
const tLogin = useTranslation("pages.login");
@@ -213,7 +216,7 @@ export function ProfilePageView({
id: "create-custom",
title: t("optionCreateCustom"),
description: "",
href: "/create",
onClick: onStartNewCustomRule,
leadingIcon: "edit",
showDescription: false,
},
@@ -251,7 +254,7 @@ export function ProfilePageView({
showDescription: false,
},
];
}, [t, onSignOut, onOpenDeleteAccount, onOpenEmailChange]);
}, [t, onSignOut, onOpenDeleteAccount, onOpenEmailChange, onStartNewCustomRule]);
const ruleCardShellClass =
"w-full !max-w-full cursor-default !gap-3 !rounded-[12px] shadow-[0_0_48px_rgba(0,0,0,0.1)] lg:!rounded-[24px] lg:shadow-[0_0_24px_rgba(0,0,0,0.1)]";