Keep a continued draft on screen, ask before Edit drops rewritten copy, and retry clearing the draft after publish.

This commit is contained in:
adilallo committed 2026-11-22 18:31:07 -07:00
1 parent 54a4871ad1
commit 7c08766092
11 files changed
+361 -41

No files matched your search

+8 -7
View File
@@ -164,19 +164,20 @@ async function errorBodyMessage(res: Response): Promise<string> {
}
/**
* Wipe the signed-in user's saved draft. Fire-and-forget: any non-2xx (including
* the sync-flag-off `503` and the unauthenticated `401`) is swallowed because
* callers only invoke this on already-published / explicitly-discarded flows
* where a leftover server draft is acceptable.
* Wipe the signed-in user's saved draft.
* Returns true when the server confirms the draft is gone.
* Callers that only need a best-effort wipe can ignore a false result
* (`503` when sync is off, `401` when signed out, or a network error).
*/
export async function deleteServerDraft(): Promise<void> {
export async function deleteServerDraft(): Promise<boolean> {
try {
await fetch("/api/drafts/me", {
const res = await fetch("/api/drafts/me", {
method: "DELETE",
credentials: "include",
});
return res.ok;
} catch {
/* ignore — server draft cleanup is best-effort */
return false;
}
}
+75 -5
View File
@@ -1,6 +1,9 @@
import type { CreateFlowState } from "../../app/(app)/create/types";
import { createFlowStateHasKeys } from "./draftHydrationUtils";
import { PUBLISHED_CUSTOM_RULE_SELECTION_KEYS } from "./customRuleFacets";
import {
CUSTOM_RULE_FACETS,
PUBLISHED_CUSTOM_RULE_SELECTION_KEYS,
} from "./customRuleFacets";
import type { StoredLastPublishedRule } from "./lastPublishedRule";
import {
createFlowStateFromPublishedRule,
@@ -17,19 +20,86 @@ function sortedIds(value: unknown): string {
.join(",");
}
function trimmedText(value: unknown): string {
return typeof value === "string" ? value.trim() : "";
}
function canonical(value: unknown): unknown {
if (Array.isArray(value)) return value.map(canonical);
if (value && typeof value === "object") {
const record = value as Record<string, unknown>;
const out: Record<string, unknown> = {};
for (const key of Object.keys(record).sort()) {
const child = record[key];
if (child === undefined) continue;
out[key] = canonical(child);
}
return out;
}
return value;
}
/** Missing and empty objects are the same: the author has not written that copy. */
function sameCopy(a: unknown, b: unknown): boolean {
const normalize = (value: unknown): unknown => {
if (value == null) return null;
if (
typeof value === "object" &&
!Array.isArray(value) &&
Object.keys(value as object).length === 0
) {
return null;
}
return value;
};
return (
JSON.stringify(canonical(normalize(a))) ===
JSON.stringify(canonical(normalize(b)))
);
}
export function createFlowStateMatchesPublishedRule(
state: CreateFlowState,
last: StoredLastPublishedRule,
): boolean {
const patch = createFlowStateFromPublishedRule(last);
const title =
typeof state.title === "string" ? state.title.trim() : "";
const patchTitle =
typeof patch.title === "string" ? patch.title.trim() : "";
const title = typeof state.title === "string" ? state.title.trim() : "";
const patchTitle = typeof patch.title === "string" ? patch.title.trim() : "";
if (title !== patchTitle) return false;
if (
trimmedText(state.communityContext) !== trimmedText(patch.communityContext)
) {
return false;
}
if (trimmedText(state.summary) !== trimmedText(patch.summary)) {
return false;
}
for (const key of PUBLISHED_CUSTOM_RULE_SELECTION_KEYS) {
if (sortedIds(state[key]) !== sortedIds(patch[key])) return false;
}
for (const row of CUSTOM_RULE_FACETS) {
if (
!sameCopy(
state[row.detailOverridesStateKey],
patch[row.detailOverridesStateKey],
)
) {
return false;
}
}
if (
!sameCopy(
state.customMethodCardFieldBlocksById,
patch.customMethodCardFieldBlocksById,
)
) {
return false;
}
if (
!sameCopy(state.customMethodCardMetaById, patch.customMethodCardMetaById)
) {
return false;
}
return true;
}