Files
semantica/explorer/src/workspaces/GraphWorkspace/nodeMarkdownSync.ts
T
Yuang PengandSameer Kadam b92fed2d0a feat(explorer): add Markdown editor write path (#1349)
* feat(explorer): add Markdown editing write path

* fix(explorer): address markdown editor review findings

* feat(explorer): add Markdown editor write path

---------

Co-authored-by: Sameer Kadam <sskadam6305@gmail.com>
2026-09-04 01:38:17 +05:30

82 lines
2.0 KiB
TypeScript

export interface NodeMarkdownAttributeUpdate {
label: string;
content: string;
properties: Record<string, unknown>;
}
export interface GraphNodeMarkdownSnapshot {
id: string;
type: string;
content: string;
properties: Record<string, unknown>;
valid_from: string | null;
valid_until: string | null;
}
export interface SavedNodeMarkdownAttributeUpdate extends NodeMarkdownAttributeUpdate {
nodeType: string;
valid_from: string | null;
valid_until: string | null;
}
export class NodeMarkdownRefreshGuard {
private readonly generations = new Map<string, number>();
begin(nodeId: string): number {
const generation = (this.generations.get(nodeId) ?? 0) + 1;
this.generations.set(nodeId, generation);
return generation;
}
invalidate(nodeId: string): void {
this.begin(nodeId);
}
isCurrent(nodeId: string, generation: number): boolean {
return this.generations.get(nodeId) === generation;
}
}
export function buildNodeMarkdownAttributeUpdate(
nodeId: string,
content: string,
properties: Record<string, unknown>,
): NodeMarkdownAttributeUpdate {
return {
label: content || nodeId,
content,
properties: {
...properties,
content,
},
};
}
export async function readNodeMarkdownAttributeUpdate(
nodeId: string,
fetcher: typeof fetch = fetch,
): Promise<SavedNodeMarkdownAttributeUpdate> {
const response = await fetcher(
`/api/graph/node?node_id=${encodeURIComponent(nodeId)}`,
);
if (!response.ok) {
throw new Error(`Graph node refresh failed (${response.status}).`);
}
const node = await response.json() as GraphNodeMarkdownSnapshot;
if (node.id !== nodeId) {
throw new Error("Graph node refresh returned a different resource.");
}
return {
...buildNodeMarkdownAttributeUpdate(
node.id,
node.content,
node.properties ?? {},
),
nodeType: node.type,
valid_from: node.valid_from ?? null,
valid_until: node.valid_until ?? null,
};
}