refactoring

This commit is contained in:
2026-03-12 17:07:42 +01:00
parent 084863909a
commit e29c5d643c
11 changed files with 136 additions and 87 deletions

View File

@@ -20,6 +20,7 @@ import {
processSvgDisplay,
stripTemplateSyntax,
} from '@/lib/graph/rendering'
import { isReachable, resolveExtendsRef, getTemplateRefs } from '@/lib/graph/templateRefs'
import type { ConfigTypeId, SourceRenderingLogicContext } from '@/lib/graph/rendering'
export type RenderingNodeData = {
@@ -136,54 +137,22 @@ export function useRenderingNodeState(
: ''
const connectedNodeIds = useMemo(() => {
const edgeList = edges as { source: string; target: string }[]
const nodeList = nodes as { id: string; type?: string; data?: unknown }[]
const out = new Set<string>()
const isReachable = (startId: string, targetId: string) => {
const q: string[] = [startId]
const seen = new Set<string>([startId])
while (q.length) {
const cur = q.shift()!
if (cur === targetId) return true
for (const e of edges as { source: string; target: string }[]) {
if (e.source === cur && !seen.has(e.target)) {
seen.add(e.target)
q.push(e.target)
}
}
}
return false
}
const resolveRef = (name: string) => {
const refName = name.replace(/\.(puml|html)$/, '').trim()
return (nodes as { id: string; data?: { title?: string } }[]).find(
(n) => n.id === refName || n.data?.title === refName
)?.id ?? refName
}
const getTemplateRefs = (content: string): string[] => {
const refs: string[] = []
const extendMatch = content.match(/\{\%\s*extends\s+["']([^"']+)["']\s*\%\}/)
if (extendMatch) refs.push(extendMatch[1].trim())
const includeRegex = /\{\%\s*include\s+["']([^"']+)["']\s*\%\}/g
let m
while ((m = includeRegex.exec(content)) !== null) refs.push(m[1].trim())
const importRegex = /\{\%\s*import\s+["']([^"']+)["']\s+as\s+\w+\s*\%\}/g
while ((m = importRegex.exec(content)) !== null) refs.push(m[1].trim())
return refs
}
const addConfigRefs = (nodeId: string, visited: Set<string>) => {
if (visited.has(nodeId)) return
const node = (nodes as { id: string; type?: string; data?: unknown }[]).find(
(n) => n.id === nodeId && n.type === 'config'
)
const node = nodeList.find((n) => n.id === nodeId && n.type === 'config')
if (!node) return
visited.add(nodeId)
out.add(nodeId)
const content = getConfigContent((node.data ?? undefined) as Record<string, unknown> | undefined)
for (const ref of getTemplateRefs(content)) {
const refId = resolveRef(ref)
const refId = resolveExtendsRef(nodeList, ref)
if (
refId &&
(nodes as { id: string; type?: string }[]).some((n) => n.id === refId && n.type === 'config') &&
isReachable(refId, id)
nodeList.some((n) => n.id === refId && n.type === 'config') &&
isReachable(edgeList, refId, id)
) {
addConfigRefs(refId, visited)
}
@@ -191,11 +160,11 @@ export function useRenderingNodeState(
}
const configVisited = new Set<string>()
for (const nid of incomingIds) {
const node = (nodes as { id: string; type?: string }[]).find((n) => n.id === nid)
const node = nodeList.find((n) => n.id === nid)
if (node?.type === 'config') addConfigRefs(nid, configVisited)
else out.add(nid)
}
for (const e of edges as { source: string; target: string }[]) {
for (const e of edgeList) {
if (out.has(e.target)) out.add(e.source)
}
return out