From d31045b0e5bd31431e1445ad547d6ea0a14b4f45 Mon Sep 17 00:00:00 2001 From: adilallo <39313955+adilallo@users.noreply.github.com> Date: Sun, 22 Nov 2026 17:50:06 -0700 Subject: [PATCH] Keep the browser draft on screen after Save your progress, including when the save link is dead. --- .../create/context/CreateFlowContext.tsx | 19 ++--- .../create/utils/anonymousDraftStorage.ts | 10 +++ app/components/modals/Login/LoginForm.tsx | 2 + tests/components/LoginForm.test.tsx | 35 +++++++++- tests/contexts/CreateFlowContext.test.tsx | 70 ++++++++++++++++++- tests/unit/anonymousDraftStorage.test.ts | 17 +++++ 6 files changed, 141 insertions(+), 12 deletions(-) diff --git a/app/(app)/create/context/CreateFlowContext.tsx b/app/(app)/create/context/CreateFlowContext.tsx index 8aba4a0..17af270 100644 --- a/app/(app)/create/context/CreateFlowContext.tsx +++ b/app/(app)/create/context/CreateFlowContext.tsx @@ -18,7 +18,6 @@ import type { import { clearAnonymousCreateFlowStorage, clearLegacyCreateFlowKeysOnce, - hasTransferPendingFlag, readAnonymousCreateFlowState, writeAnonymousCreateFlowState, } from "../utils/anonymousDraftStorage"; @@ -65,6 +64,7 @@ export function CreateFlowProvider({ const [currentStep] = useState(initialStep); const prevPersistRef = useRef(enableLocalDraftMirroring); const persistWriteSkipRef = useRef(true); + const coreValueWriteSkipRef = useRef(true); const initialHydrateDoneRef = useRef(false); useEffect(() => { @@ -115,13 +115,6 @@ export function CreateFlowProvider({ const wasOff = !prevPersistRef.current; prevPersistRef.current = true; if (!wasOff) return; - if (hasTransferPendingFlag()) return; - if ( - typeof window !== "undefined" && - new URLSearchParams(window.location.search).get("syncDraft") === "1" - ) { - return; - } const from = readAnonymousCreateFlowState(); if (Object.keys(from).length === 0) return; // eslint-disable-next-line react-hooks/set-state-in-effect -- hydrate local draft when mirroring turns on @@ -160,8 +153,16 @@ export function CreateFlowProvider({ /** Meaning/signals for core values: survives refresh for signed-in users; merged with anonymous draft when both exist. */ useEffect(() => { + if (!enableLocalDraftMirroring) { + coreValueWriteSkipRef.current = true; + return; + } + if (coreValueWriteSkipRef.current) { + coreValueWriteSkipRef.current = false; + return; + } writeCoreValueDetailsToLocalStorage(state.coreValueDetailsByChipId); - }, [state.coreValueDetailsByChipId]); + }, [state.coreValueDetailsByChipId, enableLocalDraftMirroring]); const markCreateFlowInteraction = useCallback(() => { setInteractionTouched(true); diff --git a/app/(app)/create/utils/anonymousDraftStorage.ts b/app/(app)/create/utils/anonymousDraftStorage.ts index 627e014..60341df 100644 --- a/app/(app)/create/utils/anonymousDraftStorage.ts +++ b/app/(app)/create/utils/anonymousDraftStorage.ts @@ -84,6 +84,16 @@ export function hasTransferPendingFlag(): boolean { } } +/** Drop the transfer flag without touching the stored draft. */ +export function clearTransferPendingFlag(): void { + if (typeof window === "undefined") return; + try { + window.localStorage.removeItem(CREATE_FLOW_TRANSFER_PENDING_KEY); + } catch { + // ignore + } +} + /** One-time cleanup of pre–anonymous-draft keys. */ export function clearLegacyCreateFlowKeysOnce(): void { if (typeof window === "undefined") return; diff --git a/app/components/modals/Login/LoginForm.tsx b/app/components/modals/Login/LoginForm.tsx index a8c8dfd..420c818 100644 --- a/app/components/modals/Login/LoginForm.tsx +++ b/app/components/modals/Login/LoginForm.tsx @@ -14,6 +14,7 @@ import { buildCreateFlowDraftPayload } from "../../../../lib/create/buildCreateF import { MAGIC_LINK_TTL_MINUTES } from "../../../../lib/privacyRetention"; import { safeInternalPath } from "../../../../lib/safeInternalPath"; import { + clearTransferPendingFlag, readAnonymousCreateFlowState, setTransferPendingFlag, } from "../../../(app)/create/utils/anonymousDraftStorage"; @@ -212,6 +213,7 @@ export default function LoginForm({ useEffect(() => { if (isExpiredLinkError(errorParam)) { setLinkExpired(true); + clearTransferPendingFlag(); } }, [errorParam]); diff --git a/tests/components/LoginForm.test.tsx b/tests/components/LoginForm.test.tsx index 2483c44..43650bb 100644 --- a/tests/components/LoginForm.test.tsx +++ b/tests/components/LoginForm.test.tsx @@ -45,7 +45,11 @@ vi.mock("../../app/(app)/create/utils/anonymousDraftStorage", async (importOrigi import { EMAIL_MAX_LEN } from "../../lib/create/isValidCreateFlowSaveEmail"; import { requestMagicLink } from "../../lib/create/api"; -import { setTransferPendingFlag } from "../../app/(app)/create/utils/anonymousDraftStorage"; +import { + CREATE_FLOW_ANONYMOUS_KEY, + CREATE_FLOW_TRANSFER_PENDING_KEY, + setTransferPendingFlag, +} from "../../app/(app)/create/utils/anonymousDraftStorage"; function renderLoginForm() { return renderWithProviders( @@ -62,6 +66,7 @@ describe("LoginForm", () => { navMock.replace.mockReset(); navMock.pathname = "/"; navMock.searchParams = new URLSearchParams(); + window.localStorage.clear(); }); it("renders title, email field, and submit control", () => { @@ -368,6 +373,34 @@ describe("LoginForm", () => { ).toBeInTheDocument(); }); + it("clears the transfer flag on an expired link and keeps the browser draft", () => { + window.localStorage.setItem( + CREATE_FLOW_ANONYMOUS_KEY, + JSON.stringify({ title: "Garden coop" }), + ); + window.localStorage.setItem(CREATE_FLOW_TRANSFER_PENDING_KEY, "1"); + navMock.searchParams = new URLSearchParams("error=expired_link"); + renderLoginForm(); + expect(window.localStorage.getItem(CREATE_FLOW_TRANSFER_PENDING_KEY)).toBeNull(); + expect(window.localStorage.getItem(CREATE_FLOW_ANONYMOUS_KEY)).toContain( + "Garden coop", + ); + }); + + it("clears the transfer flag on an already-used link and keeps the browser draft", () => { + window.localStorage.setItem( + CREATE_FLOW_ANONYMOUS_KEY, + JSON.stringify({ title: "Garden coop" }), + ); + window.localStorage.setItem(CREATE_FLOW_TRANSFER_PENDING_KEY, "1"); + navMock.searchParams = new URLSearchParams("error=invalid_link"); + renderLoginForm(); + expect(window.localStorage.getItem(CREATE_FLOW_TRANSFER_PENDING_KEY)).toBeNull(); + expect(window.localStorage.getItem(CREATE_FLOW_ANONYMOUS_KEY)).toContain( + "Garden coop", + ); + }); + it("shows URL-driven error for invalid_link", () => { navMock.searchParams = new URLSearchParams("error=invalid_link"); renderLoginForm(); diff --git a/tests/contexts/CreateFlowContext.test.tsx b/tests/contexts/CreateFlowContext.test.tsx index 5f9a653..f6b6e4a 100644 --- a/tests/contexts/CreateFlowContext.test.tsx +++ b/tests/contexts/CreateFlowContext.test.tsx @@ -1,10 +1,14 @@ -import { describe, it, expect } from "vitest"; -import { act, render, screen } from "@testing-library/react"; +import { afterEach, describe, it, expect } from "vitest"; +import { act, render, screen, waitFor } from "@testing-library/react"; import "@testing-library/jest-dom/vitest"; import { CreateFlowProvider, useCreateFlow, } from "../../app/(app)/create/context/CreateFlowContext"; +import { + CREATE_FLOW_ANONYMOUS_KEY, + CREATE_FLOW_TRANSFER_PENDING_KEY, +} from "../../app/(app)/create/utils/anonymousDraftStorage"; /** * Harness: mounts a consumer that renders the state we want to assert on and @@ -46,6 +50,15 @@ function Harness() {
{state.methodSectionsPinCommitted?.communication ? "yes" : "no"}
+
+ {(state.selectedCommunitySizeIds ?? []).join(",")} +
+
+ {(state.selectedOrganizationTypeIds ?? []).join(",")} +
+
+ {(state.selectedMaturityIds ?? []).join(",")} +
); } @@ -129,3 +142,56 @@ describe("CreateFlowContext — resetCustomRuleSelections", () => { expect(screen.getByTestId("core").textContent).toBe(""); }); }); + +describe("CreateFlowContext — draft hydration after Save your progress", () => { + afterEach(() => { + window.localStorage.clear(); + }); + + it("loads the stored draft when mirroring turns on, even with the transfer flag", async () => { + window.localStorage.setItem( + CREATE_FLOW_ANONYMOUS_KEY, + JSON.stringify({ + title: "Juniper Street Tenants", + selectedCommunitySizeIds: ["thirteenToOneHundred"], + selectedOrganizationTypeIds: ["housing"], + selectedMaturityIds: ["forming"], + }), + ); + window.localStorage.setItem(CREATE_FLOW_TRANSFER_PENDING_KEY, "1"); + + const { rerender } = render( + + + , + ); + rerender( + + + , + ); + + await waitFor(() => { + expect(screen.getByTestId("title").textContent).toBe( + "Juniper Street Tenants", + ); + }); + expect(screen.getByTestId("size").textContent).toBe("thirteenToOneHundred"); + expect(screen.getByTestId("kind").textContent).toBe("housing"); + expect(screen.getByTestId("maturity").textContent).toBe("forming"); + + act(() => { + getUpdateState()({ title: "Juniper Street Tenants Union" }); + }); + + await waitFor(() => { + const stored = JSON.parse( + window.localStorage.getItem(CREATE_FLOW_ANONYMOUS_KEY) ?? "{}", + ) as Record; + expect(stored.title).toBe("Juniper Street Tenants Union"); + expect(stored.selectedCommunitySizeIds).toEqual(["thirteenToOneHundred"]); + expect(stored.selectedOrganizationTypeIds).toEqual(["housing"]); + expect(stored.selectedMaturityIds).toEqual(["forming"]); + }); + }); +}); diff --git a/tests/unit/anonymousDraftStorage.test.ts b/tests/unit/anonymousDraftStorage.test.ts index a96a293..60294ef 100644 --- a/tests/unit/anonymousDraftStorage.test.ts +++ b/tests/unit/anonymousDraftStorage.test.ts @@ -15,6 +15,7 @@ import { CREATE_FLOW_ANONYMOUS_KEY, CREATE_FLOW_TRANSFER_PENDING_KEY, clearAnonymousCreateFlowStorage, + clearTransferPendingFlag, } from "../../app/(app)/create/utils/anonymousDraftStorage"; describe("clearAnonymousCreateFlowStorage", () => { @@ -41,3 +42,19 @@ describe("clearAnonymousCreateFlowStorage", () => { expect(forgetFlushedCommunityAvatarUrl).not.toHaveBeenCalled(); }); }); + +describe("clearTransferPendingFlag", () => { + beforeEach(() => { + localStorage.clear(); + localStorage.setItem(CREATE_FLOW_ANONYMOUS_KEY, "{\"title\":\"Garden\"}"); + localStorage.setItem(CREATE_FLOW_TRANSFER_PENDING_KEY, "1"); + }); + + it("removes the flag and leaves the stored draft", () => { + clearTransferPendingFlag(); + expect(localStorage.getItem(CREATE_FLOW_TRANSFER_PENDING_KEY)).toBeNull(); + expect(localStorage.getItem(CREATE_FLOW_ANONYMOUS_KEY)).toBe( + "{\"title\":\"Garden\"}", + ); + }); +});