51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import type { Block, ProjectConfig } from "@/features/editor/state/editorStore";
|
|
|
|
interface PublicProjectSnapshot {
|
|
slug: string;
|
|
title: string;
|
|
status: string;
|
|
blocks: Block[];
|
|
projectConfig: ProjectConfig | null;
|
|
}
|
|
|
|
function getStore(): Map<string, PublicProjectSnapshot> {
|
|
const g = globalThis as any;
|
|
if (!g.__PB_PUBLIC_PROJECTS) {
|
|
g.__PB_PUBLIC_PROJECTS = new Map<string, PublicProjectSnapshot>();
|
|
}
|
|
return g.__PB_PUBLIC_PROJECTS as Map<string, PublicProjectSnapshot>;
|
|
}
|
|
|
|
export function cachePublicProject(snapshot: PublicProjectSnapshot): void {
|
|
const store = getStore();
|
|
store.set(snapshot.slug, snapshot);
|
|
}
|
|
|
|
export function getCachedPublicProject(slug: string): PublicProjectSnapshot | null {
|
|
const store = getStore();
|
|
return store.get(slug) ?? null;
|
|
}
|
|
|
|
export function parseProjectContentJson(rawContent: any): {
|
|
blocks: Block[];
|
|
projectConfig: ProjectConfig | null;
|
|
} {
|
|
let blocks: Block[] = [];
|
|
let projectConfig: ProjectConfig | null = null;
|
|
|
|
if (Array.isArray(rawContent)) {
|
|
blocks = rawContent as Block[];
|
|
} else if (rawContent && typeof rawContent === "object") {
|
|
const maybeBlocks = (rawContent as any).blocks;
|
|
if (Array.isArray(maybeBlocks)) {
|
|
blocks = maybeBlocks as Block[];
|
|
const maybeConfig = (rawContent as any).projectConfig;
|
|
if (maybeConfig && typeof maybeConfig === "object") {
|
|
projectConfig = maybeConfig as ProjectConfig;
|
|
}
|
|
}
|
|
}
|
|
|
|
return { blocks, projectConfig };
|
|
}
|