Wire Publish rule from create flow

This commit is contained in:
adilallo
2026-04-07 22:26:25 -06:00
parent a4f0b449b6
commit 8f932e95cd
16 changed files with 839 additions and 252 deletions
+54 -18
View File
@@ -83,6 +83,21 @@ export async function fetchDraftFromServer(): Promise<CreateFlowState | null> {
const DRAFT_SAVE_NETWORK_ERROR =
"Something went wrong. Check your connection and try again.";
const PUBLISH_FAILED_FALLBACK =
"Something went wrong. Check your connection or try again.";
/** Parse JSON body; empty or invalid bodies return `null` (avoids `response.json()` throws). */
async function safeParseJsonResponse(res: Response): Promise<unknown> {
const text = await res.text();
const trimmed = text.trim();
if (!trimmed) return null;
try {
return JSON.parse(trimmed) as unknown;
} catch {
return null;
}
}
export type SaveDraftResult =
| { ok: true }
| { ok: false; message: string; status?: number };
@@ -131,23 +146,44 @@ export async function publishRule(input: {
title: string;
summary?: string;
document: Record<string, unknown>;
}): Promise<{ ok: true; id: string; title: string } | { error: string }> {
const res = await fetch("/api/rules", {
method: "POST",
credentials: "include",
headers: jsonHeaders,
body: JSON.stringify({
title: input.title,
summary: input.summary,
document: input.document,
}),
});
const data = await parseJson<{
error?: string;
rule?: { id: string; title: string };
}>(res);
if (!res.ok || !data.rule) {
return { error: readApiErrorMessage(data) };
}): Promise<
| { ok: true; id: string; title: string }
| { ok: false; error: string; status?: number }
> {
try {
const res = await fetch("/api/rules", {
method: "POST",
credentials: "include",
headers: jsonHeaders,
body: JSON.stringify({
title: input.title,
summary: input.summary,
document: input.document,
}),
});
const data = (await safeParseJsonResponse(res)) as {
error?: string | { message?: string };
rule?: { id: string; title: string };
} | null;
const rule = data && typeof data === "object" ? data.rule : undefined;
if (!res.ok || !rule) {
const fromBody =
data && typeof data === "object" ? readApiErrorMessage(data) : null;
const msg =
fromBody && fromBody !== "Request failed"
? fromBody
: res.statusText?.trim() || PUBLISH_FAILED_FALLBACK;
return {
ok: false as const,
error: msg,
status: res.status,
};
}
return { ok: true, id: rule.id, title: rule.title };
} catch {
return {
ok: false as const,
error: DRAFT_SAVE_NETWORK_ERROR,
};
}
return { ok: true, id: data.rule.id, title: data.rule.title };
}
+84
View File
@@ -0,0 +1,84 @@
import type { CreateFlowState } from "../../app/create/types";
import type { CommunityRuleDocumentSection } from "../../app/components/sections/CommunityRuleDocument/CommunityRuleDocument.types";
function isDocumentEntry(x: unknown): x is { title: string; body: string } {
if (!x || typeof x !== "object") return false;
const o = x as Record<string, unknown>;
return typeof o.title === "string" && typeof o.body === "string";
}
function isDocumentSection(x: unknown): x is CommunityRuleDocumentSection {
if (!x || typeof x !== "object") return false;
const o = x as Record<string, unknown>;
if (typeof o.categoryName !== "string") return false;
if (!Array.isArray(o.entries)) return false;
return o.entries.every(isDocumentEntry);
}
/** Narrow `CreateFlowState.sections` into Community Rule document sections. */
export function parseSectionsFromCreateFlowState(
state: CreateFlowState,
): CommunityRuleDocumentSection[] {
const raw = state.sections;
if (!Array.isArray(raw)) return [];
const out: CommunityRuleDocumentSection[] = [];
for (const x of raw) {
if (isDocumentSection(x)) out.push(x);
}
return out;
}
export type BuildPublishPayloadResult =
| {
ok: true;
title: string;
summary?: string;
document: Record<string, unknown>;
}
| { ok: false; error: string };
const FALLBACK_CATEGORY = "Overview";
const DEFAULT_FALLBACK_BODY =
"This CommunityRule was created in the create flow. Add more detail in a future edit.";
export function buildPublishPayload(
state: CreateFlowState,
): BuildPublishPayloadResult {
const title = typeof state.title === "string" ? state.title.trim() : "";
if (!title) {
return { ok: false, error: "missingCommunityName" };
}
let summary: string | undefined;
if (typeof state.summary === "string") {
const t = state.summary.trim();
if (t.length > 0) summary = t;
}
let sections = parseSectionsFromCreateFlowState(state);
if (sections.length === 0) {
const body = summary ?? DEFAULT_FALLBACK_BODY;
sections = [
{
categoryName: FALLBACK_CATEGORY,
entries: [{ title: "Community", body }],
},
];
}
if (summary !== undefined) {
return { ok: true, title, summary, document: { sections } };
}
return { ok: true, title, document: { sections } };
}
/** Read `document.sections` from a stored published payload for display. */
export function parseDocumentSectionsForDisplay(
document: unknown,
): CommunityRuleDocumentSection[] {
if (!document || typeof document !== "object") return [];
const sections = (document as Record<string, unknown>).sections;
if (!Array.isArray(sections)) return [];
return sections.filter(isDocumentSection);
}
+50
View File
@@ -0,0 +1,50 @@
/**
* Bridges final-review → completed without query strings.
* Replace with GET /api/rules/[id] (CR-81) when public rule fetch exists.
*/
export const CREATE_FLOW_LAST_PUBLISHED_KEY = "createFlow.lastPublished";
export type StoredLastPublishedRule = {
id: string;
title: string;
summary?: string | null;
document: Record<string, unknown>;
};
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;
}
return {
id: o.id,
title: o.title,
summary,
document: doc as Record<string, unknown>,
};
} catch {
return null;
}
}