102 lines
3.4 KiB
TypeScript
102 lines
3.4 KiB
TypeScript
import type { CopiedNameHit } from "./copiedRuleDocument";
|
|
|
|
/**
|
|
* Bridges final-review → completed without query strings, and re-opens a rule
|
|
* from profile (`/create/completed?ruleId=…`) after GET /api/rules/[id].
|
|
* Stakeholder cards on Profile open the reader page at `/rules/[id]`.
|
|
*/
|
|
export const CREATE_FLOW_LAST_PUBLISHED_KEY = "createFlow.lastPublished";
|
|
|
|
export type StoredLastPublishedRule = {
|
|
id: string;
|
|
title: string;
|
|
summary?: string | null;
|
|
document: Record<string, unknown>;
|
|
/** ISO date the Rule was finished, for RULE.md front matter. */
|
|
finishedAt?: string;
|
|
/** Present after Make a copy; drives the copy-ready banner and lavender surface. */
|
|
copiedFromTitle?: string;
|
|
copiedNameHit?: CopiedNameHit | null;
|
|
};
|
|
|
|
export function finishedAtIso(value: unknown): string | undefined {
|
|
if (typeof value === "string" && value.trim().length > 0) {
|
|
const date = new Date(value);
|
|
if (!Number.isNaN(date.getTime())) return date.toISOString();
|
|
}
|
|
if (value instanceof Date && !Number.isNaN(value.getTime())) {
|
|
return value.toISOString();
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
export function writeLastPublishedRule(data: StoredLastPublishedRule): void {
|
|
if (typeof sessionStorage === "undefined") return;
|
|
sessionStorage.setItem(CREATE_FLOW_LAST_PUBLISHED_KEY, JSON.stringify(data));
|
|
}
|
|
|
|
export function clearLastPublishedRule(): void {
|
|
if (typeof sessionStorage === "undefined") return;
|
|
sessionStorage.removeItem(CREATE_FLOW_LAST_PUBLISHED_KEY);
|
|
}
|
|
|
|
export function readLastPublishedRule(): StoredLastPublishedRule | null {
|
|
if (typeof sessionStorage === "undefined") return null;
|
|
const raw = sessionStorage.getItem(CREATE_FLOW_LAST_PUBLISHED_KEY);
|
|
if (!raw) return null;
|
|
try {
|
|
const parsed = JSON.parse(raw) as unknown;
|
|
if (!parsed || typeof parsed !== "object") return null;
|
|
const o = parsed as Record<string, unknown>;
|
|
if (typeof o.id !== "string" || typeof o.title !== "string") return null;
|
|
const doc = o.document;
|
|
if (doc === null || typeof doc !== "object" || Array.isArray(doc)) {
|
|
return null;
|
|
}
|
|
const summaryVal = o.summary;
|
|
let summary: string | null | undefined;
|
|
if (typeof summaryVal === "string") {
|
|
summary = summaryVal;
|
|
} else if (summaryVal === null) {
|
|
summary = null;
|
|
} else {
|
|
summary = undefined;
|
|
}
|
|
let finishedAt: string | undefined;
|
|
if (typeof o.finishedAt === "string" && o.finishedAt.trim().length > 0) {
|
|
finishedAt = o.finishedAt;
|
|
}
|
|
let copiedFromTitle: string | undefined;
|
|
if (
|
|
typeof o.copiedFromTitle === "string" &&
|
|
o.copiedFromTitle.trim().length > 0
|
|
) {
|
|
copiedFromTitle = o.copiedFromTitle;
|
|
}
|
|
let copiedNameHit: CopiedNameHit | null | undefined;
|
|
if (o.copiedNameHit && typeof o.copiedNameHit === "object") {
|
|
const hit = o.copiedNameHit as Record<string, unknown>;
|
|
if (
|
|
typeof hit.categoryName === "string" &&
|
|
typeof hit.entryTitle === "string"
|
|
) {
|
|
copiedNameHit = {
|
|
categoryName: hit.categoryName,
|
|
entryTitle: hit.entryTitle,
|
|
};
|
|
}
|
|
}
|
|
return {
|
|
id: o.id,
|
|
title: o.title,
|
|
summary,
|
|
document: doc as Record<string, unknown>,
|
|
...(finishedAt ? { finishedAt } : {}),
|
|
...(copiedFromTitle ? { copiedFromTitle } : {}),
|
|
...(copiedNameHit ? { copiedNameHit } : {}),
|
|
};
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|