51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import type { FlowBoardDraft, FlowBoardSnapshotData } from "@/lib/db/schema";
|
|
|
|
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
}
|
|
|
|
function normalizeRecord(value: unknown): Record<string, unknown> | null {
|
|
return isRecord(value) ? value : null;
|
|
}
|
|
|
|
export function normalizeFlowBoardTitle(value: unknown) {
|
|
const title = String(value || "").trim();
|
|
return title.slice(0, 256) || "未命名流程白板";
|
|
}
|
|
|
|
export function normalizeFlowBoardSnapshot(
|
|
value: unknown
|
|
): FlowBoardSnapshotData {
|
|
const raw = isRecord(value) ? value : {};
|
|
const updatedAt =
|
|
typeof raw.updatedAt === "string" && raw.updatedAt.trim()
|
|
? raw.updatedAt
|
|
: new Date().toISOString();
|
|
|
|
return {
|
|
children: Array.isArray(raw.children) ? raw.children.filter(isRecord) : [],
|
|
viewport: normalizeRecord(raw.viewport),
|
|
theme: normalizeRecord(raw.theme),
|
|
updatedAt,
|
|
};
|
|
}
|
|
|
|
export function mapFlowBoardDraft(draft: FlowBoardDraft) {
|
|
return {
|
|
id: draft.id,
|
|
title: draft.title || "未命名流程白板",
|
|
snapshot: draft.snapshot ?? { children: [] },
|
|
createdAt: draft.createdAt?.toISOString() ?? null,
|
|
updatedAt: draft.updatedAt?.toISOString() ?? null,
|
|
};
|
|
}
|
|
|
|
export function mapFlowBoardDraftSummary(draft: FlowBoardDraft) {
|
|
return {
|
|
id: draft.id,
|
|
title: draft.title || "未命名流程白板",
|
|
createdAt: draft.createdAt?.toISOString() ?? null,
|
|
updatedAt: draft.updatedAt?.toISOString() ?? null,
|
|
};
|
|
}
|