Load the finished Rule itself on Edit, and ask before replacing a draft in progress.

This commit is contained in:
adilallo committed 2026-11-22 17:49:09 -07:00
1 parent 942c7cc9de
commit d576ed81d9
4 files changed
+171 -7

No files matched your search

+64
View File
@@ -0,0 +1,64 @@
import type { CreateFlowState } from "../../app/(app)/create/types";
import { createFlowStateHasKeys } from "./draftHydrationUtils";
import { PUBLISHED_CUSTOM_RULE_SELECTION_KEYS } from "./customRuleFacets";
import type { StoredLastPublishedRule } from "./lastPublishedRule";
import {
createFlowStateFromPublishedRule,
methodSectionsPinsFromPublishedHydratePatch,
} from "./publishedDocumentToCreateFlowState";
function sortedIds(value: unknown): string {
if (!Array.isArray(value)) return "";
return value
.filter((id): id is string => typeof id === "string")
.map((id) => id.trim())
.filter((id) => id.length > 0)
.sort()
.join(",");
}
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() : "";
if (title !== patchTitle) return false;
for (const key of PUBLISHED_CUSTOM_RULE_SELECTION_KEYS) {
if (sortedIds(state[key]) !== sortedIds(patch[key])) return false;
}
return true;
}
/**
* True when Edit would drop a different in-progress draft to load this
* finished Rule. Empty state and a draft that already is this Rule do not
* need a confirm.
*/
export function publishedRuleEditWouldReplaceDraft(
state: CreateFlowState,
last: StoredLastPublishedRule,
): boolean {
if (!createFlowStateHasKeys(state)) return false;
const editingId =
typeof state.editingPublishedRuleId === "string"
? state.editingPublishedRuleId.trim()
: "";
if (editingId.length > 0 && editingId !== last.id) return true;
return !createFlowStateMatchesPublishedRule(state, last);
}
/** Full wizard state for editing a finished Rule — not a merge onto another draft. */
export function createFlowStateForEditingPublishedRule(
last: StoredLastPublishedRule,
): CreateFlowState {
const patch = createFlowStateFromPublishedRule(last);
return {
...patch,
methodSectionsPinCommitted:
methodSectionsPinsFromPublishedHydratePatch(patch),
};
}