Files
community-rule/lib/create/lastPublishedRule.ts
T
adilallo d9c475a967 Give finished Rules a print path, folded launch counts, and a two-day follow-up.
Export is PDF, RULE.md, and Print on US Letter, with every shared term, no member names, and a takeaway that points at Ask an organizer. Finished Rules keep type and place so the team can read folded counts without a database, Umami can sit beside the app with no cookies, and the person who holds the pen gets working PDF and RULE.md links 48 hours later.
2026-11-10 18:32:33 -07:00

97 lines
3.2 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]. Profile
* "Manage" links here; stakeholder "View" uses `/invite/[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 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;
}
}