135 lines
3.9 KiB
TypeScript
135 lines
3.9 KiB
TypeScript
import type { CreateFlowState } from "../../app/(app)/create/types";
|
|
import { createFlowStateHasKeys } from "./draftHydrationUtils";
|
|
import {
|
|
CUSTOM_RULE_FACETS,
|
|
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(",");
|
|
}
|
|
|
|
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() : "";
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* 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),
|
|
};
|
|
}
|