65 lines
2.1 KiB
TypeScript
65 lines
2.1 KiB
TypeScript
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),
|
|
};
|
|
}
|