refactor: performance

This commit is contained in:
2026-03-12 21:01:43 +01:00
parent 92fdba7eef
commit 029bab8917
9 changed files with 109 additions and 47 deletions

View File

@@ -7,6 +7,20 @@ import type { AppNode, AppEdge } from '@/lib/graph/nodeTypes'
import { DEFAULT_NODE_STYLE } from '@/lib/graph/flowUtils'
import { loadGraphFromStorage } from '@/app/pleroma/projectGraphStorage'
/** Ensure each edge has data.targetType from the target node (for labels without depending on nodes in edgesForFlow). */
export function backfillEdgeTargetTypes(
nodes: AppNode[],
edges: AppEdge[]
): AppEdge[] {
const typeById = new Map(nodes.map((n) => [n.id, n.type ?? '']))
return edges.map((e) => {
const targetType = typeById.get(e.target) ?? (e.data as Record<string, unknown>)?.targetType ?? ''
const data = typeof e.data === 'object' && e.data !== null ? (e.data as Record<string, unknown>) : {}
if (data.targetType === targetType) return e
return { ...e, data: { ...data, targetType } }
})
}
const NODE_GAP = 150
const EXAMPLE_NODES: AppNode[] = [
@@ -45,20 +59,24 @@ const EXAMPLE_EDGES: AppEdge[] = [
]
export function getExampleGraph(): { nodes: AppNode[]; edges: AppEdge[] } {
return {
nodes: EXAMPLE_NODES.map((n) => ({
...n,
data: n.data && typeof n.data === 'object' ? { ...(n.data as object) } : n.data,
})),
edges: EXAMPLE_EDGES.map((e) => ({ ...e })),
}
const nodes = EXAMPLE_NODES.map((n) => ({
...n,
data: n.data && typeof n.data === 'object' ? { ...(n.data as object) } : n.data,
}))
const edges = backfillEdgeTargetTypes(
nodes,
EXAMPLE_EDGES.map((e) => ({ ...e }))
)
return { nodes, edges }
}
export function getInitialGraph(projectId: string | undefined): { nodes: AppNode[]; edges: AppEdge[] } {
if (projectId) {
const stored = loadGraphFromStorage(projectId)
if (stored && (stored.nodes.length > 0 || stored.edges.length > 0)) {
return { nodes: stored.nodes as AppNode[], edges: stored.edges as AppEdge[] }
const nodes = stored.nodes as AppNode[]
const edges = backfillEdgeTargetTypes(nodes, stored.edges as AppEdge[])
return { nodes, edges }
}
return { nodes: [], edges: [] }
}